From 60a1bcc0ada870e57b5cd4037a1aedd42b775827 Mon Sep 17 00:00:00 2001 From: Felipe D'Abrantes <felidabrantes@gmail> Date: Tue, 21 Mar 2023 11:46:35 +0000 Subject: [PATCH] Add endpoint to fetch user's dailies (#19) --- .../app/controllers/DailyController.scala | 16 ++++++++++++++ .../feed-service/app/models/Daily.scala | 5 +++++ .../app/repositories/DailyRepository.scala | 21 +++++++++++++++++++ backend-services/feed-service/conf/routes | 2 ++ 4 files changed, 44 insertions(+) diff --git a/backend-services/feed-service/app/controllers/DailyController.scala b/backend-services/feed-service/app/controllers/DailyController.scala index 1c1f1350..6b0541c8 100644 --- a/backend-services/feed-service/app/controllers/DailyController.scala +++ b/backend-services/feed-service/app/controllers/DailyController.scala @@ -32,6 +32,22 @@ class DailyController @Inject()(val controllerComponents: ControllerComponents) } } + /** + * Create an Action to fetch the user's Dailies in the DB. + */ + def getUserDailies() = Action { implicit request: Request[AnyContent] => + println("DailyController:getUserDailies") + + try { + val result: Seq[Daily] = Daily.getUserDailiesAsync(new ObjectId("641128f7e80bcd1ba39d04af")) + val jsonResult = Daily.toJson(result) + Ok(jsonResult) + } catch { + case ex: TimeoutException => BadRequest("Request timed out") + case ex => BadRequest("Exception raised") + } + } + def create() = Action { implicit request: Request[AnyContent] => println("DailyController:create") diff --git a/backend-services/feed-service/app/models/Daily.scala b/backend-services/feed-service/app/models/Daily.scala index b5cfbc2c..00460abd 100644 --- a/backend-services/feed-service/app/models/Daily.scala +++ b/backend-services/feed-service/app/models/Daily.scala @@ -39,6 +39,11 @@ object Daily { Await.result(future, timeout.seconds) } + def getUserDailiesAsync(userId: ObjectId, timeout: Int = 4): Seq[Daily] = { + val future: Future[Seq[Daily]] = dailyRepo.getUserDailies(userId) + Await.result(future, timeout.seconds) + } + // Convert from Daily object to JSON (serializing to JSON) def toJson(daily: Daily): JsValue = { val dailyJson = Seq( diff --git a/backend-services/feed-service/app/repositories/DailyRepository.scala b/backend-services/feed-service/app/repositories/DailyRepository.scala index fa8a8370..d2b13184 100644 --- a/backend-services/feed-service/app/repositories/DailyRepository.scala +++ b/backend-services/feed-service/app/repositories/DailyRepository.scala @@ -5,6 +5,7 @@ import com.typesafe.config.ConfigFactory import models.{Daily, MongoDBClient} import org.mongodb.scala.{MongoCollection, Document} +import org.mongodb.scala.model.{Filters} import org.bson.types.ObjectId import scala.concurrent.ExecutionContext.Implicits.global @@ -54,6 +55,26 @@ class DailyRepository extends MongoDBClient { }) } + /** + * Gets all the user's Dailies. + * + * @return A Future containing a sequence of the user's Daily objects. + */ + def getUserDailies(userId: ObjectId): Future[Seq[Daily]] = { + val userFilter = Filters.equal("user_id", userId) + val documents: Future[Seq[Document]] = find(dailiesCollection, userFilter) + + documents.map(document => { + document.map(doc => Daily( + Some(doc.getObjectId("_id")), + doc.getObjectId("user_id"), + doc.getObjectId("question_id"), + doc.getString("content"), + doc.getInteger("likes") + )) + }) + } + /** * Inserts a Daily record into the database. * diff --git a/backend-services/feed-service/conf/routes b/backend-services/feed-service/conf/routes index c623039f..aed38ba7 100644 --- a/backend-services/feed-service/conf/routes +++ b/backend-services/feed-service/conf/routes @@ -13,4 +13,6 @@ GET /mongo_test controllers.MongoTestController.index() GET /daily/getAll controllers.DailyController.getAll() +GET /daily/users controllers.DailyController.getUserDailies() + POST /daily/create controllers.DailyController.create() -- GitLab