diff options
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention')
3 files changed, 105 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiIntervention.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiIntervention.scala new file mode 100644 index 0000000..37a9758 --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiIntervention.scala @@ -0,0 +1,41 @@ +package xyz.driver.pdsuidomain.formats.json.intervention + +import xyz.driver.pdsuidomain.entities.InterventionWithArms +import play.api.libs.functional.syntax._ +import play.api.libs.json.{Format, JsPath} + +final case class ApiIntervention(id: Long, + name: String, + typeId: Option[Long], + description: String, + isActive: Boolean, + arms: List[Long], + trialId: String) + +object ApiIntervention { + + implicit val format: Format[ApiIntervention] = ( + (JsPath \ "id").format[Long] and + (JsPath \ "name").format[String] and + (JsPath \ "typeId").formatNullable[Long] and + (JsPath \ "description").format[String] and + (JsPath \ "isActive").format[Boolean] and + (JsPath \ "arms").format[List[Long]] and + (JsPath \ "trialId").format[String] + ) (ApiIntervention.apply, unlift(ApiIntervention.unapply)) + + def fromDomain(interventionWithArms: InterventionWithArms): ApiIntervention = { + import interventionWithArms.intervention + import interventionWithArms.arms + + ApiIntervention( + id = intervention.id.id, + name = intervention.name, + typeId = intervention.typeId.map(_.id), + description = intervention.description, + isActive = intervention.isActive, + arms = arms.map(_.armId.id), + trialId = intervention.trialId.id + ) + } +} 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 new file mode 100644 index 0000000..ca444eb --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiInterventionType.scala @@ -0,0 +1,20 @@ +package xyz.driver.pdsuidomain.formats.json.intervention + +import play.api.libs.functional.syntax._ +import play.api.libs.json.{Format, JsPath} +import xyz.driver.pdsuidomain.entities.InterventionType + +final case class ApiInterventionType(id: Long, name: String) + +object ApiInterventionType { + + implicit val format: Format[ApiInterventionType] = ( + (JsPath \ "id").format[Long] and + (JsPath \ "name").format[String] + ) (ApiInterventionType.apply, unlift(ApiInterventionType.unapply)) + + def fromDomain(interventionType: InterventionType) = ApiInterventionType( + id = interventionType.id.id, + name = interventionType.name + ) +} diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiPartialIntervention.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiPartialIntervention.scala new file mode 100644 index 0000000..416237a --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiPartialIntervention.scala @@ -0,0 +1,44 @@ +package xyz.driver.pdsuidomain.formats.json.intervention + +import xyz.driver.pdsuicommon.domain.LongId +import xyz.driver.pdsuidomain.entities.{InterventionArm, InterventionWithArms} +import play.api.libs.functional.syntax._ +import play.api.libs.json._ + +final case class ApiPartialIntervention(typeId: Option[Long], + description: Option[String], + isActive: Option[Boolean], + arms: Option[List[Long]]) { + + def applyTo(orig: InterventionWithArms): InterventionWithArms = { + val origIntervention = orig.intervention + val draftArmList = arms.map(_.map(x => InterventionArm(LongId(x), orig.intervention.id))) + orig.copy( + intervention = origIntervention.copy( + typeId = typeId.map(LongId(_)).orElse(origIntervention.typeId), + description = description.getOrElse(origIntervention.description), + isActive = isActive.getOrElse(origIntervention.isActive) + ), + arms = draftArmList.getOrElse(orig.arms) + ) + } +} + +object ApiPartialIntervention { + + private val reads: Reads[ApiPartialIntervention] = ( + (JsPath \ "typeId").readNullable[Long] and + (JsPath \ "description").readNullable[String] and + (JsPath \ "isActive").readNullable[Boolean] and + (JsPath \ "arms").readNullable[List[Long]] + ) (ApiPartialIntervention.apply _) + + private val writes: Writes[ApiPartialIntervention] = ( + (JsPath \ "typeId").writeNullable[Long] and + (JsPath \ "description").writeNullable[String] and + (JsPath \ "isActive").writeNullable[Boolean] and + (JsPath \ "arms").writeNullable[List[Long]] + ) (unlift(ApiPartialIntervention.unapply)) + + implicit val format: Format[ApiPartialIntervention] = Format(reads, writes) +} |