-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReceiversReviewController.scala
83 lines (72 loc) · 2.6 KB
/
ReceiversReviewController.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package controllers
import play.api.mvc._
import play.api.libs.json._
import securesocial._
import models._
import models.reactive._
import dao.ReceiversReviewDAO
import play.modules.reactivemongo.MongoController
import play.modules.reactivemongo.json.collection.JSONCollection
import ModelsJsonFormats._
import securesocial.core.SecuredRequest
object ReceiversReviewController extends Controller with Security with MongoController with securesocial.core.SecureSocial {
/*
implicit val receiversReviewReads = (
(__ \ "_id").read(BSONObjectID.generate) and
(__ \ "sharingId").read(new BSONObjectID(Array[Byte](0))) and
(__ \ "experience").read[String] and
(__ \ "communication").read[Int] and
(__ \ "care").read[Int] and
(__ \ "punctual").read[Int] and
(__ \ "recommended").read[Boolean]
)(ReceiversReview)
*/
def receiversReviewCollection: JSONCollection = db.collection[JSONCollection]("ReceiversReviews")
def sharingCollection: JSONCollection = db.collection[JSONCollection]("Sharings")
def create() = AuthorizedAction(true, parse.json, Role.GUEST) {
user => implicit request =>
request.body.validate[ReceiversReview].map {
case review => {
withSharing(review.sharingId.toString) {
sharing => asGiver(sharing, user) {
Async {
ReceiversReviewDAO.insert(review).map {
case _ => Ok("Created Review id: " + review._id)
}
}
}
}
}
}.recoverTotal {
e => BadRequest("error: " + JsError.toFlatJson(e))
}
}
private def withReview(reviewId: String)(f: ReceiversReview => Result) = {
Async {
receiversReviewCollection.find(Json.obj("_id" -> reviewId)).cursor[ReceiversReview].headOption.map {
case None => NotFound("Not found")
case Some(review) => f(review)
}
}
}
private def withSharing(sharingId: String)(f: Sharing => Result) = {
Async {
sharingCollection.find(Json.obj("_id" -> sharingId)).cursor[Sharing].headOption.map {
case None => NotFound("Not found")
case Some(sharing) => f(sharing)
}
}
}
private def asGiver(sharing: Sharing, user: User)(f: => Result) = {
if (sharing.giver._id.toString == user._id.toString) f
else Forbidden("Not allowed")
}
private def isNotReviewed(sharingId: String)(f: => Result) = {
Async {
receiversReviewCollection.find(Json.obj("sharingId" -> sharingId)).cursor[ReceiversReview].headOption.map {
case None => f
case _ => Forbidden("Already reviewed")
}
}
}
}