From 3a9ba980e6ce08361f6f3cdaf6ab2a401101b35c Mon Sep 17 00:00:00 2001 From: Kseniya Tomskikh Date: Wed, 27 Sep 2017 15:50:35 +0700 Subject: PDSUI-2275 Added delivery methods to intervention type entrypoint --- .../formats/json/sprayformats/intervention.scala | 33 +++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala') 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 index daa28e4..4bd5bad 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala @@ -2,6 +2,7 @@ package xyz.driver.pdsuidomain.formats.json.sprayformats import spray.json._ import xyz.driver.pdsuicommon.domain.{LongId, StringId} +import xyz.driver.pdsuidomain.entities.InterventionType.DeliveryMethod import xyz.driver.pdsuidomain.entities._ object intervention { @@ -119,6 +120,36 @@ object intervention { case _ => deserializationError(s"Expected Json Object as partial Intervention, but got $json") } - implicit val interventionTypeFormat: RootJsonFormat[InterventionType] = jsonFormat2(InterventionType.apply) + implicit val interventionTypeFormat: JsonFormat[InterventionType] = new RootJsonFormat[InterventionType] { + override def read(json: JsValue) = json match { + case JsObject(fields) => + val id = fields + .get("id") + .map(_.convertTo[LongId[InterventionType]]) + .getOrElse(deserializationError(s"Intervention type json object does not contain `id` field: $json")) + + val name = fields + .get("name") + .map(_.convertTo[String]) + .getOrElse(deserializationError(s"Intervention type json object does not contain `name` field: $json")) + + InterventionType(id, name) + + case _ => deserializationError(s"Expected Json Object as Intervention type, but got $json") + } + + override def write(obj: InterventionType) = { + val typeMethods = InterventionType.deliveryMethodGroups + .getOrElse(obj.id, Set.empty[DeliveryMethod]) + .map(DeliveryMethod.methodToString) + .toList + + JsObject( + "id" -> obj.id.toJson, + "name" -> obj.name.toJson, + "deliveryMethods" -> typeMethods.toJson + ) + } + } } -- cgit v1.2.3