aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/formats/json/documentissue/ApiPartialDocumentIssue.scala
blob: 7a5dbe5a993b147b8e767bb4de023d9aeed34de8 (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
package xyz.driver.pdsuidomain.formats.json.documentissue

import java.time.LocalDateTime

import play.api.libs.functional.syntax._
import play.api.libs.json._
import xyz.driver.pdsuicommon.domain.{LongId, StringId, User}
import xyz.driver.pdsuidomain.entities.{Document, DocumentIssue}

final case class ApiPartialDocumentIssue(startPage: Option[Double],
                                         endPage: Option[Double],
                                         text: String,
                                         evidence: String,
                                         archiveRequired: Boolean,
                                         meta: String) {
  def applyTo(x: DocumentIssue): DocumentIssue = x.copy(
    startPage = startPage,
    endPage = endPage,
    text = text,
    evidence = evidence,
    archiveRequired = archiveRequired,
    meta = meta
  )

  def toDomain(userId: StringId[User], documentId: LongId[Document]) =
    DocumentIssue(
      id = LongId(0),
      userId = userId,
      documentId = documentId,
      startPage = startPage,
      endPage = endPage,
      lastUpdate = LocalDateTime.MIN,
      isDraft = true,
      text = text,
      evidence = evidence,
      archiveRequired = false,
      meta = meta
    )
}

object ApiPartialDocumentIssue {
  implicit val format: Format[ApiPartialDocumentIssue] = (
    (JsPath \ "startPage").formatNullable[Double] and
      (JsPath \ "endPage").formatNullable[Double] and
      (JsPath \ "text").format[String] and
      (JsPath \ "evidence").format[String] and
      (JsPath \ "archiveRequired").format[Boolean] and
      (JsPath \ "meta").format[String](Format(Reads { x =>
        JsSuccess(Json.stringify(x))
      }, Writes[String](Json.parse)))
  )(ApiPartialDocumentIssue.apply, unlift(ApiPartialDocumentIssue.unapply))
}