From 680b1c2cb1ccdf5cc3ef6d0163094945b5c9895e Mon Sep 17 00:00:00 2001 From: avalin Date: Wed, 29 May 2024 22:19:02 +0200 Subject: [PATCH] Add friend request route --- .../kotlin/allin/data/FriendDataSource.kt | 1 + .../allin/data/mock/MockFriendDataSource.kt | 9 +++++++ .../data/postgres/PostgresFriendDataSource.kt | 14 ++++++++++ .../main/kotlin/allin/routing/friendRouter.kt | 26 +++++++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/Sources/src/main/kotlin/allin/data/FriendDataSource.kt b/Sources/src/main/kotlin/allin/data/FriendDataSource.kt index 5436116..71a25ac 100644 --- a/Sources/src/main/kotlin/allin/data/FriendDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/FriendDataSource.kt @@ -6,6 +6,7 @@ import allin.model.FriendStatus interface FriendDataSource { fun addFriend(sender: String, receiver: String) fun getFriendFromUserId(id: String): List + fun getFriendRequestsFromUserId(id: String): List fun deleteFriend(senderId: String, receiverId: String): Boolean fun isFriend(firstUser: String, secondUser: String): Boolean fun filterUsersByUsername(fromUserId: String, search: String): List diff --git a/Sources/src/main/kotlin/allin/data/mock/MockFriendDataSource.kt b/Sources/src/main/kotlin/allin/data/mock/MockFriendDataSource.kt index 40c2122..d41c74e 100644 --- a/Sources/src/main/kotlin/allin/data/mock/MockFriendDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/mock/MockFriendDataSource.kt @@ -26,6 +26,15 @@ class MockFriendDataSource(private val mockData: MockDataSource.MockData) : Frie ) } + override fun getFriendRequestsFromUserId(id: String): List { + return friends + .filter { (it.receiver == id) && !isFriend(id, it.sender) } + .mapNotNull { + users.find { usr -> usr.id == it.sender } + ?.toDto(friendStatus = FriendStatus.NOT_FRIEND) + } + } + override fun deleteFriend(senderId: String, receiverId: String) = friends.removeIf { (it.sender == senderId) && (it.receiver == receiverId) } diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresFriendDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresFriendDataSource.kt index 4c2eefd..21e97fb 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresFriendDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresFriendDataSource.kt @@ -37,6 +37,20 @@ class PostgresFriendDataSource(private val database: Database) : FriendDataSourc ) } + override fun getFriendRequestsFromUserId(id: String): List { + return database.friends + .filter { it.receiver eq id } + .mapNotNull { + if (isFriend(firstUser = id, secondUser = it.sender)) { + null + } else { + database.users.find { usr -> + usr.id eq it.sender + }?.toUserDTO(friendStatus = FriendStatus.NOT_FRIEND) + } + } + } + override fun deleteFriend(senderId: String, receiverId: String): Boolean { database.friends.removeIf { (it.sender eq receiverId) and (it.receiver eq senderId) } diff --git a/Sources/src/main/kotlin/allin/routing/friendRouter.kt b/Sources/src/main/kotlin/allin/routing/friendRouter.kt index 6b747a9..137286f 100644 --- a/Sources/src/main/kotlin/allin/routing/friendRouter.kt +++ b/Sources/src/main/kotlin/allin/routing/friendRouter.kt @@ -45,6 +45,32 @@ fun Application.friendRouter() { } } + } + get("/friends/requests", { + description = "Allows you to recover all friend requests of a JWT Token" + request { + headerParameter("JWT token of the logged user") + } + response { + HttpStatusCode.Accepted to { + description = "The list of friend requests is available" + body> { + description = "List of friend requests" + } + } + } + }) { + hasToken { principal -> + verifyUserFromToken(userDataSource, principal) { _, _ -> + val username = tokenManagerBet.getUsernameFromToken(principal) + val user = userDataSource.getUserByUsername(username).first + call.respond( + HttpStatusCode.Accepted, + friendDataSource.getFriendRequestsFromUserId(user?.id.toString()) + ) + } + } + } post("/friends/add", { description = "Allows a user to add a friend"