diff options
author | Kseniya Tomskikh <ktomskih@datamonsters.co> | 2017-10-20 12:56:34 +0700 |
---|---|---|
committer | Kseniya Tomskikh <ktomskih@datamonsters.co> | 2017-10-20 12:56:34 +0700 |
commit | 081f6c2ec58268a3b84aa69d622b28562a8dc4ac (patch) | |
tree | b3d2181c1ac696ba53ecc54df0b73b3535d23671 /src/main/scala/xyz/driver/pdsuidomain/formats/json | |
parent | f3afdc358e52e2399bb4b089d26e3e3521570872 (diff) | |
parent | 9ca0cae09bc880ce083c2a49ae3804889cc9d4c4 (diff) | |
download | rest-query-081f6c2ec58268a3b84aa69d622b28562a8dc4ac.tar.gz rest-query-081f6c2ec58268a3b84aa69d622b28562a8dc4ac.tar.bz2 rest-query-081f6c2ec58268a3b84aa69d622b28562a8dc4ac.zip |
Merge branch 'master' into entity-list-responses
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats/json')
7 files changed, 41 insertions, 15 deletions
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiDocument.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiDocument.scala index 3f1ecfe..250e650 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiDocument.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiDocument.scala @@ -25,7 +25,8 @@ final case class ApiDocument(id: Long, assignee: Option[String], previousAssignee: Option[String], lastActiveUser: Option[String], - meta: Option[String]) { + meta: Option[String], + labelVersion: Int) { private def extractStatus(status: String): Document.Status = Document.Status.fromString(status).getOrElse(throw new NoSuchElementException(s"Status $status unknown")) @@ -50,7 +51,8 @@ final case class ApiDocument(id: Long, meta = this.meta.map(x => TextJson(JsonSerializer.deserialize[Document.Meta](x))), startDate = this.startDate, endDate = this.endDate, - lastUpdate = this.lastUpdate.toLocalDateTime() + lastUpdate = this.lastUpdate.toLocalDateTime(), + labelVersion = this.labelVersion ) } @@ -83,7 +85,8 @@ object ApiDocument { (JsPath \ "lastActiveUser").formatNullable[String] and (JsPath \ "meta").formatNullable(Format(Reads { x => JsSuccess(Json.stringify(x)) - }, Writes[String](Json.parse))) + }, Writes[String](Json.parse))) and + (JsPath \ "labelVersion").format[Int] )(ApiDocument.apply, unlift(ApiDocument.unapply)) def fromDomain(document: Document): ApiDocument = { @@ -104,7 +107,8 @@ object ApiDocument { assignee = document.assignee.map(_.id), previousAssignee = document.previousAssignee.map(_.id), lastActiveUser = document.lastActiveUserId.map(_.id), - meta = document.meta.map(meta => JsonSerializer.serialize(meta.content)) + meta = document.meta.map(meta => JsonSerializer.serialize(meta.content)), + labelVersion = document.labelVersion ) } } diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiPartialDocument.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiPartialDocument.scala index 032ddf6..ecbdaed 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiPartialDocument.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiPartialDocument.scala @@ -47,7 +47,8 @@ final case class ApiPartialDocument(recordId: Option[Long], meta = meta.cata(x => Some(TextJson(JsonSerializer.deserialize[Meta](x))), None, orig.meta), startDate = startDate.cata(Some(_), None, orig.startDate), endDate = endDate.cata(Some(_), None, orig.endDate), - lastUpdate = LocalDateTime.MIN // Should update internally in a business logic module + lastUpdate = LocalDateTime.MIN, // Should update internally in a business logic module, + labelVersion = orig.labelVersion ) def toDomain: Try[Document] = Try { @@ -75,7 +76,8 @@ final case class ApiPartialDocument(recordId: Option[Long], assignee = None, previousAssignee = None, lastActiveUserId = None, - lastUpdate = LocalDateTime.MIN + lastUpdate = LocalDateTime.MIN, + labelVersion = 0 ) } else { throw new JsonValidationException(validationErrors) diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/document.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/document.scala index e05e841..c584b96 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/document.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/document.scala @@ -34,7 +34,7 @@ object document { val name = fields .get("name") .map(_.convertTo[String]) - .getOrElse(deserializationError(s"Intervention type json object does not contain `name` field: $json")) + .getOrElse(deserializationError(s"Document type json object does not contain `name` field: $json")) DocumentType .fromString(name) @@ -126,7 +126,8 @@ object document { "previousAssignee" -> document.previousAssignee.toJson, "meta" -> document.meta.toJson, "lastActiveUser" -> document.lastActiveUserId.toJson, - "lastUpdate" -> document.lastUpdate.toJson + "lastUpdate" -> document.lastUpdate.toJson, + "labelVersion" -> document.labelVersion.toJson ) override def read(json: JsValue): Document = json match { @@ -185,7 +186,8 @@ object document { assignee = None, previousAssignee = None, lastActiveUserId = None, - lastUpdate = LocalDateTime.MIN + lastUpdate = LocalDateTime.MIN, + labelVersion = 0 ) case _ => deserializationError(s"Expected Json Object as Document, but got $json") diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala index bd68566..e557e47 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala @@ -128,7 +128,9 @@ object intervention { .map(_.convertTo[String]) .getOrElse(deserializationError(s"Intervention type json object does not contain `name` field: $json")) - InterventionType.typeFromString(name) + InterventionType + .typeFromString(name) + .getOrElse(deserializationError(s"Unknown intervention type: $name")) case _ => deserializationError(s"Expected Json Object as Intervention type, but got $json") } diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/record.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/record.scala index eac5ea7..09517e6 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/record.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/record.scala @@ -42,13 +42,13 @@ object record { val name = fields .get("name") .map(_.convertTo[String]) - .getOrElse(deserializationError(s"Intervention type json object does not contain `name` field: $json")) + .getOrElse(deserializationError(s"Provider type json object does not contain `name` field: $json")) ProviderType .fromString(name) .getOrElse(deserializationError(s"Unknown provider type: $name")) - case _ => deserializationError(s"Expected Json Object as Intervention type, but got $json") + case _ => deserializationError(s"Expected Json Object as Provider type, but got $json") } override def write(obj: ProviderType) = diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/studydesign.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/studydesign.scala index e801666..3002b38 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/studydesign.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/studydesign.scala @@ -7,6 +7,23 @@ object studydesign { import DefaultJsonProtocol._ import common._ - implicit val studyDesignFormat: RootJsonFormat[StudyDesign] = jsonFormat2(StudyDesign.apply) + implicit val studyDesignFormat: RootJsonFormat[StudyDesign] = new RootJsonFormat[StudyDesign] { + override def read(json: JsValue): StudyDesign = json match { + case JsObject(fields) => + val name = fields + .get("name") + .map(_.convertTo[String]) + .getOrElse(deserializationError(s"Study design json object does not contain `name` field: $json")) + + StudyDesign + .fromString(name) + .getOrElse(deserializationError(s"Unknown study design: $name")) + + case _ => deserializationError(s"Expected Json Object as Study design, but got $json") + } + + override def write(obj: StudyDesign) = + JsObject("id" -> obj.id.toJson, "name" -> obj.name.toJson) + } } diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/studydesign/ApiStudyDesign.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/studydesign/ApiStudyDesign.scala index 1e9a226..12d40df 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/studydesign/ApiStudyDesign.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/studydesign/ApiStudyDesign.scala @@ -3,11 +3,10 @@ package xyz.driver.pdsuidomain.formats.json.studydesign import xyz.driver.pdsuidomain.entities.StudyDesign import play.api.libs.functional.syntax._ import play.api.libs.json.{Format, JsPath} -import xyz.driver.pdsuicommon.domain.LongId final case class ApiStudyDesign(id: Long, name: String) { - def toDomain = StudyDesign(id = LongId[StudyDesign](id), name = name) + def toDomain: Option[StudyDesign] = StudyDesign.fromString(name) } object ApiStudyDesign { |