aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/formats
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats')
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiInterventionType.scala18
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala33
2 files changed, 44 insertions, 7 deletions
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiInterventionType.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiInterventionType.scala
index ebef225..c28c5b4 100644
--- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiInterventionType.scala
+++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiInterventionType.scala
@@ -4,8 +4,9 @@ import play.api.libs.functional.syntax._
import play.api.libs.json.{Format, JsPath}
import xyz.driver.pdsuicommon.domain.LongId
import xyz.driver.pdsuidomain.entities.InterventionType
+import xyz.driver.pdsuidomain.entities.InterventionType.DeliveryMethod
-final case class ApiInterventionType(id: Long, name: String) {
+final case class ApiInterventionType(id: Long, name: String, deliveryMethods: List[String]) {
def toDomain = InterventionType(id = LongId[InterventionType](id), name = name)
}
@@ -14,11 +15,16 @@ object ApiInterventionType {
implicit val format: Format[ApiInterventionType] = (
(JsPath \ "id").format[Long] and
- (JsPath \ "name").format[String]
+ (JsPath \ "name").format[String] and
+ (JsPath \ "deliveryMethods").format[List[String]]
)(ApiInterventionType.apply, unlift(ApiInterventionType.unapply))
- def fromDomain(interventionType: InterventionType) = ApiInterventionType(
- id = interventionType.id.id,
- name = interventionType.name
- )
+ def fromDomain(interventionType: InterventionType) = {
+ val typeMethods = InterventionType.deliveryMethodGroups.getOrElse(interventionType.id, Set.empty[DeliveryMethod])
+ ApiInterventionType(
+ id = interventionType.id.id,
+ name = interventionType.name,
+ deliveryMethods = typeMethods.map(DeliveryMethod.methodToString).toList
+ )
+ }
}
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
+ )
+ }
+ }
}