From 438cdb63d70ffd81c56979e8155e87e2e2495a20 Mon Sep 17 00:00:00 2001 From: "emre.kartal" Date: Wed, 10 Jan 2024 13:38:41 +0100 Subject: [PATCH 1/3] create bet work --- Sources/AllInApp/AllIn/AllInApp.swift | 3 --- .../AllInApp/AllIn/Services/AuthService.swift | 16 ++++++++++--- Sources/Api/Sources/Api/UserApiManager.swift | 24 ++++++++++++++----- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/Sources/AllInApp/AllIn/AllInApp.swift b/Sources/AllInApp/AllIn/AllInApp.swift index fdd165e..f79fa97 100644 --- a/Sources/AllInApp/AllIn/AllInApp.swift +++ b/Sources/AllInApp/AllIn/AllInApp.swift @@ -9,8 +9,6 @@ import SwiftUI import DependencyInjection import Model import ViewModel -import StubLib -import Api @main struct AllInApp: App { @@ -20,7 +18,6 @@ struct AllInApp: App { init() { DI.addSingleton(IAuthService.self, AuthService()) - DI.addSingleton(ManagerVM.self, ManagerVM(withModel: Manager(withBetDataManager: BetStubManager(), withUserDataManager: UserApiManager()))) } var body: some Scene { diff --git a/Sources/AllInApp/AllIn/Services/AuthService.swift b/Sources/AllInApp/AllIn/Services/AuthService.swift index b318d98..0f20ee1 100644 --- a/Sources/AllInApp/AllIn/Services/AuthService.swift +++ b/Sources/AllInApp/AllIn/Services/AuthService.swift @@ -7,6 +7,10 @@ import Foundation import Model +import ViewModel +import DependencyInjection +import Api +import StubLib class AuthService: IAuthService { @@ -35,6 +39,8 @@ class AuthService: IAuthService { if status != 200 { completion(status) AppStateContainer.shared.authenticationRefresh = nil; + } else { + self.initManagerVM(token: token) } } } @@ -71,6 +77,8 @@ class AuthService: IAuthService { if status != 200 { completion(status) AppStateContainer.shared.authenticationRefresh = nil; + } else { + self.initManagerVM(token: token) } } } @@ -86,9 +94,7 @@ class AuthService: IAuthService { guard let token = AppStateContainer.shared.authenticationRefresh else { return } - - print(token) - + let url = URL(string: Config.allInApi + "users/token")! var request = URLRequest(url: url) request.httpMethod = "GET" @@ -103,6 +109,7 @@ class AuthService: IAuthService { let user = User.mapUser(from: userJson) { AppStateContainer.shared.user = user AppStateContainer.shared.loggedState.connectedUser = true + self.initManagerVM(token: token) } } else { AppStateContainer.shared.authenticationRefresh = nil @@ -136,5 +143,8 @@ class AuthService: IAuthService { }.resume() } + private func initManagerVM(token: String) { + DependencyInjection.shared.addSingleton(ManagerVM.self, ManagerVM(withModel: Manager(withBetDataManager: BetStubManager(), withUserDataManager: UserApiManager(withUserToken: token)))) + } } diff --git a/Sources/Api/Sources/Api/UserApiManager.swift b/Sources/Api/Sources/Api/UserApiManager.swift index 9624e17..818b65f 100644 --- a/Sources/Api/Sources/Api/UserApiManager.swift +++ b/Sources/Api/Sources/Api/UserApiManager.swift @@ -11,8 +11,11 @@ import Model let allInApi = "https://codefirst.iut.uca.fr/containers/AllDev-api" public struct UserApiManager: UserDataManager { - - public init() { + + public let token: String + + public init(withUserToken token: String) { + self.token = token } public func getBets(withIndex index: Int, withCount count: Int) -> [Bet] { @@ -21,6 +24,7 @@ public struct UserApiManager: UserDataManager { public func addBet(bet: Bet) { + print(token) let url = URL(string: allInApi + "bets/add")! var request = URLRequest(url: url) request.httpMethod = "POST" @@ -29,19 +33,27 @@ public struct UserApiManager: UserDataManager { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" - let json = [ + 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": "" + "response": [], + "createdBy": token ] + print (json) + if let jsonData = try? JSONSerialization.data(withJSONObject: json, options: []){ URLSession.shared.uploadTask(with: request, from: jsonData) { data, response, error in - + print ("ALLIN : Add BET") + if let httpResponse = response as? HTTPURLResponse { + if httpResponse.statusCode == 201 { + + } + print(httpResponse.statusCode) + } }.resume() } } From f4adde1bb3a2f3bdaa5d08e084b4472edbfbeb72 Mon Sep 17 00:00:00 2001 From: "emre.kartal" Date: Thu, 11 Jan 2024 12:01:47 +0100 Subject: [PATCH 2/3] Converted all the model's structs to classes :hammer: --- .../AllInApp/AllIn/Services/AuthService.swift | 2 +- .../AllIn/Views/CreationBetView.swift | 4 +- Sources/Api/Package.swift | 3 + Sources/Api/Sources/Api/UserApiManager.swift | 4 +- Sources/Model/Package.swift | 3 + Sources/Model/Sources/Model/Bet.swift | 64 +++++++++++++++---- Sources/Model/Sources/Model/BinaryBet.swift | 47 +++++--------- .../Sources/Model/BinaryParticipation.swift | 21 ++++-- Sources/Model/Sources/Model/CustomBet.swift | 13 +--- .../Sources/Model/CustomBetResponse.swift | 10 ++- .../Sources/Model/CustomParticipation.swift | 20 ++++-- Sources/Model/Sources/Model/Manager.swift | 1 - Sources/Model/Sources/Model/MatchBet.swift | 34 +++++++--- .../Sources/Model/MatchParticipation.swift | 30 ++++++--- .../Model/Sources/Model/Participation.swift | 31 +++++++-- Sources/StubLib/Package.swift | 3 + 16 files changed, 195 insertions(+), 95 deletions(-) diff --git a/Sources/AllInApp/AllIn/Services/AuthService.swift b/Sources/AllInApp/AllIn/Services/AuthService.swift index 0f20ee1..1a7ee71 100644 --- a/Sources/AllInApp/AllIn/Services/AuthService.swift +++ b/Sources/AllInApp/AllIn/Services/AuthService.swift @@ -58,7 +58,7 @@ class AuthService: IAuthService { request.setValue("application/json", forHTTPHeaderField: "Content-Type") let json = [ - "email": email, + "email": email.lowercased(), "username": username.lowercased(), "password": password, "nbCoins": "0" diff --git a/Sources/AllInApp/AllIn/Views/CreationBetView.swift b/Sources/AllInApp/AllIn/Views/CreationBetView.swift index 6aa4975..1634241 100644 --- a/Sources/AllInApp/AllIn/Views/CreationBetView.swift +++ b/Sources/AllInApp/AllIn/Views/CreationBetView.swift @@ -113,7 +113,7 @@ struct CreationBetView: View { .frame(height: 40) ) .frame(width: 350, height: 40) - .foregroundColor(.black) + .foregroundColor(AllInColors.primaryTextColor) .overlay( RoundedRectangle(cornerRadius: 10, style: .continuous) .stroke(AllInColors.delimiterGrey, lineWidth: 1) @@ -155,7 +155,7 @@ struct CreationBetView: View { .frame(height: 110) ) .frame(width: 350, height: 110) - .foregroundColor(.black) + .foregroundColor(AllInColors.primaryTextColor) .overlay( RoundedRectangle(cornerRadius: 10, style: .continuous) .stroke(AllInColors.delimiterGrey, lineWidth: 1) diff --git a/Sources/Api/Package.swift b/Sources/Api/Package.swift index 658752e..2b21b0a 100644 --- a/Sources/Api/Package.swift +++ b/Sources/Api/Package.swift @@ -5,6 +5,9 @@ import PackageDescription let package = Package( name: "Api", + platforms: [ + .iOS(.v13) + ], products: [ // Products define the executables and libraries a package produces, and make them visible to other packages. .library( diff --git a/Sources/Api/Sources/Api/UserApiManager.swift b/Sources/Api/Sources/Api/UserApiManager.swift index 818b65f..ebb994d 100644 --- a/Sources/Api/Sources/Api/UserApiManager.swift +++ b/Sources/Api/Sources/Api/UserApiManager.swift @@ -42,9 +42,7 @@ public struct UserApiManager: UserDataManager { "response": [], "createdBy": token ] - - print (json) - + if let jsonData = try? JSONSerialization.data(withJSONObject: json, options: []){ URLSession.shared.uploadTask(with: request, from: jsonData) { data, response, error in print ("ALLIN : Add BET") diff --git a/Sources/Model/Package.swift b/Sources/Model/Package.swift index 5e7194a..2724644 100644 --- a/Sources/Model/Package.swift +++ b/Sources/Model/Package.swift @@ -5,6 +5,9 @@ import PackageDescription let package = Package( name: "Model", + platforms: [ + .iOS(.v13) + ], products: [ // Products define the executables and libraries a package produces, and make them visible to other packages. .library( diff --git a/Sources/Model/Sources/Model/Bet.swift b/Sources/Model/Sources/Model/Bet.swift index c021ac9..3721e4a 100644 --- a/Sources/Model/Sources/Model/Bet.swift +++ b/Sources/Model/Sources/Model/Bet.swift @@ -7,16 +7,56 @@ import Foundation -public protocol Bet { - //public private(set) var id: String - var theme: String { get set } - var phrase: String { get set } - var endRegisterDate: Date { get set } - var endBetDate: Date { get set } - var totalStakes: Int { get set } - var isPublic: Bool { get set } - var invited: [User] { get set } - var author: User { get set } - var registered: [User] { get set } - +/// 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 + + /// The specific phrase or question related to the bet. + public var phrase: String + + /// The deadline for users to register for the bet. + public var endRegisterDate: Date + + /// The deadline for the actual betting to take place. + public var endBetDate: Date + + /// The total stakes or amount involved in the bet. + public var totalStakes: Int + + /// Indicates whether the bet is public or private. + public var isPublic: Bool + + /// List of users who are invited to participate in the bet. + public var invited: [User] + + /// The user who created the bet. + public var author: User + + /// List of users who have registered for the bet. + public var registered: [User] + + /// Custom Constructor + /// + /// - Parameters: + /// - theme: The theme or topic of the bet. + /// - phrase: The specific phrase or question related to the bet. + /// - endRegisterDate: The deadline for users to register for the bet. + /// - endBetDate: The deadline for the actual betting to take place. + /// - totalStakes: The total stakes or amount involved in the bet. + /// - isPublic: Indicates whether the bet is public or private. + /// - invited: List of users who are invited to participate in the bet. + /// - author: The user who created the bet. + /// - registered: List of users who have registered for the bet. + public init(theme: String, phrase: String, endRegisterDate: Date, endBetDate: Date, totalStakes: Int, isPublic: Bool, invited: [User], author: User, registered: [User]) { + self.theme = theme + self.phrase = phrase + self.endRegisterDate = endRegisterDate + self.endBetDate = endBetDate + self.totalStakes = totalStakes + self.isPublic = isPublic + self.invited = invited + self.author = author + self.registered = registered + } } diff --git a/Sources/Model/Sources/Model/BinaryBet.swift b/Sources/Model/Sources/Model/BinaryBet.swift index ad107cb..8767849 100644 --- a/Sources/Model/Sources/Model/BinaryBet.swift +++ b/Sources/Model/Sources/Model/BinaryBet.swift @@ -7,37 +7,22 @@ import Foundation -public struct BinaryBet: Bet { - public var theme: String - public var phrase: String - public var endRegisterDate: Date - public var endBetDate: Date - public var totalStakes: Int - public var isPublic: Bool - public var invited: [User] - public var author: User - public var registered: [User] +/// A subclass of Bet that represents a binary bet, where participants make a choice between two possible outcomes. +public class BinaryBet: Bet { - public init( - theme: String, - phrase: String, - endRegisterDate: Date, - endBetDate: Date, - totalStakes: Int, - isPublic: Bool, - invited: [User], - author: User, - registered: [User] - ) { - self.theme = theme - self.phrase = phrase - self.endRegisterDate = endRegisterDate - self.endBetDate = endBetDate - self.totalStakes = totalStakes - self.isPublic = isPublic - self.invited = invited - self.author = author - self.registered = registered + // Custom Constructor + /// + /// - Parameters: + /// - theme: The theme or topic of the binary bet. + /// - phrase: The specific phrase or question related to the binary bet. + /// - endRegisterDate: The deadline for users to register for the binary bet. + /// - endBetDate: The deadline for the actual betting to take place for the binary bet. + /// - totalStakes: The total stakes or amount involved in the binary bet. + /// - isPublic: Indicates whether the binary bet is public or private. + /// - invited: List of users who are invited to participate in the binary bet. + /// - author: The user who created the binary bet. + /// - registered: List of users who have registered for the binary bet. + public override init(theme: String, phrase: String, endRegisterDate: Date, endBetDate: Date, totalStakes: Int, isPublic: Bool, invited: [User], author: User, registered: [User]) { + super.init(theme: theme, phrase: phrase, endRegisterDate: endRegisterDate, endBetDate: endBetDate, totalStakes: totalStakes, isPublic: isPublic, invited: invited, author: author, registered: registered) } - } diff --git a/Sources/Model/Sources/Model/BinaryParticipation.swift b/Sources/Model/Sources/Model/BinaryParticipation.swift index 33f391c..5a02622 100644 --- a/Sources/Model/Sources/Model/BinaryParticipation.swift +++ b/Sources/Model/Sources/Model/BinaryParticipation.swift @@ -7,16 +7,27 @@ import Foundation +/// Enum to represent the possible answers for a binary participation. public enum YesNo { case yes case no } -public struct BinaryParticipation: Participation { - public var coinAmount: Int - public var date: Date - public var user: User - public var bet: Bet +/// A subclass of Participation that represents a binary participation (yes/no) in a bet. +public class BinaryParticipation: Participation { + /// The answer for the binary participation (yes or no). public var answer: YesNo + /// Custom Constructor + /// + /// - Parameters: + /// - coinAmount: The amount of coins involved in the binary participation. + /// - date: The date and time when the binary participation occurred. + /// - user: The user who participated in the binary bet. + /// - bet: The binary bet in which the user participated. + /// - answer: The answer for the binary participation (yes or no). + public init(coinAmount: Int, date: Date, user: User, bet: Bet, answer: YesNo) { + self.answer = answer + super.init(coinAmount: coinAmount, date: date, user: user, bet: bet) + } } diff --git a/Sources/Model/Sources/Model/CustomBet.swift b/Sources/Model/Sources/Model/CustomBet.swift index 10f1e92..e057643 100644 --- a/Sources/Model/Sources/Model/CustomBet.swift +++ b/Sources/Model/Sources/Model/CustomBet.swift @@ -7,16 +7,7 @@ import Foundation -public struct CustomBet: Bet { - public var theme: String - public var phrase: String - public var endRegisterDate: Date - public var endBetDate: Date - public var totalStakes: Int - public var isPublic: Bool - public var invited: [User] - public var author: User - public var registered: [User] - public var possibleAnswers: [CustomBetResponse] +/// A subclass of Bet that represents a custom bet, allowing users to define their own parameters and rules. +public class CustomBet: Bet { } diff --git a/Sources/Model/Sources/Model/CustomBetResponse.swift b/Sources/Model/Sources/Model/CustomBetResponse.swift index 081ccdc..1720202 100644 --- a/Sources/Model/Sources/Model/CustomBetResponse.swift +++ b/Sources/Model/Sources/Model/CustomBetResponse.swift @@ -7,7 +7,15 @@ import Foundation -public struct CustomBetResponse { +/// A class representing a user's response to a custom bet. +public class CustomBetResponse { + /// The name or description of the custom bet response. public var name: String + /// Custom Constructor + /// + /// - Parameter name: The name or description of the custom bet response. + public init(name: String) { + self.name = name + } } diff --git a/Sources/Model/Sources/Model/CustomParticipation.swift b/Sources/Model/Sources/Model/CustomParticipation.swift index 56ab055..9956aa9 100644 --- a/Sources/Model/Sources/Model/CustomParticipation.swift +++ b/Sources/Model/Sources/Model/CustomParticipation.swift @@ -7,11 +7,21 @@ import Foundation -public struct CustomParticipation: Participation { - public var coinAmount: Int - public var date: Date - public var user: User - public var bet: Bet +/// A subclass of Participation that represents a custom participation in a bet. +public class CustomParticipation: Participation { + /// The user's response to the custom bet. public var answer: CustomBetResponse + /// Custom Constructor + /// + /// - Parameters: + /// - coinAmount: The amount of coins involved in the custom participation. + /// - date: The date and time when the custom participation occurred. + /// - user: The user who participated in the custom bet. + /// - bet: The custom bet in which the user participated. + /// - answer: The user's response to the custom bet. + public init(coinAmount: Int, date: Date, user: User, bet: Bet, answer: CustomBetResponse) { + self.answer = answer + super.init(coinAmount: coinAmount, date: date, user: user, bet: bet) + } } diff --git a/Sources/Model/Sources/Model/Manager.swift b/Sources/Model/Sources/Model/Manager.swift index eba7463..7eab799 100644 --- a/Sources/Model/Sources/Model/Manager.swift +++ b/Sources/Model/Sources/Model/Manager.swift @@ -19,5 +19,4 @@ public struct Manager { public func addBet(bet: Bet) { userDataManager.addBet(bet: bet) } - } diff --git a/Sources/Model/Sources/Model/MatchBet.swift b/Sources/Model/Sources/Model/MatchBet.swift index 4d98567..cddcc4e 100644 --- a/Sources/Model/Sources/Model/MatchBet.swift +++ b/Sources/Model/Sources/Model/MatchBet.swift @@ -7,17 +7,31 @@ import Foundation -public struct MatchBet: Bet { - public var theme: String - public var phrase: String - public var endRegisterDate: Date - public var endBetDate: Date - public var totalStakes: Int - public var isPublic: Bool - public var invited: [User] - public var author: User - public var registered: [User] +/// A subclass of Bet that represents a bet on a match between two teams. +public class MatchBet: Bet { + /// The name of the first team involved in the match. public var nameTeam1: String + + /// The name of the second team involved in the match. public var nameTeam2: String + /// Custom Constructor + /// + /// - Parameters: + /// - theme: The theme or topic of the match bet. + /// - phrase: The specific phrase or question related to the match bet. + /// - endRegisterDate: The deadline for users to register for the match bet. + /// - endBetDate: The deadline for the actual betting to take place for the match bet. + /// - totalStakes: The total stakes or amount involved in the match bet. + /// - isPublic: Indicates whether the match bet is public or private. + /// - invited: List of users who are invited to participate in the match bet. + /// - author: The user who created the match bet. + /// - registered: List of users who have registered for the match bet. + /// - nameTeam1: The name of the first team involved in the match. + /// - nameTeam2: The name of the second team involved in the match. + public init(theme: String, phrase: String, endRegisterDate: Date, endBetDate: Date, totalStakes: Int, isPublic: Bool, invited: [User], author: User, registered: [User], nameTeam1: String, nameTeam2: String) { + self.nameTeam1 = nameTeam1 + self.nameTeam2 = nameTeam2 + super.init(theme: theme, phrase: phrase, endRegisterDate: endRegisterDate, endBetDate: endBetDate, totalStakes: totalStakes, isPublic: isPublic, invited: invited, author: author, registered: registered) + } } diff --git a/Sources/Model/Sources/Model/MatchParticipation.swift b/Sources/Model/Sources/Model/MatchParticipation.swift index cd1747e..68b47ba 100644 --- a/Sources/Model/Sources/Model/MatchParticipation.swift +++ b/Sources/Model/Sources/Model/MatchParticipation.swift @@ -7,12 +7,26 @@ import Foundation -public struct MatchParticipation: Participation { - public var coinAmount: Int - public var date: Date - public var user: User - public var bet: Bet - public var PointsTeam1: Int - public var PointsTeam2: Int - +/// A subclass of Participation that represents a user's participation in a match bet. +public class MatchParticipation: Participation { + /// The points earned by the user for the first team in the match. + public var pointsTeam1: Int + + /// The points earned by the user for the second team in the match. + public var pointsTeam2: Int + + /// Custom Constructor + /// + /// - Parameters: + /// - coinAmount: The amount of coins involved in the match participation. + /// - date: The date and time when the match participation occurred. + /// - user: The user who participated in the match bet. + /// - bet: The match bet in which the user participated. + /// - pointsTeam1: The points earned by the user for the first team in the match. + /// - pointsTeam2: The points earned by the user for the second team in the match. + public init(coinAmount: Int, date: Date, user: User, bet: Bet, pointsTeam1: Int, pointsTeam2: Int) { + self.pointsTeam1 = pointsTeam1 + self.pointsTeam2 = pointsTeam2 + super.init(coinAmount: coinAmount, date: date, user: user, bet: bet) + } } diff --git a/Sources/Model/Sources/Model/Participation.swift b/Sources/Model/Sources/Model/Participation.swift index 6067ac4..6ccd9be 100644 --- a/Sources/Model/Sources/Model/Participation.swift +++ b/Sources/Model/Sources/Model/Participation.swift @@ -7,10 +7,31 @@ import Foundation -public protocol Participation { - var coinAmount: Int { get set } - var date: Date { get set } - var user: User { get set } - var bet: Bet { get set } +/// A class representing a user's participation in a bet, including the amount of coins, date, user, and the associated bet. +public class Participation: ObservableObject { + /// The amount of coins involved in the participation. + var coinAmount: Int + /// The date and time when the participation occurred. + var date: Date + + /// The user who participated in the bet. + var user: User + + /// The bet in which the user participated. + var bet: Bet + + /// Custom Constructor + /// + /// - Parameters: + /// - coinAmount: The amount of coins involved in the participation. + /// - date: The date and time when the participation occurred. + /// - user: The user who participated in the bet. + /// - bet: The bet in which the user participated. + init(coinAmount: Int, date: Date, user: User, bet: Bet) { + self.coinAmount = coinAmount + self.date = date + self.user = user + self.bet = bet + } } diff --git a/Sources/StubLib/Package.swift b/Sources/StubLib/Package.swift index 75b012d..813f113 100644 --- a/Sources/StubLib/Package.swift +++ b/Sources/StubLib/Package.swift @@ -5,6 +5,9 @@ import PackageDescription let package = Package( name: "StubLib", + platforms: [ + .iOS(.v13) + ], products: [ // Products define the executables and libraries a package produces, and make them visible to other packages. .library( From 221464696baf3a675f1e86dc39a768e186966529 Mon Sep 17 00:00:00 2001 From: "emre.kartal" Date: Thu, 11 Jan 2024 13:48:15 +0100 Subject: [PATCH 3/3] Adding a factory bet for the API and navigation to the home page once the bet is created :factory: --- .../ViewModels/CreationBetViewModel.swift | 3 + .../AllIn/Views/CreationBetView.swift | 5 +- .../Sources/Api/Factory/FactoryApiBet.swift | 65 +++++++++++++++++++ Sources/Api/Sources/Api/UserApiManager.swift | 14 +--- Sources/Model/Sources/Model/Bet.swift | 18 ++--- .../Sources/Model/Factory/FactoryBet.swift | 4 +- 6 files changed, 85 insertions(+), 24 deletions(-) create mode 100644 Sources/Api/Sources/Api/Factory/FactoryApiBet.swift diff --git a/Sources/AllInApp/AllIn/ViewModels/CreationBetViewModel.swift b/Sources/AllInApp/AllIn/ViewModels/CreationBetViewModel.swift index ea137b7..4adb521 100644 --- a/Sources/AllInApp/AllIn/ViewModels/CreationBetViewModel.swift +++ b/Sources/AllInApp/AllIn/ViewModels/CreationBetViewModel.swift @@ -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 { diff --git a/Sources/AllInApp/AllIn/Views/CreationBetView.swift b/Sources/AllInApp/AllIn/Views/CreationBetView.swift index 1634241..4659cee 100644 --- a/Sources/AllInApp/AllIn/Views/CreationBetView.swift +++ b/Sources/AllInApp/AllIn/Views/CreationBetView.swift @@ -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) { diff --git a/Sources/Api/Sources/Api/Factory/FactoryApiBet.swift b/Sources/Api/Sources/Api/Factory/FactoryApiBet.swift new file mode 100644 index 0000000..223f685 --- /dev/null +++ b/Sources/Api/Sources/Api/Factory/FactoryApiBet.swift @@ -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: []) + } + + } +} diff --git a/Sources/Api/Sources/Api/UserApiManager.swift b/Sources/Api/Sources/Api/UserApiManager.swift index ebb994d..6330e6e 100644 --- a/Sources/Api/Sources/Api/UserApiManager.swift +++ b/Sources/Api/Sources/Api/UserApiManager.swift @@ -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 diff --git a/Sources/Model/Sources/Model/Bet.swift b/Sources/Model/Sources/Model/Bet.swift index 3721e4a..919ebbd 100644 --- a/Sources/Model/Sources/Model/Bet.swift +++ b/Sources/Model/Sources/Model/Bet.swift @@ -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 /// diff --git a/Sources/Model/Sources/Model/Factory/FactoryBet.swift b/Sources/Model/Sources/Model/Factory/FactoryBet.swift index bb26240..c692035 100644 --- a/Sources/Model/Sources/Model/Factory/FactoryBet.swift +++ b/Sources/Model/Sources/Model/Factory/FactoryBet.swift @@ -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 }