From c6c254cb685eaf9dd335ef5c701cfe9982f1388b Mon Sep 17 00:00:00 2001 From: Felipe D'Abrantes <felidabrantes@gmail> Date: Sat, 8 Apr 2023 00:50:12 +0100 Subject: [PATCH] Ensure liker is an existing user --- .../feed-service/app/models/Daily.scala | 23 +++++++++++++------ .../feed-service/app/models/User.scala | 7 ++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/backend-services/feed-service/app/models/Daily.scala b/backend-services/feed-service/app/models/Daily.scala index a81a6368..ab1b1f15 100644 --- a/backend-services/feed-service/app/models/Daily.scala +++ b/backend-services/feed-service/app/models/Daily.scala @@ -66,14 +66,23 @@ object Daily { def likeAsync(dailyId: ObjectId, likerId: ObjectId, timeout: Int = 4): Unit = { val result: Future[Unit] = for { - oDaily: Option[Daily] <- dailyRepo.getById(dailyId) - like: Unit <- { - // Check daily with given ID exists - val daily = if (oDaily.isEmpty) throw new NotFoundException("No daily with given ID.") else oDaily.get - - // Check user has not already liked Daily - if (daily.usersLiked.contains(likerId)) throw new ConflictException("User has already liked this Daily.") + // Fetch Daily from given ID + daily: Daily <- { + dailyRepo.getById(dailyId).map((oDaily: Option[Daily]) => { + if (oDaily.isEmpty) + throw new NotFoundException("No daily with given ID.") + else + oDaily.get + }) + } + + // Check user has not already liked the Daily + _ = if (daily.usersLiked.contains(likerId)) throw new ConflictException("User has already liked this Daily.") + // Check user with given ID exists + _ <- User.userExists(likerId).map((exists: Boolean) => if (!exists) throw new NotFoundException("No user with given ID.")) + + like: Unit <- { val updatedUsersLiked: Seq[ObjectId] = daily.usersLiked :+ likerId val update: Bson = Updates.set("usersLiked", updatedUsersLiked) diff --git a/backend-services/feed-service/app/models/User.scala b/backend-services/feed-service/app/models/User.scala index 945285ce..6e993551 100644 --- a/backend-services/feed-service/app/models/User.scala +++ b/backend-services/feed-service/app/models/User.scala @@ -11,4 +11,11 @@ object User { val friends: Seq[ObjectId] = Seq(new ObjectId("641128f7e80bcd1ba39d04af")) Future.successful(friends) } + + def userExists(userId: ObjectId): Future[Boolean] = { + // TODO: Fetch user verification from User Service + println("Verifying user with ID ", userId.toString(), " exists") + val exists: Boolean = true + Future.successful(exists) + } } -- GitLab