diff --git a/backend-services/feed-service/app/controllers/DailyController.scala b/backend-services/feed-service/app/controllers/DailyController.scala index 3dc422bdf829972b14cbb499639ffc132c939256..8e5d1b2ba0764ff8f9e8957d8b1d7028fcd33220 100644 --- a/backend-services/feed-service/app/controllers/DailyController.scala +++ b/backend-services/feed-service/app/controllers/DailyController.scala @@ -98,4 +98,22 @@ class DailyController @Inject()(val controllerComponents: ControllerComponents) case _: Throwable => BadRequest("Exception raised") } } + + /** + * Create an Action to unlike a Daily. + */ + def unlike() = Action { + println("DailyController:unlike") + + try { + // Dummy data + Daily.unlikeAsync(new ObjectId("642314b4b9748f6794e9895b"), new ObjectId("641128f7e80bcd1ba39d04ae")) + Ok("Updated") + } catch { + case _: TimeoutException => BadRequest("Request timed out") + case ex: ConflictException => BadRequest(ex.getMessage()) + case ex: NotFoundException => BadRequest(ex.getMessage()) + case _: Throwable => BadRequest("Exception raised") + } + } } diff --git a/backend-services/feed-service/app/models/Daily.scala b/backend-services/feed-service/app/models/Daily.scala index ab1b1f15ba89fd74ceb439705bdf148d325e4942..033168a58894b93f2169b6ae773d54539ab0d856 100644 --- a/backend-services/feed-service/app/models/Daily.scala +++ b/backend-services/feed-service/app/models/Daily.scala @@ -93,6 +93,35 @@ object Daily { Await.result[Unit](result, timeout.seconds) } + def unlikeAsync(dailyId: ObjectId, likerId: ObjectId, timeout: Int = 4): Unit = { + val result: Future[Unit] = for { + // 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 with given ID exists + _ <- User.userExists(likerId).map((exists: Boolean) => if (!exists) throw new NotFoundException("No user with given ID.")) + + // Check user has liked the Daily + _ = if (!daily.usersLiked.contains(likerId)) throw new ConflictException("User has not liked this Daily.") + + unlike: Unit <- { + val updatedUsersLiked: Seq[ObjectId] = daily.usersLiked.filterNot(_ == likerId) + val update: Bson = Updates.set("usersLiked", updatedUsersLiked) + + dailyRepo.updateOne(dailyId, Seq(update)) + } + } yield unlike + + Await.result[Unit](result, timeout.seconds) + } + // Convert from Daily object to JSON (serializing to JSON) def toJson(daily: Daily): JsValue = { val usersLikedAsJsStrings: Seq[JsString] = daily.usersLiked.map[JsString](id => JsString(id.toString())) diff --git a/backend-services/feed-service/conf/routes b/backend-services/feed-service/conf/routes index f580a63499ae37b02d98176ab0f2cb791c7ef26c..90c875672604793a78be188407207bc9f5cff64e 100644 --- a/backend-services/feed-service/conf/routes +++ b/backend-services/feed-service/conf/routes @@ -18,3 +18,5 @@ GET /feed controllers.DailyController.getUserFeed() POST /daily/create controllers.DailyController.create() PUT /daily/like controllers.DailyController.like() + +PUT /daily/unlike controllers.DailyController.unlike()