diff options
author | vlad <vlad@driver.xyz> | 2017-06-27 17:13:02 -0700 |
---|---|---|
committer | vlad <vlad@driver.xyz> | 2017-06-27 17:13:02 -0700 |
commit | 5832f63b84d7388441d1200f2442dc1e9de0225c (patch) | |
tree | 32f63acdc920c14effc3d0d2822c05c125ad49e4 /src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiDocument.scala | |
parent | 9dd50590d4c8f8b9442d7c21ddd1def9dd453d5e (diff) | |
download | rest-query-5832f63b84d7388441d1200f2442dc1e9de0225c.tar.gz rest-query-5832f63b84d7388441d1200f2442dc1e9de0225c.tar.bz2 rest-query-5832f63b84d7388441d1200f2442dc1e9de0225c.zip |
All PDS UI domain models, API case classes, service traits and necessary utils moved to pdsui-commonv0.1.11
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiDocument.scala')
-rw-r--r-- | src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiDocument.scala | 71 |
1 files changed, 71 insertions, 0 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 new file mode 100644 index 0000000..be9c65b --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiDocument.scala @@ -0,0 +1,71 @@ +package xyz.driver.pdsuidomain.formats.json.document + +import java.time.{LocalDate, ZoneId, ZonedDateTime} + +import xyz.driver.pdsuidomain.entities._ +import play.api.data.validation.ValidationError +import play.api.libs.functional.syntax._ +import play.api.libs.json._ +import xyz.driver.pdsuicommon.json.JsonSerializer + +final case class ApiDocument(id: Long, + recordId: Long, + physician: Option[String], + lastUpdate: Option[ZonedDateTime], + typeId: Option[Long], + startDate: Option[LocalDate], + endDate: Option[LocalDate], + provider: Option[String], + providerTypeId: Option[Long], + status: Option[String], + previousStatus: Option[String], + assignee: Option[Long], + previousAssignee: Option[Long], + meta: Option[String]) + +object ApiDocument { + + private val statusFormat = Format( + Reads.StringReads.filter(ValidationError("unknown status")) { + case x if DocumentUtils.statusFromString.isDefinedAt(x) => true + case _ => false + }, + Writes.StringWrites + ) + + implicit val format: Format[ApiDocument] = ( + (JsPath \ "id").format[Long] and + (JsPath \ "recordId").format[Long] and + (JsPath \ "physician").formatNullable[String] and + (JsPath \ "lastUpdate").formatNullable[ZonedDateTime] and + (JsPath \ "typeId").formatNullable[Long] and + (JsPath \ "startDate").formatNullable[LocalDate] and + (JsPath \ "endDate").formatNullable[LocalDate] and + (JsPath \ "provider").formatNullable[String] and + (JsPath \ "providerTypeId").formatNullable[Long] and + (JsPath \ "status").formatNullable(statusFormat) and + (JsPath \ "previousStatus").formatNullable(statusFormat) and + (JsPath \ "assignee").formatNullable[Long] and + (JsPath \ "previousAssignee").formatNullable[Long] and + (JsPath \ "meta").formatNullable(Format(Reads { x => JsSuccess(Json.stringify(x)) }, Writes[String](Json.parse))) + ) (ApiDocument.apply, unlift(ApiDocument.unapply)) + + def fromDomain(document: Document): ApiDocument = { + ApiDocument( + id = document.id.id, + recordId = document.recordId.id, + physician = document.physician, + lastUpdate = Option(document.lastUpdate).map(ZonedDateTime.of(_, ZoneId.of("Z"))), + typeId = document.typeId.map(_.id), + startDate = document.startDate, + endDate = document.endDate, + provider = document.providerName, + providerTypeId = document.providerTypeId.map(_.id), + status = Option(DocumentUtils.statusToString(document.status)), + previousStatus = document.previousStatus.map(DocumentUtils.statusToString), + assignee = document.assignee.map(_.id), + previousAssignee = document.previousAssignee.map(_.id), + meta = document.meta.map(meta => JsonSerializer.serialize(meta.content)) + ) + } +} |