|
|
|
@ -10,14 +10,19 @@ import Model
|
|
|
|
|
|
|
|
|
|
public class FactoryApiBet: FactoryBet {
|
|
|
|
|
|
|
|
|
|
public func toResponse(bet: Bet) -> [String: Any] {
|
|
|
|
|
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: [])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|