Update Factory and add a new Method toBetDetail, also improve the Model 🎨

pull/16/head
Emre KARTAL 1 year ago
parent d24a1cc244
commit 6fb1146ab6

@ -23,9 +23,9 @@
"color-space" : "display-p3",
"components" : {
"alpha" : "1.000",
"blue" : "0xCF",
"green" : "0xCF",
"red" : "0xCF"
"blue" : "0x45",
"green" : "0x45",
"red" : "0x45"
}
},
"idiom" : "universal"

@ -74,8 +74,8 @@ struct BetCard_Previews: PreviewProvider {
phrase: "Le gagnant de la finale sera l'équipe avec le plus de tirs au but.",
endRegisterDate: Date().addingTimeInterval(86400),
endBetDate: Date().addingTimeInterval(172800),
totalStakes: 100,
isPublic: true,
status: .FINISHED,
invited: [],
author: User(username: "Imri", email: "emre.kartal@etu.uca.fr", nbCoins: 75, friends: []),
registered: []))

@ -150,7 +150,7 @@ class AuthService: IAuthService {
}
private func initManagerVM(token: String) {
DependencyInjection.shared.addSingleton(Manager.self, Manager(withBetDataManager: BetStubManager(), withUserDataManager: UserApiManager(withUserToken: token)))
DependencyInjection.shared.addSingleton(Manager.self, Manager(withBetDataManager: BetApiManager(withUserToken: token), withUserDataManager: UserApiManager(withUserToken: token)))
}
}

