From 95dcaf6098180f117f95a394091bd6738199d012 Mon Sep 17 00:00:00 2001 From: avalin Date: Wed, 29 May 2024 22:43:17 +0200 Subject: [PATCH] Fix delete friend response --- .../allin/data/postgres/PostgresFriendDataSource.kt | 6 ++++-- Sources/src/main/kotlin/allin/routing/friendRouter.kt | 8 +++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresFriendDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresFriendDataSource.kt index 21e97fb..c42df7c 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresFriendDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresFriendDataSource.kt @@ -53,8 +53,10 @@ class PostgresFriendDataSource(private val database: Database) : FriendDataSourc override fun deleteFriend(senderId: String, receiverId: String): Boolean { - database.friends.removeIf { (it.sender eq receiverId) and (it.receiver eq senderId) } - return database.friends.removeIf { (it.sender eq senderId) and (it.receiver eq receiverId) } > 0 + val result = database.friends.removeIf { (it.sender eq receiverId) and (it.receiver eq senderId) } + + database.friends.removeIf { (it.sender eq senderId) and (it.receiver eq receiverId) } + + return result > 0 } override fun isFriend(firstUser: String, secondUser: String) = diff --git a/Sources/src/main/kotlin/allin/routing/friendRouter.kt b/Sources/src/main/kotlin/allin/routing/friendRouter.kt index 137286f..3c39bd9 100644 --- a/Sources/src/main/kotlin/allin/routing/friendRouter.kt +++ b/Sources/src/main/kotlin/allin/routing/friendRouter.kt @@ -154,12 +154,10 @@ fun Application.friendRouter() { if (user == null || userFriend == null) { call.respond(HttpStatusCode.Conflict, ApiMessage.USER_NOT_FOUND) } else { - val friendlist = friendDataSource.getFriendFromUserId(user.id) - if (!friendlist.map { it.id }.contains(userFriend.id)) { - call.respond(HttpStatusCode.Conflict, ApiMessage.FRIENDS_DOESNT_EXISTS) - } else { - friendDataSource.deleteFriend(user.id, userFriend.id) + if (friendDataSource.deleteFriend(user.id, userFriend.id)) { call.respond(HttpStatusCode.Created, usernameFriend) + } else { + call.respond(HttpStatusCode.Conflict, ApiMessage.FRIENDS_DOESNT_EXISTS) } } }