aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/patienthypothesis.scala
blob: 4f2783ca11a36e3c9d8eb2421b202dadd835ec5f (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
package xyz.driver.pdsuidomain.formats.json.sprayformats

import spray.json._
import xyz.driver.pdsuidomain.entities._

object patienthypothesis {
  import DefaultJsonProtocol._
  import common._

  def applyUpdateToPatientHypothesis(json: JsValue, orig: PatientHypothesis): PatientHypothesis = json match {
    case JsObject(fields) =>
      val rationale = if (fields.contains("rationale")) {
        fields.get("rationale").map(_.convertTo[String])
      } else orig.rationale

      orig.copy(rationale = rationale)

    case _ => deserializationError(s"Expected Json Object as partial PatientHypothesis, but got $json")
  }

  implicit val patientHypothesisWriter: JsonWriter[(PatientHypothesis, Boolean)] =
    new JsonWriter[(PatientHypothesis, Boolean)] {
      override def write(obj: (PatientHypothesis, Boolean)): JsValue = {
        val patientHypothesis   = obj._1
        val isRationaleRequired = obj._2
        JsObject(
          "id"                  -> patientHypothesis.id.toJson,
          "patientId"           -> patientHypothesis.patientId.toJson,
          "hypothesisId"        -> patientHypothesis.hypothesisId.toJson,
          "matchedTrials"       -> patientHypothesis.matchedTrials.toJson,
          "rationale"           -> patientHypothesis.rationale.toJson,
          "isRationaleRequired" -> isRationaleRequired.toJson
        )
      }
    }

}