FoodController.scala 4.5 KB

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