diff --git a/backend-services/feed-service/app/controllers/DailyController.scala b/backend-services/feed-service/app/controllers/DailyController.scala index 1c1f1350871426bc2be79bdcf104126294c2a443..6b0541c87f2c22489bbf2555c4464ed7ff3decbb 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 b5cfbc2c815a3288f1e78fdcfd226eb2f1fbeb42..00460abdf1278b7b35bab9eb73cb3eddc7a04093 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 fa8a8370e6f606d56c83cfacdfe78097dc0ab727..d2b13184d5ab1c955cf84210d3a80089d7a3bedb 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 c623039fce85df68a06171a09c097924b0c7cdfc..aed38ba7334d9ae12e0bd25d837db09f8cf0da42 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()