Fix code smell

master
Lucas EVARD 1 year ago
parent bc57517aa1
commit d3481797f9

@ -87,30 +87,30 @@ class MockBetDataSource(private val mockData: MockDataSource.MockData) : BetData
override fun updateBetStatuses(date: ZonedDateTime) { override fun updateBetStatuses(date: ZonedDateTime) {
bets.forEachIndexed { idx, bet -> bets.forEachIndexed { idx, bet ->
if (bet.status != CANCELLED && bet.status != FINISHED) { if (bet.status != CANCELLED && bet.status != FINISHED && date >= bet.endRegistration) {
if (date >= bet.endRegistration) { bets[idx] = when {
if (date >= bet.endBet) { date >= bet.endBet && date.plusWeeks(1) >= bet.endBet -> {
if (date.plusWeeks(1) >= bet.endBet) { cancelBet(bet)
bets[idx] = bet.copy(status = CANCELLED)
participations
.filter { it.betId == bets[idx].id }
.forEach { p ->
users.replaceAll {
if (it.username == p.username) {
it.copy(nbCoins = it.nbCoins + p.stake)
} else it
} }
date >= bet.endBet -> bet.copy(status = CLOSING)
else -> bet.copy(status = WAITING)
} }
} else {
bets[idx] = bet.copy(status = CLOSING)
} }
} else {
bets[idx] = bet.copy(status = WAITING)
} }
} }
private fun cancelBet(bet: Bet): Bet {
participations
.filter { it.betId == bet.id }
.forEach { p ->
users.replaceAll { user ->
if (user.username == p.username) {
user.copy(nbCoins = user.nbCoins + p.stake)
} else user
} }
} }
return bet.copy(status = CANCELLED)
} }
override fun getToConfirm(user: UserDTO): List<BetDetail> = override fun getToConfirm(user: UserDTO): List<BetDetail> =

@ -19,4 +19,9 @@ object ApiMessage {
const val FRIENDS_REQUEST_HIMSELF = "The receiver can't be the sender." const val FRIENDS_REQUEST_HIMSELF = "The receiver can't be the sender."
const val FILE_NOT_FOUND = "File not found." const val FILE_NOT_FOUND = "File not found."
const val USER_DOESNT_HAVE_PERMISSION = "This user can't delete or modify this." const val USER_DOESNT_HAVE_PERMISSION = "This user can't delete or modify this."
const val JWT_TOKEN_INFO = "JWT token of the logged user"
const val ID_BET_INFO = "Id of the desired bet"
const val BET_NOT_FOUND_INFO = "Bet not found in the selected source"
const val NOT_CREATOR_INFO = "The user is not the creator of the bet"
const val USER_UPDATE_INFO = "New User information"
} }

