|
|
@@ -1,6 +1,8 @@
|
|
|
package com.weEat.controllers
|
|
|
|
|
|
import com.weEat.models.{FoodNode => FoodNodeCollection}
|
|
|
+import com.weEat.models.{RecipeVersion => RecipeVersionCollection}
|
|
|
+import com.weEat.models.FoodNodeView
|
|
|
import com.weEat.services.MongoDBService
|
|
|
import com.weEat.shared.models._
|
|
|
import javax.inject.{Inject,Singleton}
|
|
|
@@ -27,7 +29,7 @@ class FoodController @Inject()(
|
|
|
|
|
|
def get(id: String) = Action.async
|
|
|
{ implicit request: Request[AnyContent] =>
|
|
|
- withCollection(FoodNodeCollection) { (collection) =>
|
|
|
+ withCollection(FoodNodeView) { (collection) =>
|
|
|
collection.find(equal("_id", new ObjectId(id)))
|
|
|
.first()
|
|
|
.toFuture()
|
|
|
@@ -41,7 +43,7 @@ class FoodController @Inject()(
|
|
|
|
|
|
def all() = Action.async
|
|
|
{ implicit request: Request[AnyContent] =>
|
|
|
- withCollection(FoodNodeCollection) { (collection) =>
|
|
|
+ withCollection(FoodNodeView) { (collection) =>
|
|
|
collection.find()
|
|
|
.toFuture()
|
|
|
.transform({
|
|
|
@@ -53,7 +55,7 @@ class FoodController @Inject()(
|
|
|
|
|
|
def query(q: String) = Action.async
|
|
|
{ implicit request: Request[AnyContent] =>
|
|
|
- withCollection(FoodNodeCollection) { (collection) =>
|
|
|
+ withCollection(FoodNodeView) { (collection) =>
|
|
|
collection.find(regex("name", q, "i"))
|
|
|
.toFuture()
|
|
|
.transform({
|
|
|
@@ -63,6 +65,14 @@ class FoodController @Inject()(
|
|
|
}.flatten
|
|
|
}
|
|
|
|
|
|
+ def getByFdcId(fdcId: Long): Future[Option[USDANodeId]] =
|
|
|
+ withCollection(FoodNodeView) { (collection) =>
|
|
|
+ collection.find(equal("fdcId", fdcId))
|
|
|
+ .first()
|
|
|
+ .toFuture()
|
|
|
+ .map((foodNode) => Option(foodNode.asInstanceOf[USDANodeId]))
|
|
|
+ }.flatten
|
|
|
+
|
|
|
// def getImage(foodId: String, idx: Int) = Action.async
|
|
|
// { implicit request: Request[AnyContent] =>
|
|
|
// withCollection(FoodImages) {collection =>
|
|
|
@@ -131,12 +141,11 @@ class FoodController @Inject()(
|
|
|
else None
|
|
|
}).getOrElse(request.authInfo.user.userId)
|
|
|
)
|
|
|
- withCollection(FoodNodeCollection) { (collection) =>
|
|
|
- collection.insertOne(food).map({ (res) =>
|
|
|
- val id = res.getInsertedId().asObjectId().getValue()
|
|
|
- Ok(Json.toJson(food.withId(id)))
|
|
|
- }).head()
|
|
|
- }.flatten
|
|
|
+
|
|
|
+ (food match {
|
|
|
+ case recipeNode: RecipeNodeId => _addRecipe(recipeNode)
|
|
|
+ case node => _addFood(node)
|
|
|
+ }).map((food) => Ok(Json.toJson(food.asInstanceOf[FoodNodeId])))
|
|
|
}
|
|
|
catch {
|
|
|
case _: JsResultException => Future.successful(
|
|
|
@@ -145,40 +154,43 @@ class FoodController @Inject()(
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private def _addRecipe(node: RecipeNodeId) = {
|
|
|
+ val (metaNode, versNode) = RecipeMetaNodeId(node)
|
|
|
+ _addFood(metaNode, (_) => _saveVersionIfRecipe(versNode))
|
|
|
+ .map((_) => node)
|
|
|
+ }
|
|
|
+
|
|
|
+ private def _addFood(
|
|
|
+ node: FoodId,
|
|
|
+ callback: (FoodId) => Future[FoodId] = Future.successful _
|
|
|
+ ) = withCollection(FoodNodeCollection) { (collection) =>
|
|
|
+ collection.insertOne(node).map({ (res) => node }).head()
|
|
|
+ }.flatten
|
|
|
+ .flatMap(callback)
|
|
|
+
|
|
|
+ private def _saveVersionIfRecipe(node: RecipeNodeId): Future[FoodNodeId] =
|
|
|
+ withCollection(RecipeVersionCollection) { (collection) =>
|
|
|
+ collection.insertOne(node).map({ (res) => node }).head()
|
|
|
+ }.flatten
|
|
|
+
|
|
|
def update(id: String, uid: Option[String]) =
|
|
|
AuthorizedAction[Authorization](oauth)
|
|
|
.async(parse.json)
|
|
|
{ implicit request: AuthInfoRequest[JsValue, Authorization] =>
|
|
|
try {
|
|
|
- val refFood = if (request.authInfo.user.hasAdminPermissions)
|
|
|
- request.body.as[FoodNode]
|
|
|
- else
|
|
|
- request.body.as[FoodNode]
|
|
|
- .withId(new ObjectId(id), request.authInfo.user.userId)
|
|
|
-
|
|
|
- val food = refFood.withId(
|
|
|
+ val food = request.body.as[FoodNode].withId(
|
|
|
new ObjectId(id),
|
|
|
- uid.flatMap({ (id) =>
|
|
|
- if (request.authInfo.user.hasAdminPermissions) Some(new ObjectId(id))
|
|
|
+ uid.flatMap({ (uid) =>
|
|
|
+ if (request.authInfo.user.hasAdminPermissions) Some(new ObjectId(uid))
|
|
|
else None
|
|
|
// tflucke@[2023-10-07] Should this query for the current uid instead?
|
|
|
}).getOrElse(request.authInfo.user.userId)
|
|
|
)
|
|
|
- withCollection(FoodNodeCollection) { (collection) =>
|
|
|
- collection.replaceOne(refFood match {
|
|
|
- case fni: FoodNodeId => and(
|
|
|
- equal("_id", new ObjectId(id)),
|
|
|
- equal("uid", fni.uid)
|
|
|
- )
|
|
|
- case _ => equal("_id", new ObjectId(id))
|
|
|
- }, food)
|
|
|
- .map({ (res) => if (res.getModifiedCount > 0)
|
|
|
- Ok(Json.toJson(food))
|
|
|
- else
|
|
|
- NotFound(s"User ${request.authInfo.user.userId} does not have a food node $id")
|
|
|
- })
|
|
|
- .head()
|
|
|
- }.flatten
|
|
|
+
|
|
|
+ (food match {
|
|
|
+ case recipeNode: RecipeNodeId => _updateRecipe(recipeNode)
|
|
|
+ case node => _updateFood(node)
|
|
|
+ }).map((food) => Ok(Json.toJson(food.asInstanceOf[FoodNodeId])))
|
|
|
}
|
|
|
catch {
|
|
|
case _: JsResultException => Future.successful(
|
|
|
@@ -187,11 +199,27 @@ class FoodController @Inject()(
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- def getByFdcId(fdcId: Long): Future[Option[USDANodeId]] =
|
|
|
- withCollection(FoodNodeCollection) { (collection) =>
|
|
|
- collection.find(equal("fdcId", fdcId))
|
|
|
- .first()
|
|
|
- .toFuture()
|
|
|
- .map((foodNode) => Option(foodNode.asInstanceOf[USDANodeId]))
|
|
|
- }.flatten
|
|
|
+ private def _updateRecipe(node: RecipeNodeId) = {
|
|
|
+ val (metaNode, versNode) = RecipeMetaNodeId(node)
|
|
|
+ _updateFood(metaNode, (_) => _saveVersionIfRecipe(versNode))
|
|
|
+ .map((_) => node)
|
|
|
+ }
|
|
|
+
|
|
|
+ private def _updateFood(
|
|
|
+ node: FoodId,
|
|
|
+ callback: (FoodId) => Future[FoodId] = Future.successful _
|
|
|
+ ) = withCollection(FoodNodeCollection) { (collection) =>
|
|
|
+ collection.replaceOne(and(
|
|
|
+ equal("_id", node._id),
|
|
|
+ equal("uid", node.uid)
|
|
|
+ ), node)
|
|
|
+ .head()
|
|
|
+ .transform({
|
|
|
+ case Success(res) if (res.getModifiedCount > 0) => Success(node)
|
|
|
+ case Success(res) => Failure(new NoSuchElementException("User " +
|
|
|
+ s"${node.uid} does not have a food node ${node._id}"))
|
|
|
+ case Failure(e) => Failure(e)
|
|
|
+ })
|
|
|
+ }.flatten
|
|
|
+ .flatMap(callback)
|
|
|
}
|