aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/formats/json/trial/ApiPartialTrial.scala
blob: 0dc1446d4009683b82243d8ebb6f4c638edf27d5 (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.trial

import java.util.UUID

import xyz.driver.pdsuicommon.domain.{LongId, UuidId}
import xyz.driver.pdsuidomain.entities.Trial
import org.davidbild.tristate.Tristate
import org.davidbild.tristate.contrib.play.ToJsPathOpsFromJsPath
import play.api.libs.functional.syntax._
import play.api.libs.json._

final case class ApiPartialTrial(hypothesisId: Tristate[UUID],
                                 studyDesignId: Tristate[Long],
                                 overview: Tristate[String],
                                 title: Tristate[String]) {

  def applyTo(orig: Trial): Trial = {
    orig.copy(
      hypothesisId = hypothesisId.map(UuidId(_)).cata(Some(_), None, orig.hypothesisId),
      studyDesignId = studyDesignId.map(LongId(_)).cata(Some(_), None, orig.studyDesignId),
      overview = overview.cata(Some(_), None, orig.overview),
      title = title.cata(Some(_).getOrElse(""), "", orig.title)
    )
  }
}

object ApiPartialTrial {

  private val reads: Reads[ApiPartialTrial] = (
    (JsPath \ "hypothesisId").readTristate[UUID] and
      (JsPath \ "studyDesignId").readTristate[Long] and
      (JsPath \ "overview").readTristate[String] and
      (JsPath \ "title").readTristate[String]
    ) (ApiPartialTrial.apply _)

  private val writes: Writes[ApiPartialTrial] = (
    (JsPath \ "hypothesisId").writeTristate[UUID] and
      (JsPath \ "studyDesignId").writeTristate[Long] and
      (JsPath \ "overview").writeTristate[String] and
      (JsPath \ "title").writeTristate[String]
    ) (unlift(ApiPartialTrial.unapply))

  implicit val format: Format[ApiPartialTrial] = Format(reads, writes)
}