Adding a factory bet for the API and navigation to the home page once the bet is created 🏭

pull/9/head
Emre KARTAL 1 year ago
parent f4adde1bb3
commit 221464696b

@ -18,6 +18,7 @@ class CreationBetViewModel: ObservableObject {
@Published var isPublic = true
@Published var endRegisterDate = Date()
@Published var endBetDate = Date()
@Published var betAdded = false
@Published var themeFieldError: String?
@Published var descriptionFieldError: String?
@ -35,6 +36,8 @@ class CreationBetViewModel: ObservableObject {
if let user = AppStateContainer.shared.user {
manager.addBet(theme: theme, description: description, endRegister: endRegisterDate, endBet: endBetDate, isPublic: isPublic, creator: user)
}
betAdded = true
}
func checkAndSetError(forTheme checkTheme: Bool, forDescription checkDescription: Bool, forEndRegisterDate checkEndRegisterDate: Bool, forEndBetDate checkEndBetDate: Bool) -> Bool {

@ -39,7 +39,7 @@ struct CreationBetView: View {
]
@State var groupedItems: [[String]] = [[String]] ()
private func updateGroupedItems() {
var updatedGroupedItems: [[String]] = [[String]] ()
@ -71,6 +71,9 @@ struct CreationBetView: View {
var body: some View {
VStack(alignment: .center, spacing: 0) {
NavigationLink(destination: MainView(page: "Bet").navigationBarBackButtonHidden(true), isActive: $viewModel.betAdded) {
EmptyView()
}
TopBar(showMenu: self.$showMenu)
TabView(selection: $selectedTab) {

@ -0,0 +1,65 @@
//
// FactoryApiBet.swift
//
//
// Created by Emre on 11/01/2024.
//
import Foundation
import Model
public class FactoryApiBet: FactoryBet {
public func toResponse(bet: Bet) -> [String: Any] {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let json: [String: Any] = [
"theme": bet.theme,
"sentenceBet": bet.phrase,
"endRegistration": dateFormatter.string(from: bet.endRegisterDate),
"endBet": dateFormatter.string(from: bet.endBetDate),
"isPrivate": String(bet.isPublic),
"response": [],
]
return json
}
public func toModel(from json: [String: Any]) -> Bet? {
guard let theme = json["theme"] as? String,
let phrase = json["sentenceBet"] as? String,
let endRegisterDateString = json["endRegistration"] as? String,
let endBetDateString = json["endBet"] as? String,
let isPublicString = json["isPrivate"] as? String,
let createdBy = json["createdBy"] as? User, // Assuming User object can be parsed from JSON
let type = json["type"] as? Int else {
return nil
}
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
guard let endRegisterDate = dateFormatter.date(from: endRegisterDateString),
let endBetDate = dateFormatter.date(from: endBetDateString) else {
return nil
}
let isPublic = (isPublicString.lowercased() == "true")
return toModel(theme: theme, description: phrase, endRegister: endRegisterDate, endBet: endBetDate, isPublic: isPublic, creator: createdBy, type: type)
}
public func toModel(theme: String, description: String, endRegister: Date, endBet: Date, isPublic: Bool, creator: User, type: Int) -> Bet {
switch type {
case 0:
return BinaryBet(theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: [])
case 1:
return MatchBet(theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: [], nameTeam1: "", nameTeam2: "")
case 2:
return CustomBet(theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: [])
default:
return BinaryBet(theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: [])
}
}
}

@ -30,18 +30,8 @@ public struct UserApiManager: UserDataManager {
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let json: [String: Any] = [
"theme": bet.theme,
"sentenceBet": bet.phrase,
"endRegistration": dateFormatter.string(from: bet.endRegisterDate),
"endBet": dateFormatter.string(from: bet.endBetDate),
"isPrivate": String(bet.isPublic),
"response": [],
"createdBy": token
]
var json = FactoryApiBet().toResponse(bet: bet)
json["createdBy"] = token
if let jsonData = try? JSONSerialization.data(withJSONObject: json, options: []){
URLSession.shared.uploadTask(with: request, from: jsonData) { data, response, error in

@ -10,31 +10,31 @@ import Foundation
/// A class representing a betting entity, including details about the bet theme, participants, and deadlines.
public class Bet: ObservableObject {
/// The theme or topic of the bet.
public var theme: String
public private(set) var theme: String
/// The specific phrase or question related to the bet.
public var phrase: String
public private(set) var phrase: String
/// The deadline for users to register for the bet.
public var endRegisterDate: Date
public private(set) var endRegisterDate: Date
/// The deadline for the actual betting to take place.
public var endBetDate: Date
public private(set) var endBetDate: Date
/// The total stakes or amount involved in the bet.
public var totalStakes: Int
public private(set) var totalStakes: Int
/// Indicates whether the bet is public or private.
public var isPublic: Bool
public private(set) var isPublic: Bool
/// List of users who are invited to participate in the bet.
public var invited: [User]
public private(set) var invited: [User]
/// The user who created the bet.
public var author: User
public private(set) var author: User
/// List of users who have registered for the bet.
public var registered: [User]
public private(set) var registered: [User]
/// Custom Constructor
///

@ -8,7 +8,7 @@
import Foundation
public protocol FactoryBet {
func toResponse()
func toModel() -> Bet
func toResponse(bet: Bet) -> [String: Any]
func toModel(from json: [String: Any]) -> Bet?
func toModel(theme: String, description: String, endRegister: Date, endBet: Date, isPublic: Bool, creator: User, type: Int) -> Bet
}

Loading…
Cancel
Save