From 344996ef6d19551d7cbff717ada35718afcf346a Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Mon, 30 Oct 2017 14:17:28 -0700 Subject: Implement REST layer for PatientLabelService --- .../utils/CustomSwaggerJsonFormats.scala | 2 +- .../pdsuidomain/formats/json/patientlabel.scala | 21 +++--- .../services/rest/RestPatientLabelService.scala | 84 ++++++++++++++++++++++ 3 files changed, 95 insertions(+), 12 deletions(-) create mode 100644 src/main/scala/xyz/driver/pdsuidomain/services/rest/RestPatientLabelService.scala (limited to 'src/main/scala') 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) + } + } + +} -- cgit v1.2.3