@ -30,7 +30,7 @@ fun Application.betRouter() {
post("/bets/add", { post("/bets/add", {
description = "Allows a user to create a new bet" description = "Allows a user to create a new bet"
request { request {
headerParameter<JWTPrincipal>("JWT token of the logged user") headerParameter<JWTPrincipal>(ApiMessage.JWT_TOKEN_INFO)
body<Bet> { body<Bet> {
description = "Bet to add in the selected source" description = "Bet to add in the selected source"
} }
@ -73,7 +73,7 @@ fun Application.betRouter() {
post("/bets/gets", { post("/bets/gets", {
description = "Allows you to recover all bets" description = "Allows you to recover all bets"
request { request {
headerParameter<JWTPrincipal>("JWT token of the logged user") headerParameter<JWTPrincipal>(ApiMessage.JWT_TOKEN_INFO)
body<List<BetFilter>> { body<List<BetFilter>> {
description = "List of filters" description = "List of filters"
} }
@ -105,7 +105,7 @@ fun Application.betRouter() {
get("/bets/popular", { get("/bets/popular", {
description = "Allows you to recover the most popular public bets" description = "Allows you to recover the most popular public bets"
request { request {
headerParameter<JWTPrincipal>("JWT token of the logged user") headerParameter<JWTPrincipal>(ApiMessage.JWT_TOKEN_INFO)
} }
response { response {
HttpStatusCode.Accepted to { HttpStatusCode.Accepted to {
@ -134,7 +134,7 @@ fun Application.betRouter() {
get("/bets/get/{id}", { get("/bets/get/{id}", {
description = "Retrieves a specific bet" description = "Retrieves a specific bet"
request { request {
pathParameter<UUID>("Id of the desired bet") pathParameter<String>(ApiMessage.ID_BET_INFO)
} }
response { response {
HttpStatusCode.Accepted to { HttpStatusCode.Accepted to {
@ -144,7 +144,7 @@ fun Application.betRouter() {
} }
} }
HttpStatusCode.NotFound to { HttpStatusCode.NotFound to {
description = "Bet not found in the selected source" description = ApiMessage.BET_NOT_FOUND_INFO
body(ApiMessage.BET_NOT_FOUND) body(ApiMessage.BET_NOT_FOUND)
} }
} }
@ -162,7 +162,7 @@ fun Application.betRouter() {
description = "Delete a specific bet" description = "Delete a specific bet"
request { request {
body<Map<String, String>> { body<Map<String, String>> {
description = "Id of the desired bet" description = ApiMessage.ID_BET_INFO
} }
} }
response { response {
@ -170,7 +170,7 @@ fun Application.betRouter() {
description = "The bet has been deleted" description = "The bet has been deleted"
} }
HttpStatusCode.NotFound to { HttpStatusCode.NotFound to {
description = "Bet not found in the selected source" description = ApiMessage.BET_NOT_FOUND_INFO
body(ApiMessage.BET_NOT_FOUND) body(ApiMessage.BET_NOT_FOUND)
} }
} }
@ -198,7 +198,7 @@ fun Application.betRouter() {
description = "The bet has been updated" description = "The bet has been updated"
} }
HttpStatusCode.NotFound to { HttpStatusCode.NotFound to {
description = "Bet not found in the selected source" description = ApiMessage.BET_NOT_FOUND_INFO
body(ApiMessage.BET_NOT_FOUND) body(ApiMessage.BET_NOT_FOUND)
} }
} }
@ -218,7 +218,7 @@ fun Application.betRouter() {
get("/bets/toConfirm", { get("/bets/toConfirm", {
description = "Allows a user to know which bets can be validated" description = "Allows a user to know which bets can be validated"
request { request {
headerParameter<JWTPrincipal>("JWT token of the logged user") headerParameter<JWTPrincipal>(ApiMessage.JWT_TOKEN_INFO)
} }
response { response {
HttpStatusCode.Accepted to { HttpStatusCode.Accepted to {
@ -244,7 +244,7 @@ fun Application.betRouter() {
get("/bets/getWon", { get("/bets/getWon", {
description = "Allows a user to know their won bets" description = "Allows a user to know their won bets"
request { request {
headerParameter<JWTPrincipal>("JWT token of the logged user") headerParameter<JWTPrincipal>(ApiMessage.JWT_TOKEN_INFO)
} }
response { response {
HttpStatusCode.Accepted to { HttpStatusCode.Accepted to {
@ -269,7 +269,7 @@ fun Application.betRouter() {
get("/bets/history", { get("/bets/history", {
description = "Allows a user to know own history of bets" description = "Allows a user to know own history of bets"
request { request {
headerParameter<JWTPrincipal>("JWT token of the logged user") headerParameter<JWTPrincipal>(ApiMessage.JWT_TOKEN_INFO)
} }
response { response {
HttpStatusCode.Accepted to { HttpStatusCode.Accepted to {
@ -297,7 +297,7 @@ fun Application.betRouter() {
get("/bets/current", { get("/bets/current", {
description = "Allows a user to know current bets" description = "Allows a user to know current bets"
request { request {
headerParameter<JWTPrincipal>("JWT token of the logged user") headerParameter<JWTPrincipal>(ApiMessage.JWT_TOKEN_INFO)
} }
response { response {
HttpStatusCode.Accepted to { HttpStatusCode.Accepted to {
@ -323,10 +323,10 @@ fun Application.betRouter() {
authenticate { authenticate {
post("/bets/confirm/{id}", { post("/bets/confirm/{id}", {
description = "allows the creator of a bet to confrm the final answer" description = "allows the creator of a bet to confirm the final answer"
request { request {
headerParameter<JWTPrincipal>("JWT token of the logged user") headerParameter<JWTPrincipal>(ApiMessage.JWT_TOKEN_INFO)
pathParameter<UUID>("Id of the desired bet") pathParameter<String>(ApiMessage.ID_BET_INFO)
body<String> { body<String> {
description = "Final answer of the bet" description = "Final answer of the bet"
} }
@ -336,7 +336,7 @@ fun Application.betRouter() {
description = "The final answer has been set" description = "The final answer has been set"
} }
HttpStatusCode.Unauthorized to { HttpStatusCode.Unauthorized to {
description = "The user is not the creator of the bet" description = ApiMessage.NOT_CREATOR_INFO
} }
} }
}) { }) {
@ -360,47 +360,44 @@ fun Application.betRouter() {
post("/bets/users", { post("/bets/users", {
description = "gets all userDTO of a bet" description = "gets all userDTO of a bet"
request { request {
headerParameter<JWTPrincipal>("JWT token of the logged user") headerParameter<JWTPrincipal>(ApiMessage.JWT_TOKEN_INFO)
pathParameter<String>("Id of the desired bet") pathParameter<String>(ApiMessage.ID_BET_INFO)
body<List<UserDTO>> { body<List<UserDTO>> {
description = "UserDTO of the bet" description = "UserDTO of the bet"
} }
} }
response { response {
HttpStatusCode.OK to { HttpStatusCode.Accepted to {
description = "The final answer has been set" description = "List of 4 user of the selected bet"
}
HttpStatusCode.Unauthorized to {
description = "The user is not the creator of the bet"
} }
} }
}) { }) {
logManager.log("Routing", "POST /bets/users") logManager.log("Routing", "POST /bets/users")
hasToken { principal -> hasToken { principal ->
verifyUserFromToken(userDataSource, principal) { user, _ -> verifyUserFromToken(userDataSource, principal) { _, _ ->
val id = call.receive<Map<String, String>>()["id"] ?: "" val id = call.receive<Map<String, String>>()["id"] ?: ""
val participations = participationDataSource.getParticipationFromBetId(id) val participations = participationDataSource.getParticipationFromBetId(id)
val users = val users =
participations.map { userDataSource.getUserByUsername(it.username).first }.toSet().take(4) participations.map { userDataSource.getUserByUsername(it.username).first }.toSet().take(4)
.toList() .toList()
call.respond(users) call.respond(HttpStatusCode.Accepted, users)
} }
} }
} }
post("/bets/pvbet/update", { post("/bets/pvbet/update", {
description = "Add new users to a private bet" description = "Add new users to a private bet"
request { request {
headerParameter<JWTPrincipal>("JWT token of the logged user") headerParameter<JWTPrincipal>(ApiMessage.JWT_TOKEN_INFO)
body<UpdatedPrivateBet> { body<UpdatedPrivateBet> {
description = "Bet id and list of new users" description = "Bet id and list of new users"
} }
} }
response { response {
HttpStatusCode.OK to { HttpStatusCode.Accepted to {
description = "The final answer has been set" description = "Invited users list updated"
} }
HttpStatusCode.Unauthorized to { HttpStatusCode.Unauthorized to {
description = "The user is not the creator of the bet" description = ApiMessage.NOT_CREATOR_INFO
} }
} }
}) { }) {
@ -410,7 +407,7 @@ fun Application.betRouter() {
val updateRequest = call.receive<UpdatedPrivateBet>() val updateRequest = call.receive<UpdatedPrivateBet>()
val bet = betDataSource.getBetById(updateRequest.betid) val bet = betDataSource.getBetById(updateRequest.betid)
if (user.username != bet?.createdBy) { if (user.username != bet?.createdBy) {
call.respond(HttpStatusCode.Forbidden, ApiMessage.USER_DOESNT_HAVE_PERMISSION) call.respond(HttpStatusCode.Unauthorized, ApiMessage.USER_DOESNT_HAVE_PERMISSION)
} }
betDataSource.addUserInPrivatebet(updateRequest) betDataSource.addUserInPrivatebet(updateRequest)
call.respond(HttpStatusCode.Accepted, updateRequest) call.respond(HttpStatusCode.Accepted, updateRequest)

@ -27,7 +27,7 @@ fun Application.friendRouter() {
get("/friends/gets", { get("/friends/gets", {
description = "Allows you to recover all friends of a JWT Token" description = "Allows you to recover all friends of a JWT Token"
request { request {
headerParameter<JWTPrincipal>("JWT token of the logged user") headerParameter<JWTPrincipal>(ApiMessage.JWT_TOKEN_INFO)
} }
response { response {
HttpStatusCode.Accepted to { HttpStatusCode.Accepted to {
@ -43,7 +43,10 @@ fun Application.friendRouter() {
verifyUserFromToken(userDataSource, principal) { _, _ -> verifyUserFromToken(userDataSource, principal) { _, _ ->
val username = tokenManagerBet.getUsernameFromToken(principal) val username = tokenManagerBet.getUsernameFromToken(principal)
val user = userDataSource.getUserByUsername(username).first val user = userDataSource.getUserByUsername(username).first
logManager.log("Routing","ACCEPTED /friends/gets\t${friendDataSource.getFriendFromUserId(user?.id.toString())}") logManager.log(
"Routing",
"ACCEPTED /friends/gets\t${friendDataSource.getFriendFromUserId(user?.id.toString())}"
)
call.respond(HttpStatusCode.Accepted, friendDataSource.getFriendFromUserId(user?.id.toString())) call.respond(HttpStatusCode.Accepted, friendDataSource.getFriendFromUserId(user?.id.toString()))
} }
} }
@ -52,7 +55,7 @@ fun Application.friendRouter() {
get("/friends/requests", { get("/friends/requests", {
description = "Allows you to recover all friend requests of a JWT Token" description = "Allows you to recover all friend requests of a JWT Token"
request { request {
headerParameter<JWTPrincipal>("JWT token of the logged user") headerParameter<JWTPrincipal>(ApiMessage.JWT_TOKEN_INFO)
} }
response { response {
HttpStatusCode.Accepted to { HttpStatusCode.Accepted to {
@ -68,8 +71,11 @@ fun Application.friendRouter() {
verifyUserFromToken(userDataSource, principal) { _, _ -> verifyUserFromToken(userDataSource, principal) { _, _ ->
val username = tokenManagerBet.getUsernameFromToken(principal) val username = tokenManagerBet.getUsernameFromToken(principal)
val user = userDataSource.getUserByUsername(username).first val user = userDataSource.getUserByUsername(username).first
logManager.log("Routing","ACCEPTED /friends/requests\t${friendDataSource.getFriendRequestsFromUserId(user?.id.toString()) logManager.log(
}") "Routing", "ACCEPTED /friends/requests\t${
friendDataSource.getFriendRequestsFromUserId(user?.id.toString())
}"
)
call.respond( call.respond(
HttpStatusCode.Accepted, HttpStatusCode.Accepted,
friendDataSource.getFriendRequestsFromUserId(user?.id.toString()) friendDataSource.getFriendRequestsFromUserId(user?.id.toString())
@ -81,7 +87,7 @@ fun Application.friendRouter() {
post("/friends/add", { post("/friends/add", {
description = "Allows a user to add a friend" description = "Allows a user to add a friend"
request { request {
headerParameter<JWTPrincipal>("JWT token of the logged user") headerParameter<JWTPrincipal>(ApiMessage.JWT_TOKEN_INFO)
body<String> { body<String> {
description = "User to add in the friends list" description = "User to add in the friends list"
} }
@ -133,7 +139,7 @@ fun Application.friendRouter() {
post("/friends/delete", { post("/friends/delete", {
description = "Allows a user to delete a friend" description = "Allows a user to delete a friend"
request { request {
headerParameter<JWTPrincipal>("JWT token of the logged user") headerParameter<JWTPrincipal>(ApiMessage.JWT_TOKEN_INFO)
body<String> { body<String> {
description = "User to delete in the friends list" description = "User to delete in the friends list"
} }
@ -182,7 +188,7 @@ fun Application.friendRouter() {
get("/friends/search/{search}", { get("/friends/search/{search}", {
description = "Search for users based on username" description = "Search for users based on username"
request { request {
headerParameter<JWTPrincipal>("JWT token of the logged user") headerParameter<JWTPrincipal>(ApiMessage.JWT_TOKEN_INFO)
pathParameter<String>("Search string") pathParameter<String>("Search string")
} }
response { response {

@ -37,7 +37,7 @@ fun Application.userRouter() {
description = "Allows a user to register" description = "Allows a user to register"
request { request {
body<UserRequest> { body<UserRequest> {
description = "User information" description = ApiMessage.USER_UPDATE_INFO
} }
} }
response { response {
@ -92,7 +92,7 @@ fun Application.userRouter() {
description = "Allows a user to login" description = "Allows a user to login"
request { request {
body<CheckUser> { body<CheckUser> {
description = "User information" description = ApiMessage.USER_UPDATE_INFO
} }
} }
response { response {
@ -114,8 +114,7 @@ fun Application.userRouter() {
userDtoWithToken.token = tokenManagerUser.generateOrReplaceJWTToken(userDtoWithToken) userDtoWithToken.token = tokenManagerUser.generateOrReplaceJWTToken(userDtoWithToken)
logManager.log("Routing", "ACCEPTED /users/login\t${userDtoWithToken}") logManager.log("Routing", "ACCEPTED /users/login\t${userDtoWithToken}")
call.respond(HttpStatusCode.OK, userDtoWithToken) call.respond(HttpStatusCode.OK, userDtoWithToken)
} ?: } ?: logManager.log("Routing", "${ApiMessage.USER_NOT_FOUND} /users/login")
logManager.log("Routing","${ApiMessage.USER_NOT_FOUND} /users/login")
call.respond(HttpStatusCode.NotFound, ApiMessage.USER_NOT_FOUND) call.respond(HttpStatusCode.NotFound, ApiMessage.USER_NOT_FOUND)
} else { } else {
logManager.log("Routing", "${ApiMessage.INCORRECT_LOGIN_PASSWORD} /users/login") logManager.log("Routing", "${ApiMessage.INCORRECT_LOGIN_PASSWORD} /users/login")
@ -148,9 +147,9 @@ fun Application.userRouter() {
description = "Allow you to delete your account" description = "Allow you to delete your account"
request { request {
headerParameter<JWTPrincipal>("JWT token of the logged user") headerParameter<JWTPrincipal>(ApiMessage.JWT_TOKEN_INFO)
body<CheckUser> { body<CheckUser> {
description = "User information" description = ApiMessage.USER_UPDATE_INFO
} }
} }
response { response {
@ -193,7 +192,7 @@ fun Application.userRouter() {
get("/users/token", { get("/users/token", {
description = "Allows you to retrieve the user linked to a JWT token" description = "Allows you to retrieve the user linked to a JWT token"
request { request {
headerParameter<JWTPrincipal>("JWT token of the user") headerParameter<JWTPrincipal>(ApiMessage.JWT_TOKEN_INFO)
} }
response { response {
HttpStatusCode.OK to { HttpStatusCode.OK to {
@ -214,7 +213,7 @@ fun Application.userRouter() {
get("/users/gift", { get("/users/gift", {
description = "Allows you to collect your daily gift" description = "Allows you to collect your daily gift"
request { request {
headerParameter<JWTPrincipal>("JWT token of the logged user") headerParameter<JWTPrincipal>(ApiMessage.JWT_TOKEN_INFO)
} }
response { response {
HttpStatusCode.OK to { HttpStatusCode.OK to {
@ -248,9 +247,9 @@ fun Application.userRouter() {
description = "Allow you to add a profil image" description = "Allow you to add a profil image"
request { request {
headerParameter<JWTPrincipal>("JWT token of the logged user") headerParameter<JWTPrincipal>(ApiMessage.JWT_TOKEN_INFO)
body<CheckUser> { body<CheckUser> {
description = "User information" description = ApiMessage.USER_UPDATE_INFO
} }
} }
response { response {

Loading…
Cancel
Save