diff options
author | Jakob Odersky <jakob@odersky.com> | 2017-08-14 22:29:45 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-14 22:29:45 -0700 |
commit | 322ea28ecf5ad5f65d3376f3e97e004d229d4736 (patch) | |
tree | c405d10d70f4ec1f18ffa81bc01cd8da64bddcba /src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/documentissue.scala | |
parent | be74ae7c5531af998d38b9de8052791f17b25341 (diff) | |
parent | 442579b27ccbac82cb001a5b02402a593d005977 (diff) | |
download | rest-query-322ea28ecf5ad5f65d3376f3e97e004d229d4736.tar.gz rest-query-322ea28ecf5ad5f65d3376f3e97e004d229d4736.tar.bz2 rest-query-322ea28ecf5ad5f65d3376f3e97e004d229d4736.zip |
Merge pull request #18 from drivergroup/PDSUI-2188
PDSUI-2188 Create spray json formats for domain entities
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/documentissue.scala')
-rw-r--r-- | src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/documentissue.scala | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/documentissue.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/documentissue.scala new file mode 100644 index 0000000..28b2a5e --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/documentissue.scala @@ -0,0 +1,77 @@ +package xyz.driver.pdsuidomain.formats.json.sprayformats + +import java.time.LocalDateTime + +import spray.json._ +import xyz.driver.pdsuicommon.domain.{LongId, StringId, User} +import xyz.driver.pdsuidomain.entities._ + +object documentissue { + import DefaultJsonProtocol._ + import common._ + + def applyUpdateToDocumentIssue(json: JsValue, orig: DocumentIssue): DocumentIssue = json match { + case JsObject(fields) => + val text = fields + .get("text") + .map(_.convertTo[String]) + .getOrElse(deserializationError(s"DocumentIssue json object does not contain `text` field: $json")) + + val archiveRequired = fields + .get("archiveRequired") + .map(_.convertTo[Boolean]) + .getOrElse(deserializationError(s"DocumentIssue json object does not contain `archiveRequired` field: $json")) + + val startPage = fields.get("startPage").map(_.convertTo[Double]) + val endPage = fields.get("endPage").map(_.convertTo[Double]) + + orig.copy( + text = text, + archiveRequired = archiveRequired, + startPage = startPage, + endPage = endPage + ) + + case _ => deserializationError(s"Expected Json Object as partial DocumentIssue, but got $json") + + } + + def jsValueToDocumentIssue(json: JsValue, documentId: LongId[Document], userId: StringId[User]): DocumentIssue = + json match { + case JsObject(fields) => + val text = fields + .get("text") + .map(_.convertTo[String]) + .getOrElse(deserializationError(s"DocumentIssue json object does not contain `text` field: $json")) + + val startPage = fields.get("startPage").map(_.convertTo[Double]) + val endPage = fields.get("endPage").map(_.convertTo[Double]) + DocumentIssue( + id = LongId(0), + userId = userId, + documentId = documentId, + lastUpdate = LocalDateTime.MIN, + isDraft = true, + text = text, + archiveRequired = false, + startPage = startPage, + endPage = endPage + ) + + case _ => deserializationError(s"Expected Json Object as DocumentIssue, but got $json") + } + + implicit val documentIssueWriter = new JsonWriter[DocumentIssue] { + override def write(obj: DocumentIssue) = JsObject( + "id" -> obj.id.toJson, + "startPage" -> obj.startPage.toJson, + "endPage" -> obj.endPage.toJson, + "text" -> obj.text.toJson, + "lastUpdate" -> obj.lastUpdate.toJson, + "userId" -> obj.userId.toJson, + "isDraft" -> obj.isDraft.toJson, + "archiveRequired" -> obj.archiveRequired.toJson + ) + } + +} |