aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/formats/json/document
diff options
context:
space:
mode:
authorJakob Odersky <jakob@driver.xyz>2017-07-24 16:24:12 -0700
committerJakob Odersky <jakob@driver.xyz>2017-07-25 15:43:56 -0700
commite22a94604d6090d88801ec52c39f4eab500e80e1 (patch)
treee20332a9693a4c2d00d32cf67438ef4ba74f9356 /src/main/scala/xyz/driver/pdsuidomain/formats/json/document
parent23192326cfc82e726b03f23396f2587ca4f606f5 (diff)
downloadrest-query-e22a94604d6090d88801ec52c39f4eab500e80e1.tar.gz
rest-query-e22a94604d6090d88801ec52c39f4eab500e80e1.tar.bz2
rest-query-e22a94604d6090d88801ec52c39f4eab500e80e1.zip
Implement ReP rest services
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats/json/document')
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiDocument.scala42
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiDocumentType.scala10
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiPartialDocument.scala28
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiProviderType.scala10
4 files changed, 70 insertions, 20 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 c01e65c..1869ff3 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
@@ -1,6 +1,8 @@
package xyz.driver.pdsuidomain.formats.json.document
import java.time.{LocalDate, ZoneId, ZonedDateTime}
+import xyz.driver.pdsuicommon.domain.{LongId, StringId, TextJson}
+import xyz.driver.pdsuicommon.json.JsonSerializer
import xyz.driver.pdsuidomain.entities._
import play.api.data.validation.ValidationError
@@ -11,7 +13,7 @@ import xyz.driver.pdsuicommon.json.JsonSerializer
final case class ApiDocument(id: Long,
recordId: Long,
physician: Option[String],
- lastUpdate: Option[ZonedDateTime],
+ lastUpdate: ZonedDateTime,
typeId: Option[Long],
startDate: Option[LocalDate],
endDate: Option[LocalDate],
@@ -23,14 +25,40 @@ final case class ApiDocument(id: Long,
assignee: Option[String],
previousAssignee: Option[String],
lastActiveUser: Option[String],
- meta: Option[String])
+ meta: Option[String]) {
+
+ private def extractStatus(status: String): Document.Status =
+ Document.Status.fromString(status).getOrElse(throw new NoSuchElementException(s"Status $status unknown"))
+
+ private def extractRequiredType(tpe: String): Document.RequiredType =
+ Document.RequiredType.fromString(tpe).getOrElse(throw new NoSuchElementException(s"RequitedType $tpe unknown"))
+
+ def toDomain = Document(
+ id = LongId(this.id),
+ status = extractStatus(this.status.getOrElse("")),
+ previousStatus = previousStatus.map(extractStatus),
+ assignee = this.assignee.map(StringId(_)),
+ previousAssignee = this.previousAssignee.map(StringId(_)),
+ lastActiveUserId = this.lastActiveUser.map(StringId(_)),
+ recordId = LongId(this.recordId),
+ physician = this.physician,
+ typeId = this.typeId.map(LongId(_)),
+ providerName = this.provider,
+ providerTypeId = this.providerTypeId.map(LongId(_)),
+ requiredType = this.requiredType.map(extractRequiredType),
+ meta = this.meta.map(x => TextJson(JsonSerializer.deserialize[Document.Meta](x))),
+ startDate = this.startDate,
+ endDate = this.endDate,
+ lastUpdate = this.lastUpdate.toLocalDateTime()
+ )
+
+}
object ApiDocument {
private val statusFormat = Format(
- Reads.StringReads.filter(ValidationError("unknown status")) {
- case x if Document.Status.fromString.isDefinedAt(x) => true
- case _ => false
+ Reads.StringReads.filter(ValidationError("unknown status")) { x =>
+ Document.Status.fromString(x).isDefined
},
Writes.StringWrites
)
@@ -39,7 +67,7 @@ object ApiDocument {
(JsPath \ "id").format[Long] and
(JsPath \ "recordId").format[Long] and
(JsPath \ "physician").formatNullable[String] and
- (JsPath \ "lastUpdate").formatNullable[ZonedDateTime] and
+ (JsPath \ "lastUpdate").format[ZonedDateTime] and
(JsPath \ "typeId").formatNullable[Long] and
(JsPath \ "startDate").formatNullable[LocalDate] and
(JsPath \ "endDate").formatNullable[LocalDate] and
@@ -61,7 +89,7 @@ object ApiDocument {
id = document.id.id,
recordId = document.recordId.id,
physician = document.physician,
- lastUpdate = Option(document.lastUpdate).map(ZonedDateTime.of(_, ZoneId.of("Z"))),
+ lastUpdate = ZonedDateTime.of(document.lastUpdate, ZoneId.of("Z")),
typeId = document.typeId.map(_.id),
startDate = document.startDate,
endDate = document.endDate,
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiDocumentType.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiDocumentType.scala
index 8b11b91..8b4974f 100644
--- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiDocumentType.scala
+++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiDocumentType.scala
@@ -2,9 +2,17 @@ package xyz.driver.pdsuidomain.formats.json.document
import play.api.libs.functional.syntax._
import play.api.libs.json.{Format, JsPath}
+import xyz.driver.pdsuicommon.domain.LongId
import xyz.driver.pdsuidomain.entities.DocumentType
-final case class ApiDocumentType(id: Long, name: String)
+final case class ApiDocumentType(id: Long, name: String) {
+
+ def toDomain = DocumentType(
+ id = LongId(this.id),
+ name = this.name
+ )
+
+}
object ApiDocumentType {
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 e9485e7..eae0c62 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
@@ -31,7 +31,7 @@ final case class ApiPartialDocument(recordId: Option[Long],
def applyTo(orig: Document): Document = Document(
id = orig.id,
- status = status.map(Document.Status.fromString).getOrElse(orig.status),
+ status = status.flatMap(Document.Status.fromString).getOrElse(orig.status),
previousStatus = orig.previousStatus,
assignee = assignee.map(StringId[User]).cata(Some(_), None, orig.assignee),
previousAssignee = orig.previousAssignee,
@@ -90,16 +90,22 @@ object ApiPartialDocument {
(JsPath \ "endDate").readTristate[LocalDate] and
(JsPath \ "provider").readTristate[String] and
(JsPath \ "providerTypeId").readTristate[Long] and
- (JsPath \ "status").readNullable[String](Reads.of[String].filter(ValidationError("unknown status"))({
- case x if Document.Status.fromString.isDefinedAt(x) => true
- case _ => false
- })) and
+ (JsPath \ "status").readNullable[String](
+ Reads
+ .of[String]
+ .filter(ValidationError("unknown status"))(
+ Document.Status.fromString(_).isDefined
+ )) and
(JsPath \ "assignee").readTristate[String] and
- (JsPath \ "meta").readTristate(Reads { x => JsSuccess(Json.stringify(x)) }).map {
- case Tristate.Present("{}") => Tristate.Absent
- case x => x
- }
- ) (ApiPartialDocument.apply _)
+ (JsPath \ "meta")
+ .readTristate(Reads { x =>
+ JsSuccess(Json.stringify(x))
+ })
+ .map {
+ case Tristate.Present("{}") => Tristate.Absent
+ case x => x
+ }
+ )(ApiPartialDocument.apply _)
private val writes: Writes[ApiPartialDocument] = (
(JsPath \ "recordId").writeNullable[Long] and
@@ -112,7 +118,7 @@ object ApiPartialDocument {
(JsPath \ "status").writeNullable[String] and
(JsPath \ "assignee").writeTristate[String] and
(JsPath \ "meta").writeTristate(Writes[String](Json.parse))
- ) (unlift(ApiPartialDocument.unapply))
+ )(unlift(ApiPartialDocument.unapply))
implicit val format: Format[ApiPartialDocument] = Format(reads, writes)
}
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiProviderType.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiProviderType.scala
index eb0ac46..c0eddad 100644
--- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiProviderType.scala
+++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiProviderType.scala
@@ -2,9 +2,17 @@ package xyz.driver.pdsuidomain.formats.json.document
import play.api.libs.functional.syntax._
import play.api.libs.json.{Format, JsPath}
+import xyz.driver.pdsuicommon.domain.LongId
import xyz.driver.pdsuidomain.entities.ProviderType
-final case class ApiProviderType(id: Long, name: String)
+final case class ApiProviderType(id: Long, name: String) {
+
+ def toDomain = ProviderType(
+ id = LongId(this.id),
+ name = this.name
+ )
+
+}
object ApiProviderType {