@ -118,7 +118,7 @@ struct DetailsView: View {
ResultBanner()
}
VStack(alignment: .leading, spacing: 5) {
BetLineLoading(participations: viewModel.betDetail!.participations).padding(.vertical,15)
BetLineLoading(participations: viewModel.betDetail?.participations ?? []).padding(.vertical,15)
Text("Liste des participants")
.font(.system(size: 18))
.foregroundStyle(AllInColors.grey100Color)

@ -10,7 +10,11 @@ import Model
public struct BetApiManager: BetDataManager {
public init() { }
public let token: String
public init(withUserToken token: String) {
self.token = token
}
public func getBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) {
let url = URL(string: allInApi + "bets/gets")!
@ -27,9 +31,8 @@ public struct BetApiManager: BetDataManager {
do {
if let httpResponse = response as? HTTPURLResponse, let jsonArray = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
for json in jsonArray {
if let bet = FactoryApiBet().toModel(from: json) {
if let bet = FactoryApiBet().toBet(from: json) {
bets.append(bet)
print(bet.theme)
}
}
print(httpResponse.statusCode)
@ -47,7 +50,30 @@ public struct BetApiManager: BetDataManager {
}
public func getBet(withId id: String, completion: @escaping (BetDetail) -> Void) {
let url = URL(string: allInApi + "betdetail/get/" + id)!
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
print ("ALLIN : get bet with id :" + id)
do {
if let httpResponse = response as? HTTPURLResponse, let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
if let betDetail = FactoryApiBet().toBetDetail(from: json) {
completion(betDetail)
}
print(httpResponse.statusCode)
}
} catch {
print("Error parsing JSON: \(error)")
}
}
}.resume()
}
}

@ -10,14 +10,19 @@ import Model
public class FactoryApiBet: FactoryBet {
func formatZonedDateTime(dateTime: Date) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
formatter.timeZone = TimeZone.current
return formatter.string(from: dateTime)
}
public func toResponse(bet: Bet) -> [String: Any] {
let json: [String: Any] = [
"id": "1",
"theme": bet.theme,
"sentenceBet": bet.phrase,
"endRegistration": bet.endRegisterDate.timeIntervalSince1970,
"endBet": bet.endBetDate.timeIntervalSince1970,
"endRegistration": formatZonedDateTime(dateTime: bet.endRegisterDate),
"endBet": formatZonedDateTime(dateTime: bet.endBetDate),
"isPrivate": String(bet.isPublic),
"response": [],
]
@ -25,7 +30,15 @@ public class FactoryApiBet: FactoryBet {
return json
}
public func toModel(from json: [String: Any]) -> Bet? {
public func fromJsonDateString(_ dateString: String) -> Date? {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let date = formatter.date(from: dateString)
return date
}
public func toBet(from json: [String: Any]) -> Bet? {
guard let id = json["id"] as? String,
let theme = json["theme"] as? String,
let phrase = json["sentenceBet"] as? String,
@ -36,28 +49,33 @@ public class FactoryApiBet: FactoryBet {
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 {
guard let endRegisterDate = fromJsonDateString(endRegisterDateString),
let endBetDate = fromJsonDateString(endBetDateString) else {
return nil
}
return toModel(id: id, theme: theme, description: phrase, endRegister: endRegisterDate, endBet: endBetDate, isPublic: isPublic, creator: User(username: createdBy, email: createdBy, nbCoins: 0, friends: []), type: 0)
return toBet(id: id, theme: theme, description: phrase, endRegister: endRegisterDate, endBet: endBetDate, isPublic: isPublic, status: .FINISHED, creator: User(username: createdBy, email: createdBy, nbCoins: 0, friends: []), type: 0)
}
public func toModel(id: String, theme: String, description: String, endRegister: Date, endBet: Date, isPublic: Bool, creator: User, type: Int) -> Bet {
public func toBet(id: String, theme: String, description: String, endRegister: Date, endBet: Date, isPublic: Bool, status: BetStatus, creator: User, type: Int) -> Bet {
switch type {
case 0:
return BinaryBet(id: id, theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: [])
return BinaryBet(id: id, theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, isPublic: isPublic, status: status, invited: [], author: creator, registered: [])
case 1:
return MatchBet(id: id, theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: [], nameTeam1: "", nameTeam2: "")
return MatchBet(id: id, theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, isPublic: isPublic, status: status, invited: [], author: creator, registered: [], nameTeam1: "", nameTeam2: "")
case 2:
return CustomBet(id: id, theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: [])
return CustomBet(id: id, theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, isPublic: isPublic, status: status, invited: [], author: creator, registered: [])
default:
return BinaryBet(id: id, theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: [])
return BinaryBet(id: id, theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, isPublic: isPublic, status: status, invited: [], author: creator, registered: [])
}
}
public func toBetDetail(from json: [String: Any]) -> BetDetail? {
guard let betJson = json["bet"] as? [String: Any],
let bet = self.toBet(from: betJson) else {
return nil
}
return BetDetail(bet: bet, answers: [], participations: [])
}
}

@ -24,15 +24,14 @@ 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"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
let json = FactoryApiBet().toResponse(bet: bet)
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
print ("ALLIN : Add BET")

@ -1,5 +1,5 @@
//
// BetAnswerDetail.swift
// AnswerDetail.swift
//
//
// Created by Emre on 19/01/2024.
@ -8,7 +8,7 @@
import Foundation
/// A class representing detailed information about a specific answer option for a bet.
public class BetAnswerDetail: ObservableObject {
public class AnswerDetail: ObservableObject {
/// The response or outcome associated with this answer.
public private(set) var response: String

@ -25,12 +25,11 @@ public class Bet: ObservableObject, Identifiable {
/// The deadline for the actual betting to take place.
public private(set) var endBetDate: Date
/// The total stakes or amount involved in the bet.
public private(set) var totalStakes: Int
/// Indicates whether the bet is public or private.
public private(set) var isPublic: Bool
public private(set) var status: BetStatus
/// List of users who are invited to participate in the bet.
public private(set) var invited: [User]
@ -39,6 +38,7 @@ public class Bet: ObservableObject, Identifiable {
/// List of users who have registered for the bet.
public private(set) var registered: [User]
/// Custom Constructor
///
@ -48,19 +48,18 @@ public class Bet: ObservableObject, Identifiable {
/// - 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(id: String, theme: String, phrase: String, endRegisterDate: Date, endBetDate: Date, totalStakes: Int, isPublic: Bool, invited: [User], author: User, registered: [User]) {
public init(id: String, theme: String, phrase: String, endRegisterDate: Date, endBetDate: Date, isPublic: Bool, status: BetStatus, invited: [User], author: User, registered: [User]) {
self.id = id
self.theme = theme
self.phrase = phrase
self.endRegisterDate = endRegisterDate
self.endBetDate = endBetDate
self.totalStakes = totalStakes
self.isPublic = isPublic
self.status = status
self.invited = invited
self.author = author
self.registered = registered
@ -73,19 +72,18 @@ public class Bet: ObservableObject, Identifiable {
/// - 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]) {
public init(theme: String, phrase: String, endRegisterDate: Date, endBetDate: Date, isPublic: Bool, status: BetStatus, invited: [User], author: User, registered: [User]) {
self.id = UUID().uuidString
self.theme = theme
self.phrase = phrase
self.endRegisterDate = endRegisterDate
self.endBetDate = endBetDate
self.totalStakes = totalStakes
self.isPublic = isPublic
self.status = status
self.invited = invited
self.author = author
self.registered = registered

@ -14,14 +14,11 @@ public class BetDetail: ObservableObject {
public private(set) var bet: Bet
/// Details about the answers available for the bet.
public private(set) var answers: [BetAnswerDetail]
public private(set) var answers: [AnswerDetail]
/// List of user participations in the bet.
public private(set) var participations: [Participation]
/// The user's own participation in the bet.
public private(set) var userParticipation: Participation
public private(set) var finalAnswer: String?
/// Custom Constructor
@ -31,11 +28,10 @@ public class BetDetail: ObservableObject {
/// - answers: Details about the answers available for the bet.
/// - participations: List of user participations in the bet.
/// - userParticipation: The user's own participation in the bet.
public init(bet: Bet, answers: [BetAnswerDetail], participations: [Participation], userParticipation: Participation, finalAnswer: String? = nil) {
public init(bet: Bet, answers: [AnswerDetail], participations: [Participation], finalAnswer: String? = nil) {
self.bet = bet
self.answers = answers
self.participations = participations
self.userParticipation = userParticipation
self.finalAnswer = finalAnswer
}

@ -10,36 +10,4 @@ import Foundation
/// A subclass of Bet that represents a binary bet, where participants make a choice between two possible outcomes.
public class BinaryBet: Bet {
// Custom Constructor
///
/// - Parameters:
/// - id: The id for the bet.
/// - 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(id: String, theme: String, phrase: String, endRegisterDate: Date, endBetDate: Date, totalStakes: Int, isPublic: Bool, invited: [User], author: User, registered: [User]) {
super.init(id: id, theme: theme, phrase: phrase, endRegisterDate: endRegisterDate, endBetDate: endBetDate, totalStakes: totalStakes, isPublic: isPublic, invited: invited, author: author, registered: registered)
}
// Custom Constructor without Id
///
/// - 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)
}
}

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

@ -0,0 +1,12 @@
//
// File.swift
//
//
// Created by étudiant on 23/01/2024.
//
import Foundation
public enum BetStatus {
case WAITING, IN_PROGRESS, FINISHED
}

@ -17,7 +17,7 @@ public struct Manager {
}
public func addBet(theme: String, description: String, endRegister: Date, endBet: Date, isPublic: Bool, creator: User) {
userDataManager.addBet(bet: BinaryBet(theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, totalStakes: 0, isPublic: isPublic, invited: [], author: creator, registered: []))
userDataManager.addBet(bet: BinaryBet(theme: theme, phrase: description, endRegisterDate: endRegister, endBetDate: endBet, isPublic: isPublic, status: .IN_PROGRESS, invited: [], author: creator, registered: []))
}
public func getBets(withIndex index: Int, withCount count: Int, completion: @escaping ([Bet]) -> Void) {

@ -30,10 +30,10 @@ public class MatchBet: 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(id: String, theme: String, phrase: String, endRegisterDate: Date, endBetDate: Date, totalStakes: Int, isPublic: Bool, invited: [User], author: User, registered: [User], nameTeam1: String, nameTeam2: String) {
public init(id: String, theme: String, phrase: String, endRegisterDate: Date, endBetDate: Date, isPublic: Bool, status: BetStatus, invited: [User], author: User, registered: [User], nameTeam1: String, nameTeam2: String) {
self.nameTeam1 = nameTeam1
self.nameTeam2 = nameTeam2
super.init(id: id, theme: theme, phrase: phrase, endRegisterDate: endRegisterDate, endBetDate: endBetDate, totalStakes: totalStakes, isPublic: isPublic, invited: invited, author: author, registered: registered)
super.init(id: id, theme: theme, phrase: phrase, endRegisterDate: endRegisterDate, endBetDate: endBetDate, isPublic: isPublic, status: status, invited: invited, author: author, registered: registered)
}
/// Custom Constructor without Id
@ -50,9 +50,9 @@ public class MatchBet: 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) {
public init(theme: String, phrase: String, endRegisterDate: Date, endBetDate: Date, isPublic: Bool, status: BetStatus, 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)
super.init(theme: theme, phrase: phrase, endRegisterDate: endRegisterDate, endBetDate: endBetDate, isPublic: isPublic, status: status, invited: invited, author: author, registered: registered)
}
}

@ -8,7 +8,7 @@
import Foundation
/// A class representing a user's participation in a bet.
public class Participation: ObservableObject, Identifiable{
public class Participation: ObservableObject, Identifiable {
public let id: UUID
/// The amount of stake in the bet.
@ -24,7 +24,7 @@ public class Participation: ObservableObject, Identifiable{
public private(set) var user: User
/// The unique identifier of the bet.
let betId: String
public private(set) var betId: String
/// Custom Constructor
///

@ -8,10 +8,10 @@
import Foundation
public struct User {
public var username: String
public var email: String
public var nbCoins: Int
public var friends: [User]
public private(set) var username: String
public private(set) var email: String
public private(set) var nbCoins: Int
public private(set) var friends: [User]
public init(username: String, email: String, nbCoins: Int, friends: [User]) {
self.username = username

@ -37,8 +37,8 @@ struct Stub {
phrase: "Le gagnant de la finale sera l'équipe avec le plus de tirs au but.",
endRegisterDate: Date().addingTimeInterval(-86400),
endBetDate: Date().addingTimeInterval(172800),
totalStakes: 100,
isPublic: true,
status: .IN_PROGRESS,
invited: [],
author: user1,
registered: [user2]
@ -50,8 +50,8 @@ struct Stub {
phrase: "Le plat préféré du jury sera une recette végétarienne.",
endRegisterDate: Date().addingTimeInterval(172800),
endBetDate: Date().addingTimeInterval(259200),
totalStakes: 150,
isPublic: false,
status: .IN_PROGRESS,
invited: [user3],
author: user1,
registered: [user2]
@ -63,8 +63,8 @@ struct Stub {
phrase: "Le nombre total de précommandes dépassera-t-il 1 million dans la première semaine ?",
endRegisterDate: Date().addingTimeInterval(259200),
endBetDate: Date().addingTimeInterval(345600),
totalStakes: 75,
isPublic: true,
status: .FINISHED,
invited: [],
author: user1,
registered: [user2, user1, user3]
@ -76,8 +76,8 @@ struct Stub {
phrase: "Le film favori des critiques remportera-t-il le prix du meilleur film ?",
endRegisterDate: Date().addingTimeInterval(345600),
endBetDate: Date().addingTimeInterval(432000),
totalStakes: 120,
isPublic: false,
status: .FINISHED,
invited: [user1],
author: user2,
registered: [user3]
@ -85,7 +85,7 @@ struct Stub {
self.bets.append(bet4)
for bet in bets {
let betDetail = BetDetail(bet: bet, answers: [], participations: [], userParticipation: Participation(stake: 0, date: Date(), response: "", user: user1, betId: ""))
let betDetail = BetDetail(bet: bet, answers: [], participations: [])
self.betsDetail.append(betDetail)
}
@ -97,7 +97,7 @@ struct Stub {
}
public mutating func add(bet: Bet) {
let newBetDetail = BetDetail(bet: bet, answers: [], participations: [], userParticipation: Participation(stake: 0, date: Date(), response: "", user: users[1], betId: ""), finalAnswer: "test")
let newBetDetail = BetDetail(bet: bet, answers: [], participations: [], finalAnswer: "test")
self.betsDetail.append(newBetDetail)
}
}

Loading…
Cancel
Save