aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/formats/json/message/ApiPartialMessage.scala
blob: a2656f255d42e6716b60d9a343acb87e0c2942f3 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package xyz.driver.pdsuidomain.formats.json.message

import java.time.LocalDateTime

import xyz.driver.pdsuicommon.domain._
import play.api.libs.functional.syntax._
import play.api.libs.json.{Format, JsPath}
import xyz.driver.pdsuidomain.entities.Message

final case class ApiPartialMessage(text: Option[String],
                                   recordId: Option[Long],
                                   documentId: Option[Long],
                                   patientId: Option[String],
                                   trialId: Option[String],
                                   startPage: Option[Double],
                                   endPage: Option[Double],
                                   evidence: Option[String],
                                   archiveRequired: Option[Boolean],
                                   meta: Option[String]) {

  def toDomain(userId: StringId[User]) = Message(
    id = LongId(0),
    text = text.getOrElse(""),
    userId = userId,
    isDraft = true,
    recordId = recordId.map(LongId(_)),
    documentId = documentId.map(LongId(_)),
    patientId = patientId.map(UuidId(_)),
    trialId = trialId.map(StringId(_)),
    startPage = startPage,
    endPage = endPage,
    evidence = evidence,
    archiveRequired = archiveRequired,
    meta = meta,
    lastUpdate = LocalDateTime.MIN
  )

  def applyTo(orig: Message): Message = {
    orig.copy(
      text = text.getOrElse(""),
      recordId = recordId.map(LongId(_)),
      documentId = documentId.map(LongId(_)),
      patientId = patientId.map(UuidId(_)),
      trialId = trialId.map(StringId(_)),
      startPage = startPage,
      endPage = endPage,
      evidence = evidence,
      archiveRequired = archiveRequired,
      meta = meta,
      lastUpdate = LocalDateTime.MIN
    )
  }
}

object ApiPartialMessage {

  implicit val format: Format[ApiPartialMessage] = (
    (JsPath \ "text").formatNullable[String] and
      (JsPath \ "recordId").formatNullable[Long] and
      (JsPath \ "documentId").formatNullable[Long] and
      (JsPath \ "patientId").formatNullable[String] and
      (JsPath \ "trialId").formatNullable[String] and
      (JsPath \ "startPage").formatNullable[Double] and
      (JsPath \ "endPage").formatNullable[Double] and
      (JsPath \ "evidence").formatNullable[String] and
      (JsPath \ "archiveRequired").formatNullable[Boolean] and
      (JsPath \ "meta").formatNullable[String]
  )(ApiPartialMessage.apply, unlift(ApiPartialMessage.unapply))

  def fromDomain(domain: Message) = ApiPartialMessage(
    text = Some(domain.text),
    recordId = domain.recordId.map(_.id),
    documentId = domain.documentId.map(_.id),
    patientId = domain.patientId.map(_.toString),
    trialId = domain.trialId.map(_.toString),
    startPage = domain.startPage,
    endPage = domain.endPage,
    evidence = domain.evidence,
    archiveRequired = domain.archiveRequired,
    meta = domain.meta
  )
}