create bet work

pull/8/head
Emre KARTAL 1 year ago
parent 8292c672bf
commit 438cdb63d7

@ -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 {

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

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

Loading…
Cancel
Save