diff options
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats/json/documentissue')
2 files changed, 100 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/documentissue/ApiDocumentIssue.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/documentissue/ApiDocumentIssue.scala new file mode 100644 index 0000000..fab2546 --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/documentissue/ApiDocumentIssue.scala @@ -0,0 +1,48 @@ +package xyz.driver.pdsuidomain.formats.json.documentissue + +import java.time.{ZoneId, ZonedDateTime} + +import play.api.libs.functional.syntax._ +import play.api.libs.json._ +import xyz.driver.pdsuidomain.entities.DocumentIssue + +final case class ApiDocumentIssue(id: Long, + startPage: Option[Double], + endPage: Option[Double], + text: String, + lastUpdate: ZonedDateTime, + userId: String, + isDraft: Boolean, + evidence: String, + archiveRequired: Boolean, + meta: String) + +object ApiDocumentIssue { + implicit val format: Format[ApiDocumentIssue] = ( + (JsPath \ "id").format[Long] and + (JsPath \ "startPage").formatNullable[Double] and + (JsPath \ "endPage").formatNullable[Double] and + (JsPath \ "text").format[String] and + (JsPath \ "lastUpdate").format[ZonedDateTime] and + (JsPath \ "userId").format[String] and + (JsPath \ "isDraft").format[Boolean] 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))) + )(ApiDocumentIssue.apply, unlift(ApiDocumentIssue.unapply)) + + def fromDomain(x: DocumentIssue) = ApiDocumentIssue( + id = x.id.id, + startPage = x.startPage, + endPage = x.endPage, + text = x.text, + lastUpdate = ZonedDateTime.of(x.lastUpdate, ZoneId.of("Z")), + userId = x.userId.id, + isDraft = x.isDraft, + evidence = x.evidence, + archiveRequired = x.archiveRequired, + meta = x.meta + ) +} diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/documentissue/ApiPartialDocumentIssue.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/documentissue/ApiPartialDocumentIssue.scala new file mode 100644 index 0000000..7a5dbe5 --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/documentissue/ApiPartialDocumentIssue.scala @@ -0,0 +1,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)) +} |