diff --git a/backend-services/feed-service/app/controllers/DailyController.scala b/backend-services/feed-service/app/controllers/DailyController.scala index 0217dc2e89b06f12c5be545fff00eac0b1aa274c..3dc422bdf829972b14cbb499639ffc132c939256 100644 --- a/backend-services/feed-service/app/controllers/DailyController.scala +++ b/backend-services/feed-service/app/controllers/DailyController.scala @@ -5,7 +5,7 @@ import play.api.mvc._ import play.api.libs.json.JsValue import models.{Daily} -import models.exceptions.ConflictException +import models.exceptions.{ConflictException, NotFoundException} import scala.concurrent.TimeoutException import org.bson.types.ObjectId @@ -94,6 +94,7 @@ class DailyController @Inject()(val controllerComponents: ControllerComponents) } 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 db1c1098e307312756681bdfcad4eabae93d4a69..a81a63686f793c8822df1021bd271541871e898c 100644 --- a/backend-services/feed-service/app/models/Daily.scala +++ b/backend-services/feed-service/app/models/Daily.scala @@ -1,7 +1,7 @@ package models import repositories.{DailyRepository} -import models.exceptions.{ConflictException} +import models.exceptions.{ConflictException, NotFoundException} import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.{Future, Await} @@ -66,8 +66,11 @@ object Daily { def likeAsync(dailyId: ObjectId, likerId: ObjectId, timeout: Int = 4): Unit = { val result: Future[Unit] = for { - daily: Daily <- dailyRepo.getById(dailyId) + 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.") diff --git a/backend-services/feed-service/app/models/exceptions/NotFoundException.scala b/backend-services/feed-service/app/models/exceptions/NotFoundException.scala new file mode 100644 index 0000000000000000000000000000000000000000..edd62c3f6713f21512da8db56d2c2f543f233a43 --- /dev/null +++ b/backend-services/feed-service/app/models/exceptions/NotFoundException.scala @@ -0,0 +1,3 @@ +package models.exceptions + +case class NotFoundException(message: String) extends Exception(message) diff --git a/backend-services/feed-service/app/repositories/Repository.scala b/backend-services/feed-service/app/repositories/Repository.scala index ee79ca590688d34bd9ae1271c59943be492a1e80..4cb446e36575e453b3b100ed5d77b54e9ef9ac90 100644 --- a/backend-services/feed-service/app/repositories/Repository.scala +++ b/backend-services/feed-service/app/repositories/Repository.scala @@ -55,11 +55,11 @@ class Repository[T: ClassTag](databaseName: String, collectionName: String) { /** * Gets a record with the given ID in the collection. * - * @return A Future containing a sequence of matching documents. + * @return A Future containing an optional matching document. */ - def getById(id: ObjectId): Future[T] = { + def getById(id: ObjectId): Future[Option[T]] = { val filter: Bson = Filters.equal[ObjectId]("_id", id) - MongoConnection.find[T](collection, filter).map(_.head) + MongoConnection.find[T](collection, filter).map(_.headOption) } /**