aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiPartialIntervention.scala
blob: f67ba6bb7ea30bb567214438c7b1cbc8cf2a6c07 (plain) (blame)
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
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)
}