From 27bd45c071f2b6b4bdba06de3a62127ddcfa599f Mon Sep 17 00:00:00 2001 From: "emre.kartal" Date: Wed, 10 Jan 2024 12:52:18 +0100 Subject: [PATCH] Add Api package --- .../contents.xcworkspacedata | 3 + Sources/AllInApp/AllIn/AllInApp.swift | 3 +- .../AllInApp/AllIn/Services/AuthService.swift | 2 +- .../ViewModels/CreationBetViewModel.swift | 4 +- .../AllIn/Views/CreationBetView.swift | 140 +++++++++++------- .../AllInApp.xcodeproj/project.pbxproj | 7 + Sources/Api/Package.swift | 25 ++++ Sources/Api/README.md | 3 + Sources/Api/Sources/Api/UserApiManager.swift | 52 +++++++ Sources/Model/Package.swift | 3 - .../Sources/Model/Factory/FactoryBet.swift | 14 ++ Sources/Model/Sources/Model/Manager.swift | 4 + Sources/StubLib/Package.swift | 3 - Sources/StubLib/Sources/StubLib/Stub.swift | 2 +- Sources/ViewModel/Package.swift | 3 - .../Sources/ViewModel/ManagerVM.swift | 4 +- 16 files changed, 201 insertions(+), 71 deletions(-) create mode 100644 Sources/Api/Package.swift create mode 100644 Sources/Api/README.md create mode 100644 Sources/Api/Sources/Api/UserApiManager.swift create mode 100644 Sources/Model/Sources/Model/Factory/FactoryBet.swift diff --git a/Sources/AllIn.xcworkspace/contents.xcworkspacedata b/Sources/AllIn.xcworkspace/contents.xcworkspacedata index d92865d..50d956e 100644 --- a/Sources/AllIn.xcworkspace/contents.xcworkspacedata +++ b/Sources/AllIn.xcworkspace/contents.xcworkspacedata @@ -1,6 +1,9 @@ + + diff --git a/Sources/AllInApp/AllIn/AllInApp.swift b/Sources/AllInApp/AllIn/AllInApp.swift index 910f49f..fdd165e 100644 --- a/Sources/AllInApp/AllIn/AllInApp.swift +++ b/Sources/AllInApp/AllIn/AllInApp.swift @@ -10,6 +10,7 @@ import DependencyInjection import Model import ViewModel import StubLib +import Api @main struct AllInApp: App { @@ -19,7 +20,7 @@ struct AllInApp: App { init() { DI.addSingleton(IAuthService.self, AuthService()) - DI.addSingleton(ManagerVM.self, ManagerVM(withModel: Manager(withBetDataManager: BetStubManager(), withUserDataManager: UserStubManager(username: "Imri")))) + 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 7f22be8..b318d98 100644 --- a/Sources/AllInApp/AllIn/Services/AuthService.swift +++ b/Sources/AllInApp/AllIn/Services/AuthService.swift @@ -62,7 +62,7 @@ class AuthService: IAuthService { URLSession.shared.uploadTask(with: request, from: jsonData) { data, response, error in print ("ALLIN : Process REGISTER") if let httpResponse = response as? HTTPURLResponse { - if httpResponse.statusCode == 200 { + if httpResponse.statusCode == 201 { if let data = data, let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any], let token = json["token"] as? String { diff --git a/Sources/AllInApp/AllIn/ViewModels/CreationBetViewModel.swift b/Sources/AllInApp/AllIn/ViewModels/CreationBetViewModel.swift index f844fa0..ea137b7 100644 --- a/Sources/AllInApp/AllIn/ViewModels/CreationBetViewModel.swift +++ b/Sources/AllInApp/AllIn/ViewModels/CreationBetViewModel.swift @@ -32,7 +32,9 @@ class CreationBetViewModel: ObservableObject { resetAllFieldErrors() - manager.addBet() + if let user = AppStateContainer.shared.user { + manager.addBet(theme: theme, description: description, endRegister: endRegisterDate, endBet: endBetDate, isPublic: isPublic, creator: user) + } } 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 68ae1e0..6aa4975 100644 --- a/Sources/AllInApp/AllIn/Views/CreationBetView.swift +++ b/Sources/AllInApp/AllIn/Views/CreationBetView.swift @@ -97,23 +97,29 @@ struct CreationBetView: View { .frame(width: 340) .padding(.leading, 10) - TextField("", text: $viewModel.theme, prompt: Text("Études, sport, soirée...") - .foregroundColor(AllInColors.lightGrey300Color) - .font(.system(size: 14)) - .fontWeight(.light)) - .padding() - .background( - RoundedRectangle(cornerRadius: 9) - .fill(AllInColors.componentBackgroundColor) - .frame(height: 40) - ) - .frame(width: 350, height: 40) - .foregroundColor(.black) - .overlay( - RoundedRectangle(cornerRadius: 10, style: .continuous) - .stroke(AllInColors.delimiterGrey, lineWidth: 1) - ) - .padding(.bottom, 5) + VStack { + if let themeError = $viewModel.themeFieldError.wrappedValue { + Text(themeError) + .textStyle(weight: .bold, color: .red, size: 10) + } + TextField("", text: $viewModel.theme, prompt: Text("Études, sport, soirée...") + .foregroundColor(AllInColors.lightGrey300Color) + .font(.system(size: 14)) + .fontWeight(.light)) + .padding() + .background( + RoundedRectangle(cornerRadius: 9) + .fill(AllInColors.componentBackgroundColor) + .frame(height: 40) + ) + .frame(width: 350, height: 40) + .foregroundColor(.black) + .overlay( + RoundedRectangle(cornerRadius: 10, style: .continuous) + .stroke(AllInColors.delimiterGrey, lineWidth: 1) + ) + .padding(.bottom, 5) + } } HStack(spacing: 5) { @@ -132,24 +138,30 @@ struct CreationBetView: View { .frame(width: 340) .padding(.leading, 10) - TextField("", text: $viewModel.description, prompt: Text("David sera absent Lundi matin en cours ?") - .foregroundColor(AllInColors.lightGrey300Color) - .font(.system(size: 14)) - .fontWeight(.light), axis: .vertical) - .lineLimit(4, reservesSpace: true) - .padding() - .background( - RoundedRectangle(cornerRadius: 9) - .fill(AllInColors.componentBackgroundColor) - .frame(height: 110) - ) - .frame(width: 350, height: 110) - .foregroundColor(.black) - .overlay( - RoundedRectangle(cornerRadius: 10, style: .continuous) - .stroke(AllInColors.delimiterGrey, lineWidth: 1) - ) - .padding(.bottom, 30) + VStack { + if let descriptionError = $viewModel.descriptionFieldError.wrappedValue { + Text(descriptionError) + .textStyle(weight: .bold, color: .red, size: 10) + } + TextField("", text: $viewModel.description, prompt: Text("David sera absent Lundi matin en cours ?") + .foregroundColor(AllInColors.lightGrey300Color) + .font(.system(size: 14)) + .fontWeight(.light), axis: .vertical) + .lineLimit(4, reservesSpace: true) + .padding() + .background( + RoundedRectangle(cornerRadius: 9) + .fill(AllInColors.componentBackgroundColor) + .frame(height: 110) + ) + .frame(width: 350, height: 110) + .foregroundColor(.black) + .overlay( + RoundedRectangle(cornerRadius: 10, style: .continuous) + .stroke(AllInColors.delimiterGrey, lineWidth: 1) + ) + .padding(.bottom, 30) + } HStack(spacing: 5) { Text("Date de fin des inscriptions") @@ -166,19 +178,25 @@ struct CreationBetView: View { .frame(width: 340) .padding(.leading, 10) - HStack(spacing: 5) { - DatePicker( - "", - selection: $viewModel.endRegisterDate, - in: dateRange, - displayedComponents: [.date, .hourAndMinute] - ) - .accentColor(AllInColors.lightPurpleColor) - .labelsHidden() - .padding(.bottom, 10) - Spacer() + VStack { + if let endRegisterError = $viewModel.endRegisterDateFieldError.wrappedValue { + Text(endRegisterError) + .textStyle(weight: .bold, color: .red, size: 10) + } + HStack(spacing: 5) { + DatePicker( + "", + selection: $viewModel.endRegisterDate, + in: dateRange, + displayedComponents: [.date, .hourAndMinute] + ) + .accentColor(AllInColors.lightPurpleColor) + .labelsHidden() + .padding(.bottom, 10) + Spacer() + } + .frame(width: 340) } - .frame(width: 340) VStack(alignment: .leading, spacing: 5) { VStack() { @@ -197,15 +215,25 @@ struct CreationBetView: View { } .padding(.leading, 10) } - DatePicker( - "", - selection: $viewModel.endBetDate, - in: dateRange, - displayedComponents: [.date, .hourAndMinute] - ) - .accentColor(AllInColors.lightPurpleColor) - .labelsHidden() - .padding(.bottom, 40) + + VStack { + if let endBetError = $viewModel.endBetDateFieldError.wrappedValue { + Text(endBetError) + .textStyle(weight: .bold, color: .red, size: 10) + } + HStack(spacing: 5) { + DatePicker( + "", + selection: $viewModel.endBetDate, + in: dateRange, + displayedComponents: [.date, .hourAndMinute] + ) + .accentColor(AllInColors.lightPurpleColor) + .labelsHidden() + .padding(.bottom, 40) + Spacer() + } + } } .frame(width: 340) diff --git a/Sources/AllInApp/AllInApp.xcodeproj/project.pbxproj b/Sources/AllInApp/AllInApp.xcodeproj/project.pbxproj index c72f194..8e23970 100644 --- a/Sources/AllInApp/AllInApp.xcodeproj/project.pbxproj +++ b/Sources/AllInApp/AllInApp.xcodeproj/project.pbxproj @@ -60,6 +60,7 @@ ECB7BC6A2B2F410A002A6654 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECB7BC692B2F410A002A6654 /* AppDelegate.swift */; }; ECB7BC6C2B2F43EE002A6654 /* AppState.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECB7BC6B2B2F43EE002A6654 /* AppState.swift */; }; ECB7BC702B336E28002A6654 /* RegisterViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECB7BC6F2B336E28002A6654 /* RegisterViewModel.swift */; }; + ECCD244A2B4DE8010071FA9E /* Api in Frameworks */ = {isa = PBXBuildFile; productRef = ECCD24492B4DE8010071FA9E /* Api */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -156,6 +157,7 @@ files = ( EC8B9CCE2B42C9D3002806F3 /* StubLib in Frameworks */, ECB357352B3E13A400045D41 /* Model in Frameworks */, + ECCD244A2B4DE8010071FA9E /* Api in Frameworks */, ECB357312B3CA69300045D41 /* DependencyInjection.framework in Frameworks */, ECB26A152B406B4800FE06B3 /* ViewModel in Frameworks */, ); @@ -354,6 +356,7 @@ ECB357342B3E13A400045D41 /* Model */, ECB26A142B406B4800FE06B3 /* ViewModel */, EC8B9CCD2B42C9D3002806F3 /* StubLib */, + ECCD24492B4DE8010071FA9E /* Api */, ); productName = AllIn; productReference = EC6B96982B24B4CC00FC1C58 /* AllIn.app */; @@ -852,6 +855,10 @@ isa = XCSwiftPackageProductDependency; productName = Model; }; + ECCD24492B4DE8010071FA9E /* Api */ = { + isa = XCSwiftPackageProductDependency; + productName = Api; + }; /* End XCSwiftPackageProductDependency section */ }; rootObject = EC6B96902B24B4CC00FC1C58 /* Project object */; diff --git a/Sources/Api/Package.swift b/Sources/Api/Package.swift new file mode 100644 index 0000000..5f646b4 --- /dev/null +++ b/Sources/Api/Package.swift @@ -0,0 +1,25 @@ +// swift-tools-version: 5.8 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "Api", + products: [ + // Products define the executables and libraries a package produces, and make them visible to other packages. + .library( + name: "Api", + targets: ["Api"]), + ], + dependencies: [ + // Dependencies declare other packages that this package depends on. + // .package(url: /* package url */, from: "1.0.0"), + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages this package depends on. + .target( + name: "Api", + dependencies: []), + ] +) diff --git a/Sources/Api/README.md b/Sources/Api/README.md new file mode 100644 index 0000000..c22c038 --- /dev/null +++ b/Sources/Api/README.md @@ -0,0 +1,3 @@ +# Api + +A description of this package. diff --git a/Sources/Api/Sources/Api/UserApiManager.swift b/Sources/Api/Sources/Api/UserApiManager.swift new file mode 100644 index 0000000..9624e17 --- /dev/null +++ b/Sources/Api/Sources/Api/UserApiManager.swift @@ -0,0 +1,52 @@ +// +// UserApiManager.swift +// +// +// Created by Emre on 31/12/2023. +// + +import Foundation +import Model + +let allInApi = "https://codefirst.iut.uca.fr/containers/AllDev-api" + +public struct UserApiManager: UserDataManager { + + public init() { + } + + public func getBets(withIndex index: Int, withCount count: Int) -> [Bet] { + fatalError("Not implemented yet") + } + + public func addBet(bet: Bet) { + + let url = URL(string: allInApi + "bets/add")! + var request = URLRequest(url: url) + request.httpMethod = "POST" + request.setValue("application/json", forHTTPHeaderField: "Content-Type") + + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" + + let json = [ + "theme": bet.theme, + "sentenceBet": bet.phrase, + "endRegistration": dateFormatter.string(from: bet.endRegisterDate), + "endBet": dateFormatter.string(from: bet.endBetDate), + "isPrivate": String(bet.isPublic), + "response": "", + "createdBy": "" + ] + + if let jsonData = try? JSONSerialization.data(withJSONObject: json, options: []){ + URLSession.shared.uploadTask(with: request, from: jsonData) { data, response, error in + + }.resume() + } + } + + public func getFriends() -> [User] { + fatalError("Not implemented yet") + } +} diff --git a/Sources/Model/Package.swift b/Sources/Model/Package.swift index 3495d60..5e7194a 100644 --- a/Sources/Model/Package.swift +++ b/Sources/Model/Package.swift @@ -21,8 +21,5 @@ let package = Package( .target( name: "Model", dependencies: []), - .testTarget( - name: "ModelTests", - dependencies: ["Model"]), ] ) diff --git a/Sources/Model/Sources/Model/Factory/FactoryBet.swift b/Sources/Model/Sources/Model/Factory/FactoryBet.swift new file mode 100644 index 0000000..bb26240 --- /dev/null +++ b/Sources/Model/Sources/Model/Factory/FactoryBet.swift @@ -0,0 +1,14 @@ +// +// FactoryBet.swift +// +// +// Created by Emre on 09/01/2024. +// + +import Foundation + +public protocol FactoryBet { + func toResponse() + func toModel() -> Bet + func toModel(theme: String, description: String, endRegister: Date, endBet: Date, isPublic: Bool, creator: User, type: Int) -> Bet +} diff --git a/Sources/Model/Sources/Model/Manager.swift b/Sources/Model/Sources/Model/Manager.swift index 2463e97..eba7463 100644 --- a/Sources/Model/Sources/Model/Manager.swift +++ b/Sources/Model/Sources/Model/Manager.swift @@ -16,4 +16,8 @@ public struct Manager { self.userDataManager = userDataManager } + public func addBet(bet: Bet) { + userDataManager.addBet(bet: bet) + } + } diff --git a/Sources/StubLib/Package.swift b/Sources/StubLib/Package.swift index a8abfb8..75b012d 100644 --- a/Sources/StubLib/Package.swift +++ b/Sources/StubLib/Package.swift @@ -22,8 +22,5 @@ let package = Package( .target( name: "StubLib", dependencies: ["Model"]), - .testTarget( - name: "StubLibTests", - dependencies: ["StubLib"]), ] ) diff --git a/Sources/StubLib/Sources/StubLib/Stub.swift b/Sources/StubLib/Sources/StubLib/Stub.swift index c5d7e04..fe9e225 100644 --- a/Sources/StubLib/Sources/StubLib/Stub.swift +++ b/Sources/StubLib/Sources/StubLib/Stub.swift @@ -27,7 +27,7 @@ struct Stub { users.append(user2) user1.addFriend(user: user2) - var user3 = User(username: "Arthur", email: "arthur.valin@etu.uca.fr", nbCoins: 30, friends: [user2]) + let user3 = User(username: "Arthur", email: "arthur.valin@etu.uca.fr", nbCoins: 30, friends: [user2]) users.append(user3) user2.addFriend(user: user3) diff --git a/Sources/ViewModel/Package.swift b/Sources/ViewModel/Package.swift index e1b79e7..67fd2cf 100644 --- a/Sources/ViewModel/Package.swift +++ b/Sources/ViewModel/Package.swift @@ -25,8 +25,5 @@ let package = Package( .target( name: "ViewModel", dependencies: ["Model"]), - .testTarget( - name: "ViewModelTests", - dependencies: ["ViewModel"]), ] ) diff --git a/Sources/ViewModel/Sources/ViewModel/ManagerVM.swift b/Sources/ViewModel/Sources/ViewModel/ManagerVM.swift index 91c1245..2bcb8b0 100644 --- a/Sources/ViewModel/Sources/ViewModel/ManagerVM.swift +++ b/Sources/ViewModel/Sources/ViewModel/ManagerVM.swift @@ -19,7 +19,7 @@ public class ManagerVM: ObservableObject { } - public func addBet() { - + public func addBet(theme: String, description: String, endRegister: Date, endBet: Date, isPublic: Bool, creator: User) { + model.addBet(bet: BinaryBet(theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: [])) } }