diff options
author | Aleksandr <ognelisar@gmail.com> | 2017-12-06 15:05:54 +0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-06 15:05:54 +0700 |
commit | 51d5d1ddba83f539405c6d395933e1f8a456c871 (patch) | |
tree | 21cb0ee82567e72ba8314e19f896a62407932def /src/main/scala/xyz/driver/pdsuidomain/formats | |
parent | ed8db038b3b74e0089fe3415d4e3df5ceb882d86 (diff) | |
parent | 4d07f7cdb4df601398f982dd1df895304c554566 (diff) | |
download | rest-query-51d5d1ddba83f539405c6d395933e1f8a456c871.tar.gz rest-query-51d5d1ddba83f539405c6d395933e1f8a456c871.tar.bz2 rest-query-51d5d1ddba83f539405c6d395933e1f8a456c871.zip |
Merge pull request #72 from drivergroup/PDSUI-2396
PDSUI-2396 Fixed RestTrialService singleAction method
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats')
-rw-r--r-- | src/main/scala/xyz/driver/pdsuidomain/formats/json/trialissue.scala | 88 |
1 files changed, 80 insertions, 8 deletions
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/trialissue.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/trialissue.scala index f9bb0b6..79882c2 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/trialissue.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/trialissue.scala @@ -2,16 +2,26 @@ package xyz.driver.pdsuidomain.formats.json import java.time.LocalDateTime -import spray.json._ +import spray.json.{RootJsonReader, _} +import xyz.driver.core.{Id, auth} import xyz.driver.core.auth.User import xyz.driver.core.json._ import xyz.driver.pdsuicommon.domain.{LongId, StringId} import xyz.driver.pdsuidomain.entities._ object trialissue { + import DefaultJsonProtocol._ import common._ + private def deserializationErrorFieldMessage(field: String, json: JsValue)(implicit className: String) = { + deserializationError(s"$className json object do not contain '$field' field: $json") + } + + private def deserializationErrorEntityMessage(json: JsValue)(implicit className: String) = { + deserializationError(s"Expected Json Object as $className, but got $json") + } + def applyUpdateToTrialIssue(json: JsValue, orig: TrialIssue): TrialIssue = { json.asJsObject.getFields("text", "evidence", "archiveRequired", "meta") match { case Seq(text, evidence, archiveRequired, meta) => @@ -48,15 +58,77 @@ object trialissue { implicit val trialIssueWriter = new RootJsonWriter[TrialIssue] { override def write(obj: TrialIssue) = JsObject( - "id" -> obj.id.toJson, - "text" -> obj.text.toJson, - "lastUpdate" -> obj.lastUpdate.toJson, - "userId" -> obj.userId.toJson, - "isDraft" -> obj.isDraft.toJson, - "evidence" -> obj.evidence.toJson, + "id" -> obj.id.toJson, + "text" -> obj.text.toJson, + "lastUpdate" -> obj.lastUpdate.toJson, + "userId" -> obj.userId.toJson, + "isDraft" -> obj.isDraft.toJson, + "evidence" -> obj.evidence.toJson, "archiveRequired" -> obj.archiveRequired.toJson, - "meta" -> obj.meta.toJson + "meta" -> obj.meta.toJson ) } + def trialIssueReader(trialId: StringId[Trial]): RootJsonReader[TrialIssue] = + new RootJsonReader[TrialIssue] { + implicit val className: String = "TrialIssue" + + override def read(json: JsValue): TrialIssue = json match { + case JsObject(fields) => + val id = fields + .get("id") + .map(_.convertTo[LongId[TrialIssue]]) + .getOrElse(deserializationErrorFieldMessage("id", json)) + + val text = fields + .get("text") + .map(_.convertTo[String]) + .getOrElse(deserializationErrorFieldMessage("text", json)) + + val lastUpdate = fields + .get("lastUpdate") + .map(_.convertTo[LocalDateTime]) + .getOrElse(deserializationErrorFieldMessage("lastUpdate", json)) + + val userId = fields + .get("userId") + .map(_.convertTo[Id[auth.User]]) + .getOrElse(deserializationErrorFieldMessage("userId", json)) + + val isDraft = fields + .get("isDraft") + .map(_.convertTo[Boolean]) + .getOrElse(deserializationErrorFieldMessage("isDraft", json)) + + val evidence = fields + .get("evidence") + .map(_.convertTo[String]) + .getOrElse(deserializationErrorFieldMessage("evidence", json)) + + val archiveRequired = fields + .get("archiveRequired") + .map(_.convertTo[Boolean]) + .getOrElse(deserializationErrorFieldMessage("archiveRequired", json)) + + val meta = fields + .get("meta") + .map(_.convertTo[String]) + .getOrElse(deserializationErrorFieldMessage("meta", json)) + + TrialIssue( + id = id, + userId = userId, + trialId = trialId, + lastUpdate = lastUpdate, + isDraft = isDraft, + text = text, + evidence = evidence, + archiveRequired = archiveRequired, + meta = meta + ) + + case _ => deserializationErrorEntityMessage(json) + } + } + } |