aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Odersky <jakob@driver.xyz>2017-10-30 14:17:28 -0700
committerJakob Odersky <jakob@driver.xyz>2017-10-30 14:30:28 -0700
commit344996ef6d19551d7cbff717ada35718afcf346a (patch)
tree42d6a85b8898ac228971c1c0bdb083145f906701
parent604fbf0a7a082bc440c0778abd6f90005b210c16 (diff)
downloadrest-query-344996ef6d19551d7cbff717ada35718afcf346a.tar.gz
rest-query-344996ef6d19551d7cbff717ada35718afcf346a.tar.bz2
rest-query-344996ef6d19551d7cbff717ada35718afcf346a.zip
Implement REST layer for PatientLabelService
-rw-r--r--src/main/scala/xyz/driver/pdsuicommon/utils/CustomSwaggerJsonFormats.scala2
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/formats/json/patientlabel.scala21
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/services/rest/RestPatientLabelService.scala84
-rw-r--r--src/test/scala/xyz/driver/pdsuidomain/formats/json/PatientLabelFormatSuite.scala6
4 files changed, 98 insertions, 15 deletions
diff --git a/src/main/scala/xyz/driver/pdsuicommon/utils/CustomSwaggerJsonFormats.scala b/src/main/scala/xyz/driver/pdsuicommon/utils/CustomSwaggerJsonFormats.scala
index a79071e..fd8ff76 100644
--- a/src/main/scala/xyz/driver/pdsuicommon/utils/CustomSwaggerJsonFormats.scala
+++ b/src/main/scala/xyz/driver/pdsuicommon/utils/CustomSwaggerJsonFormats.scala
@@ -193,7 +193,7 @@ object CustomSwaggerJsonFormats {
val customTreatmentMatchingObjectsExamples = immutable.Map[Class[_], JsValue](
classOf[Patient] -> patientFormat.write(nextPatient()),
- classOf[RichPatientLabel] -> richPatientLabelWriter.write(nextRichPatientLabel()),
+ classOf[RichPatientLabel] -> richPatientLabelFormat.write(nextRichPatientLabel()),
classOf[PatientLabel] -> patientLabelDefiningCriteriaWriter.write(nextPatientLabel()),
classOf[RichPatientCriterion] -> patientCriterionWriter.write(nextRichPatientCriterion()),
classOf[DraftPatientCriterion] -> draftPatientCriterionFormat.write(nextDraftPatientCriterion()),
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/patientlabel.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/patientlabel.scala
index 15fec45..d944e42 100644
--- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/patientlabel.scala
+++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/patientlabel.scala
@@ -33,18 +33,17 @@ object patientlabel {
case _ => deserializationError(s"Expected Json Object as PatientLabel, but got $json")
}
- implicit val richPatientLabelWriter: RootJsonWriter[RichPatientLabel] = new RootJsonWriter[RichPatientLabel] {
+ implicit val patientLabelFormat: RootJsonFormat[PatientLabel] = jsonFormat8(PatientLabel.apply)
+
+ implicit val richPatientLabelFormat: RootJsonFormat[RichPatientLabel] = new RootJsonFormat[RichPatientLabel] {
+ override def read(json: JsValue): RichPatientLabel = {
+ val isVerified =
+ json.asJsObject.fields.getOrElse("isVerified", deserializationError("isVerified field is missing"))
+ RichPatientLabel(json.convertTo[PatientLabel], isVerified.convertTo[Boolean])
+ }
override def write(obj: RichPatientLabel): JsValue = {
- JsObject(
- "id" -> obj.patientLabel.id.toJson,
- "labelId" -> obj.patientLabel.labelId.toJson,
- "primaryValue" -> obj.patientLabel.primaryValue.toJson,
- "verifiedPrimaryValue" -> obj.patientLabel.verifiedPrimaryValue.toJson,
- "score" -> obj.patientLabel.score.toJson,
- "isImplicitMatch" -> obj.patientLabel.isImplicitMatch.toJson,
- "isVisible" -> obj.patientLabel.isVisible.toJson,
- "isVerified" -> obj.isVerified.toJson
- )
+ val labelFields = obj.patientLabel.toJson.asJsObject.fields
+ JsObject(labelFields ++ Map("isVerified" -> obj.isVerified.toJson))
}
}
diff --git a/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestPatientLabelService.scala b/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestPatientLabelService.scala
new file mode 100644
index 0000000..335d662
--- /dev/null
+++ b/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestPatientLabelService.scala
@@ -0,0 +1,84 @@
+package xyz.driver.pdsuidomain.services.rest
+
+import akka.http.scaladsl.marshalling.Marshal
+import akka.http.scaladsl.model._
+import akka.stream.Materializer
+import xyz.driver.core.rest.{Pagination => _, _}
+import xyz.driver.entities.labels.Label
+import xyz.driver.pdsuicommon.auth._
+import xyz.driver.pdsuicommon.db._
+import xyz.driver.pdsuicommon.domain._
+import xyz.driver.pdsuidomain.ListResponse
+import xyz.driver.pdsuidomain.entities._
+import xyz.driver.pdsuidomain.services.PatientLabelService
+
+import scala.concurrent.{ExecutionContext, Future}
+
+class RestPatientLabelService(transport: ServiceTransport, baseUri: Uri)(
+ implicit protected val materializer: Materializer,
+ protected val exec: ExecutionContext)
+ extends PatientLabelService with RestHelper {
+
+ import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+ import xyz.driver.pdsuidomain.formats.json.listresponse._
+ import xyz.driver.pdsuidomain.formats.json.patientlabel._
+ import xyz.driver.pdsuidomain.services.PatientLabelService._
+
+ def getAll(patientId: UuidId[Patient],
+ filter: SearchFilterExpr = SearchFilterExpr.Empty,
+ sorting: Option[Sorting] = None,
+ pagination: Option[Pagination] = None)(
+ implicit requestContext: AuthenticatedRequestContext): Future[GetListReply] = {
+ val request = HttpRequest(HttpMethods.GET,
+ endpointUri(baseUri,
+ s"/v1/patient/$patientId/labels",
+ filterQuery(filter) ++ sortingQuery(sorting) ++ paginationQuery(pagination)))
+ for {
+ response <- transport.sendRequestGetResponse(requestContext)(request)
+ reply <- apiResponse[ListResponse[RichPatientLabel]](response)
+ } yield {
+ GetListReply.EntityList(reply.items, reply.meta.itemsCount)
+ }
+ }
+
+ def getDefiningCriteriaList(patientId: UuidId[Patient],
+ hypothesisId: UuidId[Hypothesis],
+ pagination: Option[Pagination] = None)(
+ implicit requestContext: AuthenticatedRequestContext): Future[GetDefiningCriteriaListReply] = {
+ val request = HttpRequest(HttpMethods.GET,
+ endpointUri(baseUri, s"/patient/$patientId/hypothesis", paginationQuery(pagination)))
+ for {
+ response <- transport.sendRequestGetResponse(requestContext)(request)
+ reply <- apiResponse[ListResponse[PatientLabel]](response)
+ } yield {
+ GetDefiningCriteriaListReply.EntityList(reply.items, reply.meta.itemsCount)
+ }
+ }
+
+ def getByLabelIdOfPatient(patientId: UuidId[Patient], labelId: LongId[Label])(
+ implicit requestContext: AuthenticatedRequestContext): Future[GetByLabelIdReply] = {
+ val request = HttpRequest(HttpMethods.GET, endpointUri(baseUri, s"/v1/patient/$patientId/label/$labelId"))
+ for {
+ response <- transport.sendRequestGetResponse(requestContext)(request)
+ reply <- apiResponse[RichPatientLabel](response)
+ } yield {
+ GetByLabelIdReply.Entity(reply)
+ }
+ }
+
+ def update(origPatientLabel: PatientLabel, draftPatientLabel: PatientLabel)(
+ implicit requestContext: AuthenticatedRequestContext): Future[UpdateReply] = {
+ for {
+ entity <- Marshal(draftPatientLabel).to[RequestEntity]
+ request = HttpRequest(
+ HttpMethods.PATCH,
+ endpointUri(baseUri, s"/v1/patient/${origPatientLabel.patientId}/label/${origPatientLabel.labelId}"))
+ .withEntity(entity)
+ response <- transport.sendRequestGetResponse(requestContext)(request)
+ reply <- apiResponse[RichPatientLabel](response)
+ } yield {
+ UpdateReply.Updated(reply)
+ }
+ }
+
+}
diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/PatientLabelFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/PatientLabelFormatSuite.scala
index 95dfa68..b83aad1 100644
--- a/src/test/scala/xyz/driver/pdsuidomain/formats/json/PatientLabelFormatSuite.scala
+++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/PatientLabelFormatSuite.scala
@@ -23,11 +23,11 @@ class PatientLabelFormatSuite extends FlatSpec with Matchers {
score = 1,
isImplicitMatch = false
)
- val writtenJson = richPatientLabelWriter.write(RichPatientLabel(orig, isVerified = true))
+ val writtenJson = richPatientLabelFormat.write(RichPatientLabel(orig, isVerified = true))
writtenJson should be(
- """{"id":1,"labelId":20,"primaryValue":"Yes","verifiedPrimaryValue":null,"isVisible":true,"isVerified":true,
- "score":1,"isImplicitMatch":false}""".parseJson)
+ """{"id":1,"labelId":20,"primaryValue":"Yes","isVisible":true,"isVerified":true,
+ "score":1,"isImplicitMatch":false, "patientId":"748b5884-3528-4cb9-904b-7a8151d6e343"}""".parseJson)
val updatePatientLabelJson = """{"verifiedPrimaryValue":"No"}""".parseJson
val expectedUpdatedPatientLabel = orig.copy(verifiedPrimaryValue = Some(LabelValue.No))