From c60e3905aa436f0b36ab53eb4c5837f566c5a765 Mon Sep 17 00:00:00 2001 From: luevard <99143550+saucepommefrite@users.noreply.github.com> Date: Thu, 16 May 2024 09:23:29 +0200 Subject: [PATCH] :sparkles: Add friend mock and refactor code --- .../kotlin/allin/data/mock/MockDataSource.kt | 1 + .../allin/data/mock/MockFriendDataSource.kt | 28 +++++++++------ .../data/postgres/PostgresFriendDataSource.kt | 12 +++---- .../src/main/kotlin/allin/model/ApiMessage.kt | 4 +-- .../main/kotlin/allin/routing/friendRouter.kt | 35 +++++++++++++++++-- 5 files changed, 57 insertions(+), 23 deletions(-) diff --git a/Sources/src/main/kotlin/allin/data/mock/MockDataSource.kt b/Sources/src/main/kotlin/allin/data/mock/MockDataSource.kt index 8dbf3f1..417bdf3 100644 --- a/Sources/src/main/kotlin/allin/data/mock/MockDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/mock/MockDataSource.kt @@ -19,6 +19,7 @@ class MockDataSource : AllInDataSource() { val users by lazy { mutableListOf() } val lastGifts by lazy { mutableMapOf() } val participations by lazy { mutableListOf() } + val friends by lazy { mutableListOf() } } private val mockData by lazy { MockData() } diff --git a/Sources/src/main/kotlin/allin/data/mock/MockFriendDataSource.kt b/Sources/src/main/kotlin/allin/data/mock/MockFriendDataSource.kt index 585d394..f8f0bac 100644 --- a/Sources/src/main/kotlin/allin/data/mock/MockFriendDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/mock/MockFriendDataSource.kt @@ -1,22 +1,28 @@ package allin.data.mock import allin.data.FriendDataSource +import allin.model.Friend + +class MockFriendDataSource(private val mockData: MockDataSource.MockData) : FriendDataSource { + + private val friends get() = mockData.friends -class MockFriendDataSource(mockData: MockDataSource.MockData) : FriendDataSource { override fun addFriend(sender: String, receiver: String) { - TODO("Not yet implemented") + mockData.friends.add(Friend(sender, receiver)) } - override fun getFriendFromUserId(id: String): List { - TODO("Not yet implemented") - } + override fun getFriendFromUserId(id: String) = + friends.map { Friend(sender = it.sender, receiver = it.receiver) } + .filter { it.sender == id } + .map { it.receiver } - override fun deleteFriend(senderId: String, receiverId: String): Boolean { - TODO("Not yet implemented") - } + override fun deleteFriend(senderId: String, receiverId: String) = + friends.removeIf { (it.sender == senderId) && (it.receiver == receiverId) } - override fun isFriend(firstUser: String, secondUser: String): Boolean { - TODO("Not yet implemented") - } + override fun isFriend(firstUser: String, secondUser: String) = + friends + .filter { (it.sender == firstUser) and (it.receiver == secondUser) } + .map { Friend(sender = it.sender, receiver = it.receiver) } + .isNotEmpty() } \ No newline at end of file diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresFriendDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresFriendDataSource.kt index cbfd179..130205a 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresFriendDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresFriendDataSource.kt @@ -21,17 +21,15 @@ class PostgresFriendDataSource(private val database: Database) : FriendDataSourc ) } - override fun getFriendFromUserId(id: String): List { - val friendList = database.friends.map { it.toFriend() } - val friendPairs = friendList.map { it.sender to it.receiver }.toSet() - return friendList + override fun getFriendFromUserId(id: String) = + database.friends.map { it.toFriend() } .filter { it.sender == id } .map { it.receiver } - } + override fun deleteFriend(senderId: String, receiverId: String): Boolean { - database.friends.removeIf { it.sender eq senderId } - return database.friends.removeIf { it.sender eq senderId } > 0 + database.friends.removeIf { (it.sender eq senderId) and (it.receiver eq receiverId) } + return database.friends.removeIf { (it.sender eq senderId) and (it.receiver eq receiverId) } > 0 } override fun isFriend(firstUser: String, secondUser: String) = diff --git a/Sources/src/main/kotlin/allin/model/ApiMessage.kt b/Sources/src/main/kotlin/allin/model/ApiMessage.kt index 2d1a045..b8b3ed3 100644 --- a/Sources/src/main/kotlin/allin/model/ApiMessage.kt +++ b/Sources/src/main/kotlin/allin/model/ApiMessage.kt @@ -14,6 +14,6 @@ object ApiMessage { const val NO_GIFT = "Can't get daily gift." const val USER_CANT_BE_DELETE = "This user can't be delete now !" const val FRIENDS_ALREADY_EXISTS = "User already exists in your Friends List." - const val FRIENDS_DOESNT_EXISTS = "User already exists in your Friends List." - + 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." } \ No newline at end of file diff --git a/Sources/src/main/kotlin/allin/routing/friendRouter.kt b/Sources/src/main/kotlin/allin/routing/friendRouter.kt index c1ad79b..f7d857c 100644 --- a/Sources/src/main/kotlin/allin/routing/friendRouter.kt +++ b/Sources/src/main/kotlin/allin/routing/friendRouter.kt @@ -2,7 +2,10 @@ package allin.routing import allin.dataSource import allin.ext.hasToken +import allin.ext.verifyUserFromToken import allin.model.ApiMessage +import allin.model.Bet +import io.github.smiley4.ktorswaggerui.dsl.get import io.github.smiley4.ktorswaggerui.dsl.post import io.ktor.http.* @@ -21,8 +24,30 @@ fun Application.friendRouter() { routing { authenticate { + get("/friends/gets", { + description = "Allows you to recover all friends of a JWT Token" + request { + headerParameter("JWT token of the logged user") + } + response { + HttpStatusCode.Accepted to { + description = "The list of friends is available" + body> { + description = "List of friends" + } + } + } + }) { + hasToken { principal -> + verifyUserFromToken(userDataSource, principal) { _, _ -> + val username = tokenManagerBet.getUsernameFromToken(principal) + val user = userDataSource.getUserByUsername(username).first + call.respond(HttpStatusCode.Accepted, friendDataSource.getFriendFromUserId(user?.id.toString())) + } + } - post("/friend/add", { + } + post("/friends/add", { description = "Allows a user to add a friend" request { headerParameter("JWT token of the logged user") @@ -53,7 +78,11 @@ fun Application.friendRouter() { if (user == null || userFriend == null) { call.respond(HttpStatusCode.Conflict, ApiMessage.USER_NOT_FOUND) - } else { + } + else if (userFriend == user) { + call.respond(HttpStatusCode.Conflict,ApiMessage.FRIENDS_REQUEST_HIMSELF) + } + else { val friendlist = friendDataSource.getFriendFromUserId(user.id) if (friendlist.contains(userFriend.id)) { call.respond(HttpStatusCode.Conflict,ApiMessage.FRIENDS_ALREADY_EXISTS) @@ -65,7 +94,7 @@ fun Application.friendRouter() { } } - post("/friend/delete", { + post("/friends/delete", { description = "Allows a user to delete a friend" request { headerParameter("JWT token of the logged user")