Skip to content
Snippets Groups Projects
Commit f351ef93 authored by Felipe D'Abrantes's avatar Felipe D'Abrantes
Browse files

Add questionId parameter to getUserFeed request (#32)

parent 9c8ae0ec
No related branches found
No related tags found
1 merge request!19Fix feed endpoint issues
......@@ -60,13 +60,13 @@ class DailyController @Inject()(val controllerComponents: ControllerComponents,
*
* @param userId The ID of the user to get the feed for.
*/
def getUserFeed(userId: String) = authenticatedUserAction { implicit request: AuthenticationRequest[AnyContent] =>
def getUserFeed(userId: String, questionId: String) = authenticatedUserAction { implicit request: AuthenticationRequest[AnyContent] =>
println("DailyController:getUserFeed")
try {
if (!ObjectId.isValid(userId)) throw new InvalidRequestBodyException("Invalid query parameter ID format: userId")
val result: Seq[Daily] = Daily.getUserFeedAsync(new ObjectId(userId), request.jwt)
val result: Seq[Daily] = Daily.getUserFeedAsync(new ObjectId(userId), new ObjectId(questionId), request.jwt)
val jsonResult: JsValue = Daily.toJson(result)
Ok(jsonResult)
} catch {
......
......@@ -17,7 +17,7 @@ import play.api.libs.json.{Json, JsValue, JsString, JsObject, JsArray}
import org.bson.{BsonWriter, BsonReader, BsonType}
import org.bson.codecs.{Codec, EncoderContext, DecoderContext}
import org.bson.conversions.Bson
import org.mongodb.scala.model.Updates
import org.mongodb.scala.model.{Updates, Filters}
case class Daily(
id: Option[ObjectId],
......@@ -54,11 +54,17 @@ object Daily {
Await.result[Seq[Daily]](future, timeout.seconds)
}
def getUserFeedAsync(userId: ObjectId, jwt: String, timeout: Int = 4): Seq[Daily] = {
def getUserFeedAsync(userId: ObjectId, questionId: ObjectId, jwt: String, 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, jwt)
feed: Seq[Daily] <- dailyRepo.getByValues[ObjectId]("user_id", friends)
feed: Seq[Daily] <- {
val friendsFilter: Bson = Filters.in("user_id", friends: _*)
val questionFilter: Bson = Filters.eq("question_id", questionId)
val filters: Bson = Filters.and(friendsFilter, questionFilter)
dailyRepo.getAll(Some(filters), None, None, None)
}
} yield feed
Await.result[Seq[Daily]](result, timeout.seconds)
......
......@@ -13,7 +13,7 @@ POST /daily/create controllers.DailyController.create()
GET /user/dailies controllers.DailyController.getUserDailies(userId: String)
GET /feed controllers.DailyController.getUserFeed(userId: String)
GET /feed controllers.DailyController.getUserFeed(userId: String, questionId: String)
PUT /daily/like controllers.DailyController.like()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment