Skip to content
Snippets Groups Projects
Commit c6c254cb authored by Felipe D'Abrantes's avatar Felipe D'Abrantes
Browse files

Ensure liker is an existing user

parent 9724bef7
No related branches found
No related tags found
1 merge request!14Add endpoints to manage Dailies
...@@ -66,14 +66,23 @@ object Daily { ...@@ -66,14 +66,23 @@ object Daily {
def likeAsync(dailyId: ObjectId, likerId: ObjectId, timeout: Int = 4): Unit = { def likeAsync(dailyId: ObjectId, likerId: ObjectId, timeout: Int = 4): Unit = {
val result: Future[Unit] = for { val result: Future[Unit] = for {
oDaily: Option[Daily] <- dailyRepo.getById(dailyId) // Fetch Daily from given ID
like: Unit <- { daily: Daily <- {
// Check daily with given ID exists dailyRepo.getById(dailyId).map((oDaily: Option[Daily]) => {
val daily = if (oDaily.isEmpty) throw new NotFoundException("No daily with given ID.") else oDaily.get if (oDaily.isEmpty)
throw new NotFoundException("No daily with given ID.")
// Check user has not already liked Daily else
if (daily.usersLiked.contains(likerId)) throw new ConflictException("User has already liked this Daily.") 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 updatedUsersLiked: Seq[ObjectId] = daily.usersLiked :+ likerId
val update: Bson = Updates.set("usersLiked", updatedUsersLiked) val update: Bson = Updates.set("usersLiked", updatedUsersLiked)
......
...@@ -11,4 +11,11 @@ object User { ...@@ -11,4 +11,11 @@ object User {
val friends: Seq[ObjectId] = Seq(new ObjectId("641128f7e80bcd1ba39d04af")) val friends: Seq[ObjectId] = Seq(new ObjectId("641128f7e80bcd1ba39d04af"))
Future.successful(friends) 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)
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment