diff --git a/backend-services/feed-service/app/models/Daily.scala b/backend-services/feed-service/app/models/Daily.scala index 00460abdf1278b7b35bab9eb73cb3eddc7a04093..64bb55543a54c56d194e4e8a463b6e9621a12064 100644 --- a/backend-services/feed-service/app/models/Daily.scala +++ b/backend-services/feed-service/app/models/Daily.scala @@ -44,6 +44,16 @@ object Daily { Await.result(future, timeout.seconds) } + def getUserFeedAsync(userId: ObjectId, timeout: Int = 4): Seq[Daily] = { + // Sequentially waits for Future objects to complete before calling next method + val result: Future[Seq[Daily]] = for { + friends: Seq[ObjectId] <- User.getUserFriends(userId) + feed: Seq[Daily] <- dailyRepo.getUserDailies(friends) + } yield feed + + Await.result(result, 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 d2b13184d5ab1c955cf84210d3a80089d7a3bedb..5e18898181decedf376b2ea37c13116cf7fff0cd 100644 --- a/backend-services/feed-service/app/repositories/DailyRepository.scala +++ b/backend-services/feed-service/app/repositories/DailyRepository.scala @@ -75,6 +75,28 @@ class DailyRepository extends MongoDBClient { }) } + /** + * Gets all the user's Dailies. + * + * @return A Future containing a sequence of the user's Daily objects. + */ + def getUserDailies(userIds: Seq[ObjectId]): Future[Seq[Daily]] = { + // The syntax userIds: _* is used to convert the List of ObjectId instances to a variable-length argument list + val userFilter = Filters.in("user_id", userIds: _*) + val documents: Future[Seq[Document]] = find(dailiesCollection, userFilter) + + documents.map(document => { + println(documents) + 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. *