From b21138096ce3a0bc38e5b10bf8653c79e6ae15a5 Mon Sep 17 00:00:00 2001
From: Felipe D'Abrantes <felidabrantes@gmail>
Date: Wed, 15 Mar 2023 03:02:32 +0000
Subject: [PATCH] Implement endpoint for fetching all Dailies (#19)

---
 .../app/controllers/DailyController.scala     | 39 +++++++++++++++++++
 backend-services/feed-service/conf/routes     |  2 +
 2 files changed, 41 insertions(+)
 create mode 100644 backend-services/feed-service/app/controllers/DailyController.scala

diff --git a/backend-services/feed-service/app/controllers/DailyController.scala b/backend-services/feed-service/app/controllers/DailyController.scala
new file mode 100644
index 00000000..5cd5d85c
--- /dev/null
+++ b/backend-services/feed-service/app/controllers/DailyController.scala
@@ -0,0 +1,39 @@
+package controllers
+
+import javax.inject._
+import play.api._
+import play.api.mvc._
+
+import models.Daily
+
+import scala.concurrent.ExecutionContext.Implicits.global
+import scala.concurrent.{Future, Await, TimeoutException}
+import scala.concurrent.duration._
+import org.bson.types.ObjectId
+
+/**
+ * This controller handles all the Daily endpoints.
+ */
+@Singleton
+class DailyController @Inject()(val controllerComponents: ControllerComponents) extends BaseController {
+
+  /**
+   * Create an Action to fetch all the Dailies in the DB.
+   */
+  def getAll() = Action { implicit request: Request[AnyContent] =>
+    println("DailyController:getAll")
+
+    var result = Daily.getAllDailies()
+
+    try {
+        // Wait for 4 seconds for the Future to complete
+        val resultCompleted = Await.result(result, 4.seconds)
+
+        val jsonResult: Seq[String] = resultCompleted.map(daily => Daily.toString(daily))
+        Ok(jsonResult.toString())
+    } catch {
+        case e: TimeoutException =>
+            BadRequest("Request timed out")
+    }
+  }
+}
diff --git a/backend-services/feed-service/conf/routes b/backend-services/feed-service/conf/routes
index c987e313..bb8d194f 100644
--- a/backend-services/feed-service/conf/routes
+++ b/backend-services/feed-service/conf/routes
@@ -10,3 +10,5 @@ GET     /                           controllers.HomeController.index()
 GET     /assets/*file               controllers.Assets.versioned(path="/public", file: Asset)
 
 GET     /mongo_test                 controllers.MongoTestController.index()
+
+GET     /daily/getAll      controllers.DailyController.getAll()
-- 
GitLab