FoodController.scala 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.weEat.controllers
  2. import com.weEat.models.{FoodNode => FoodNodeCollection}
  3. import com.weEat.services.MongoDBService
  4. import com.weEat.shared.models._
  5. import com.weEat.shared.models.UnitType._
  6. import javax.inject.{Inject,Singleton}
  7. import play.api._
  8. import play.api.libs.json._
  9. import play.api.mvc._
  10. import org.bson.types.ObjectId
  11. import org.mongodb.scala.model.Filters._
  12. import org.mongodb.scala.model.Updates._
  13. import scala.concurrent.{ExecutionContext,Future}
  14. import scala.util.{Success,Failure}
  15. import com.weEat.models.Authorization
  16. import scalaoauth2.provider.{AuthInfoRequest,OAuth2ProviderActionBuilders}
  17. import com.weEat.services.OAuth2Service
  18. @Singleton
  19. class FoodController @Inject()(
  20. val controllerComponents: ControllerComponents,
  21. oauth: OAuth2Service,
  22. db: MongoDBService
  23. ) extends BaseController
  24. with OAuth2ProviderActionBuilders {
  25. implicit val ec = scala.concurrent.ExecutionContext.global
  26. import db.withCollection
  27. def get(id: String) = Action.async
  28. { implicit request: Request[AnyContent] =>
  29. withCollection(FoodNodeCollection) {collection =>
  30. collection.find(equal("id", id))
  31. .first()
  32. .toFuture()
  33. .transform({
  34. case Success(x) => Success(Ok(Json.toJson(x)))
  35. case Failure(x) => throw x
  36. })
  37. }.flatten
  38. }
  39. def all() = Action.async
  40. { implicit request: Request[AnyContent] =>
  41. withCollection(FoodNodeCollection) {collection =>
  42. collection.find()
  43. .toFuture()
  44. .transform({
  45. case Success(x) => Success(Ok(Json.toJson(x)))
  46. case Failure(x) => throw x
  47. })
  48. }.flatten
  49. }
  50. def query(q: String) = Action.async
  51. { implicit request: Request[AnyContent] =>
  52. withCollection(FoodNodeCollection) {collection =>
  53. collection.find(regex("name", s".*$q.*"))
  54. .toFuture()
  55. .transform({
  56. case Success(x) => Success(Ok(Json.toJson(x)))
  57. case Failure(x) => throw x
  58. })
  59. }.flatten
  60. }
  61. // def getImage(foodId: String, idx: Int) = Action.async
  62. // { implicit request: Request[AnyContent] =>
  63. // withCollection(FoodImages) {collection =>
  64. // collection.find(and(equal("food", foodId), equal("index", idx)))
  65. // .first()
  66. // .toFuture()
  67. // .transform({
  68. // case Success(img) =>
  69. // Success(Ok.streamed(img.data, img.data.length, img.mime))
  70. // case Failure(x) => throw x
  71. // })
  72. // }.flatten
  73. // ???
  74. // }
  75. // def addImageTo(id: String) = Action.async(parse.byteString)
  76. // { implicit request: Request[ByteString] =>
  77. // withCollection(FoodImages) { images =>
  78. // images.insertOne(FoodImage(None, id, , request.body, request.contentType.get))
  79. // .head().transformWith({
  80. // case Success(img) => withCollection(FoodNode) { foods =>
  81. // foods.findOneAndUpdate(
  82. // equal("id", id),
  83. // push("imageIds", img._id.get)
  84. // ).head().transform({
  85. // case Success(x) => Success(Ok(x))
  86. // case Failure(x) =>
  87. // images.deleteOne(eq("_id", img._id.get))
  88. // throw x
  89. // })
  90. // }
  91. // case Failure(x) => throw x
  92. // })
  93. // }.flatten
  94. // ???
  95. // }
  96. // def deleteImage(foodId: String, imageId: String) = Action.async
  97. // { implicit request: Request[AnyContent] =>
  98. // withCollection(FoodNode) { foods =>
  99. // foods.findOneAndUpdate(
  100. // equal("id", foodId),
  101. // pull("imageIds", imageId)
  102. // ).head().transformWith({
  103. // case Success(x) => images.deleteOne(eq("_id", img._id.get))
  104. // .head().transform({
  105. // case Success(x) => Success(Ok(x))
  106. // case Failure(x) => throw x
  107. // })
  108. // case Failure(x) => throw x
  109. // })
  110. // }.flatten
  111. // ???
  112. // }
  113. def add(uid: Option[String]) = AuthorizedAction[Authorization](oauth)
  114. .async(parse.json)
  115. { implicit request: AuthInfoRequest[JsValue, Authorization] =>
  116. try {
  117. val food = request.body.as[FoodNode].withId(
  118. new ObjectId,
  119. uid.flatMap({ id =>
  120. if (request.authInfo.user.hasAdminPermissions) Some(new ObjectId(id))
  121. else None
  122. }).getOrElse(request.authInfo.user.userId)
  123. )
  124. withCollection(FoodNodeCollection) {collection =>
  125. collection.insertOne(food).map({ res =>
  126. val id = res.getInsertedId().asObjectId().getValue()
  127. Ok(Json.toJson(food.withId(id)))
  128. }).head()
  129. }.flatten
  130. }
  131. catch {
  132. case jsre: JsResultException => Future.successful(
  133. BadRequest(s"Could not parse json into a Food node.")
  134. )
  135. }
  136. }
  137. }