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() } }