aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala
diff options
context:
space:
mode:
authorKseniya Tomskikh <ktomskih@datamonsters.co>2017-08-04 17:57:12 +0600
committerKseniya Tomskikh <ktomskih@datamonsters.co>2017-08-11 14:41:17 +0600
commitef9517e1b8f599fbdd15c474cf7dfea61e803c2f (patch)
treec31a139d35dab0c10c70bb7afef2bca8e48f0fee /src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala
parent5519c219e2404cb19b6116dee90b40b5e5e2a720 (diff)
downloadrest-query-ef9517e1b8f599fbdd15c474cf7dfea61e803c2f.tar.gz
rest-query-ef9517e1b8f599fbdd15c474cf7dfea61e803c2f.tar.bz2
rest-query-ef9517e1b8f599fbdd15c474cf7dfea61e803c2f.zip
PDSUI-2188 Created and fixed spray json formats for tric and rep
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala')
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala
new file mode 100644
index 0000000..a8ce950
--- /dev/null
+++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala
@@ -0,0 +1,60 @@
+package xyz.driver.pdsuidomain.formats.json.sprayformats
+
+import spray.json._
+import xyz.driver.pdsuicommon.domain.LongId
+import xyz.driver.pdsuidomain.entities._
+
+object intervention {
+ import DefaultJsonProtocol._
+ import common._
+
+ implicit val interventionWriter: JsonWriter[InterventionWithArms] = new JsonWriter[InterventionWithArms] {
+ override def write(obj: InterventionWithArms) =
+ JsObject(
+ "id" -> obj.intervention.id.toJson,
+ "name" -> obj.intervention.name.toJson,
+ "typeId" -> obj.intervention.typeId.toJson,
+ "description" -> obj.intervention.description.toJson,
+ "isActive" -> obj.intervention.isActive.toJson,
+ "arms" -> obj.arms.map(_.armId).toJson,
+ "trialId" -> obj.intervention.trialId.toJson,
+ "originalName" -> obj.intervention.originalName.toJson,
+ "originalDescription" -> obj.intervention.originalDescription.toJson,
+ "originalType" -> obj.intervention.originalType.toJson
+ )
+ }
+
+ def applyUpdateToInterventionWithArms(json: JsValue, orig: InterventionWithArms): InterventionWithArms = json match {
+ case JsObject(fields) =>
+ val typeId = fields
+ .get("typeId")
+ .map(_.convertTo[LongId[InterventionType]])
+
+ val description = fields
+ .get("description")
+ .map(_.convertTo[String])
+
+ val isActive = fields
+ .get("isActive")
+ .map(_.convertTo[Boolean])
+
+ val origIntervention = orig.intervention
+ val arms = fields
+ .get("arms")
+ .map(_.convertTo[List[LongId[Arm]]].map(x => InterventionArm(x, orig.intervention.id)))
+
+ orig.copy(
+ intervention = origIntervention.copy(
+ typeId = typeId.orElse(origIntervention.typeId),
+ description = description.getOrElse(origIntervention.description),
+ isActive = isActive.getOrElse(origIntervention.isActive)
+ ),
+ arms = arms.getOrElse(orig.arms)
+ )
+
+ case _ => deserializationError(s"Expected Json Object as partial Intervention, but got $json")
+ }
+
+ implicit val interventionTypeFormat: RootJsonFormat[InterventionType] = jsonFormat2(InterventionType.apply)
+
+}