Fix Error and add route for add new users to a private bet
continuous-integration/drone/push Build is passing Details

master
Lucas EVARD 6 months ago
parent e8922f95b3
commit c9225557df

@ -13,7 +13,7 @@ interface BetDataSource {
fun removeBet(id: String): Boolean
fun updateBet(data: UpdatedBetData): Boolean
fun updateBetStatuses(date: ZonedDateTime)
fun getToConfirm(username: String): List<BetDetail>
fun getToConfirm(user: UserDTO): List<BetDetail>
fun confirmBet(betId: String, result: String)
fun getWonNotifications(username: String): List<BetResultDetail>
fun getHistory(username: String): List<BetResultDetail>
@ -22,4 +22,5 @@ interface BetDataSource {
fun updatePopularityScore(betId: String)
fun addPrivateBet(bet: Bet)
fun isInvited(betid: String, userId: String): Boolean
fun addUserInPrivatebet(updatedPrivateBet: UpdatedPrivateBet)
}

@ -113,9 +113,9 @@ class MockBetDataSource(private val mockData: MockDataSource.MockData) : BetData
}
}
override fun getToConfirm(username: String): List<BetDetail> =
bets.filter { it.createdBy == username && it.status == CLOSING }
.map { it.toBetDetail(username) }
override fun getToConfirm(user: UserDTO): List<BetDetail> =
bets.filter { it.createdBy == user.id && it.status == CLOSING }
.map { it.toBetDetail(user.username) }
override fun confirmBet(betId: String, result: String) {
results.add(
@ -235,11 +235,20 @@ class MockBetDataSource(private val mockData: MockDataSource.MockData) : BetData
}
override fun addPrivateBet(bet: Bet) {
TODO()
addBet(bet)
bet.userInvited?.forEach {
mockData.privatebets.add(InvitationBet(bet.id, it))
}
}
override fun isInvited(betid: String, userId: String): Boolean {
TODO("Not yet implemented")
override fun isInvited(betid: String, userId: String) =
mockData.privatebets.filter { (it.betid == betid) and (it.userId == userId) }.isNotEmpty()
override fun addUserInPrivatebet(updatedPrivateBet: UpdatedPrivateBet) {
updatedPrivateBet.usersInvited?.forEach {
mockData.privatebets.add(InvitationBet(updatedPrivateBet.betid, it))
}
}
}

@ -20,6 +20,7 @@ class MockDataSource : AllInDataSource() {
val lastGifts by lazy { mutableMapOf<String, ZonedDateTime>() }
val participations by lazy { mutableListOf<Participation>() }
val friends by lazy { mutableListOf<Friend>() }
val privatebets by lazy { mutableListOf<InvitationBet>() }
}
private val mockData by lazy { MockData() }

@ -65,12 +65,12 @@ class PostgresBetDataSource(private val database: Database) : BetDataSource {
.map { it.toBet(database) }
}
override fun getToConfirm(username: String): List<BetDetail> {
override fun getToConfirm(user: UserDTO): List<BetDetail> {
return database.bets
.filter {
(it.createdBy eq username) and (BetsEntity.status eq BetStatus.CLOSING)
(it.createdBy eq user.id) and (BetsEntity.status eq BetStatus.CLOSING)
}
.map { it.toBetDetail(database, username) }
.map { it.toBetDetail(database, user.username) }
}
override fun confirmBet(betId: String, result: String) {
@ -282,7 +282,16 @@ class PostgresBetDataSource(private val database: Database) : BetDataSource {
}
}
override fun isInvited(betid: String, userId: String): Boolean {
return database.privatebets.filter { (it.betid eq betid) and (it.userId eq userId) }.isNotEmpty()
override fun isInvited(betid: String, userId: String) =
database.privatebets.filter { (it.betid eq betid) and (it.userId eq userId) }.isNotEmpty()
override fun addUserInPrivatebet(updatedPrivateBet: UpdatedPrivateBet) {
updatedPrivateBet.usersInvited?.forEach {
database.privatebets.add(PrivateBetEntity {
betId = updatedPrivateBet.betid
userId = it
})
}
}
}

@ -18,4 +18,5 @@ object ApiMessage {
const val FRIENDS_DOESNT_EXISTS = "User doesn't exists in your Friends List."
const val FRIENDS_REQUEST_HIMSELF = "The receiver can't be the sender."
const val FILE_NOT_FOUND = "File not found."
const val USER_DOESNT_HAVE_PERMISSION = "This user can't delete or modify this."
}

@ -32,4 +32,16 @@ data class UpdatedBetData(
@Serializable(ZonedDateTimeSerializer::class) val endBet: ZonedDateTime,
val isPrivate: Boolean,
val response: List<String>
)
@Serializable
data class InvitationBet(
val betid: String,
val userId: String
)
@Serializable
data class UpdatedPrivateBet(
val betid: String,
val usersInvited: List<String>? = null
)

@ -232,7 +232,7 @@ fun Application.betRouter() {
logManager.log("Routing", "GET /bets/toConfirm")
hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { user, _ ->
val response = betDataSource.getToConfirm(user.username)
val response = betDataSource.getToConfirm(user)
logManager.log("Routing", "ACCEPTED /bets/toConfirm\t${response}")
call.respond(HttpStatusCode.Accepted, response)
}
@ -346,7 +346,7 @@ fun Application.betRouter() {
val betId = call.parameters["id"] ?: ""
val result = call.receive<String>()
if (betDataSource.getBetById(betId)?.createdBy == user.username) {
if (betDataSource.getBetById(betId)?.createdBy == user.id) {
betDataSource.confirmBet(betId, result)
logManager.log("Routing", "ACCEPTED /bets/confirm/{id}")
call.respond(HttpStatusCode.OK)
@ -375,17 +375,48 @@ fun Application.betRouter() {
}
}
}) {
logManager.log("Routing","POST /bets/users")
logManager.log("Routing", "POST /bets/users")
hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { user, _ ->
val id = call.receive<Map<String, String>>()["id"] ?: ""
val participations = participationDataSource.getParticipationFromBetId(id)
val users = participations.map { userDataSource.getUserByUsername(it.username).first }.toSet().take(4).toList()
val users =
participations.map { userDataSource.getUserByUsername(it.username).first }.toSet().take(4)
.toList()
call.respond(users)
}
}
}
post("/bets/pvbet/update", {
description = "Add new users to a private bet"
request {
headerParameter<JWTPrincipal>("JWT token of the logged user")
body<UpdatedPrivateBet> {
description = "Bet id and list of new users"
}
}
response {
HttpStatusCode.OK to {
description = "The final answer has been set"
}
HttpStatusCode.Unauthorized to {
description = "The user is not the creator of the bet"
}
}
}) {
logManager.log("Routing", "POST /bets/pvbet/update")
hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { user, _ ->
val updateRequest = call.receive<UpdatedPrivateBet>()
val bet = betDataSource.getBetById(updateRequest.betid)
if (user.username != bet?.createdBy) {
call.respond(HttpStatusCode.Forbidden, ApiMessage.USER_DOESNT_HAVE_PERMISSION)
}
betDataSource.addUserInPrivatebet(updateRequest)
call.respond(HttpStatusCode.Accepted, updateRequest)
}
}
}
}
}
}
}
Loading…
Cancel
Save