aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/formats/json/export/ApiExportTrialWithLabels.scala
blob: e383a1f63af13906ef9c6ee943faf3a094ada4c2 (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
45
46
47
48
49
50
51
52
53
package xyz.driver.pdsuidomain.formats.json.export

import java.time.{Instant, ZoneId}

import play.api.libs.functional.syntax._
import play.api.libs.json.{Format, JsPath}
import xyz.driver.pdsuicommon.domain.{StringId, UuidId}
import xyz.driver.pdsuidomain.entities.Trial
import xyz.driver.pdsuidomain.entities.export.trial.ExportTrialWithLabels

final case class ApiExportTrialWithLabels(nctId: String,
                                          trialId: String,
                                          condition: String,
                                          lastReviewed: Long,
                                          labelVersion: Long,
                                          arms: List[ApiExportTrialArm],
                                          criteria: List[ApiExportTrialLabelCriterion]) {

  def toDomain: ExportTrialWithLabels = {
    ExportTrialWithLabels(
      StringId[Trial](nctId),
      UuidId[Trial](trialId),
      condition,
      lastReviewed = Instant.ofEpochMilli(lastReviewed).atZone(ZoneId.of("Z")).toLocalDateTime,
      labelVersion,
      arms.map(_.toDomain),
      criteria.map(_.toDomain)
    )
  }
}

object ApiExportTrialWithLabels {

  implicit val format: Format[ApiExportTrialWithLabels] = (
    (JsPath \ "nctId").format[String] and
      (JsPath \ "trialId").format[String] and
      (JsPath \ "disease").format[String] and
      (JsPath \ "lastReviewed").format[Long] and
      (JsPath \ "labelVersion").format[Long] and
      (JsPath \ "arms").format[List[ApiExportTrialArm]] and
      (JsPath \ "criteria").format[List[ApiExportTrialLabelCriterion]]
  )(ApiExportTrialWithLabels.apply, unlift(ApiExportTrialWithLabels.unapply))

  def fromDomain(x: ExportTrialWithLabels) = ApiExportTrialWithLabels(
    nctId = x.nctId.id,
    trialId = x.trialId.toString,
    condition = x.condition,
    lastReviewed = x.lastReviewed.atZone(ZoneId.of("Z")).toEpochSecond,
    labelVersion = x.labelVersion,
    arms = x.arms.map(ApiExportTrialArm.fromDomain),
    criteria = x.criteria.map(ApiExportTrialLabelCriterion.fromDomain)
  )
}