aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiIntervention.scala
blob: 39e00005bafd6c6e6eb22d5c6672a76b777c9e5a (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
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
    )
  }
}