From f9ac0adf5c3bcfcde03bd3ea2bc2471b0d0f99fe Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Wed, 5 Jul 2017 19:02:13 -0700 Subject: Implement REST services for trial curation --- .../pdsuidomain/formats/json/trial/ApiTrial.scala | 58 +++++++++++++++++----- 1 file changed, 46 insertions(+), 12 deletions(-) (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats/json/trial/ApiTrial.scala') diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/trial/ApiTrial.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/trial/ApiTrial.scala index 940f1f0..2556feb 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/trial/ApiTrial.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/trial/ApiTrial.scala @@ -2,65 +2,99 @@ package xyz.driver.pdsuidomain.formats.json.trial import java.time.{ZoneId, ZonedDateTime} import java.util.UUID +import xyz.driver.pdsuicommon.domain.{LongId, StringId, UuidId} import xyz.driver.pdsuidomain.entities.Trial import play.api.libs.functional.syntax._ import play.api.libs.json._ final case class ApiTrial(id: String, - lastUpdate: Option[ZonedDateTime], + externalId: UUID, + lastUpdate: ZonedDateTime, status: String, assignee: Option[String], previousStatus: Option[String], previousAssignee: Option[String], lastActiveUser: Option[String], - condition: Option[String], - phase: Option[String], + condition: String, + phase: String, hypothesisId: Option[UUID], studyDesignId: Option[Long], + originalStudyDesign: Option[String], isPartner: Boolean, overview: Option[String], overviewTemplate: String, isUpdated: Boolean, - title: String) + title: String, + originalTitle: String) { + + def toDomain = Trial( + id = StringId(this.id), + externalId = UuidId(this.externalId), + status = TrialStatus.statusFromString(this.status), + assignee = this.assignee.map(id => StringId(id)), + previousStatus = this.previousStatus.map(s => TrialStatus.statusFromString(s)), + previousAssignee = this.previousAssignee.map(id => StringId(id)), + lastActiveUserId = this.lastActiveUser.map(id => StringId(id)), + lastUpdate = this.lastUpdate.toLocalDateTime(), + condition = Trial.Condition.fromString(this.condition), + phase = this.phase, + hypothesisId = this.hypothesisId.map(id => UuidId(id)), + studyDesignId = this.studyDesignId.map(id => LongId(id)), + originalStudyDesign = this.originalStudyDesign, + isPartner = this.isPartner, + overview = this.overview, + overviewTemplate = this.overviewTemplate, + isUpdated = this.isUpdated, + title = this.title, + originalTitle = this.originalTitle + ) + +} object ApiTrial { implicit val format: Format[ApiTrial] = ( (JsPath \ "id").format[String] and - (JsPath \ "lastUpdate").formatNullable[ZonedDateTime] and + (JsPath \ "externalid").format[UUID] and + (JsPath \ "lastUpdate").format[ZonedDateTime] and (JsPath \ "status").format[String] and (JsPath \ "assignee").formatNullable[String] and (JsPath \ "previousStatus").formatNullable[String] and (JsPath \ "previousAssignee").formatNullable[String] and (JsPath \ "lastActiveUser").formatNullable[String] and - (JsPath \ "condition").formatNullable[String] and - (JsPath \ "phase").formatNullable[String] and + (JsPath \ "condition").format[String] and + (JsPath \ "phase").format[String] and (JsPath \ "hypothesisId").formatNullable[UUID] and (JsPath \ "studyDesignId").formatNullable[Long] and + (JsPath \ "originalStudyDesignId").formatNullable[String] and (JsPath \ "isPartner").format[Boolean] and (JsPath \ "overview").formatNullable[String] and (JsPath \ "overviewTemplate").format[String] and (JsPath \ "isUpdated").format[Boolean] and - (JsPath \ "title").format[String] + (JsPath \ "title").format[String] and + (JsPath \ "otiginalTitle").format[String] )(ApiTrial.apply, unlift(ApiTrial.unapply)) def fromDomain(trial: Trial): ApiTrial = ApiTrial( id = trial.id.id, + externalId = trial.externalId.id, status = TrialStatus.statusToString(trial.status), assignee = trial.assignee.map(_.id), previousStatus = trial.previousStatus.map(TrialStatus.statusToString), previousAssignee = trial.previousAssignee.map(_.id), lastActiveUser = trial.lastActiveUserId.map(_.id), - lastUpdate = Option(ZonedDateTime.of(trial.lastUpdate, ZoneId.of("Z"))), - condition = Option(trial.condition.toString), - phase = Option(trial.phase), + lastUpdate = ZonedDateTime.of(trial.lastUpdate, ZoneId.of("Z")), + condition = trial.condition.toString, + phase = trial.phase, hypothesisId = trial.hypothesisId.map(_.id), studyDesignId = trial.studyDesignId.map(_.id), + originalStudyDesign = trial.originalStudyDesign, isPartner = trial.isPartner, overview = trial.overview, overviewTemplate = trial.overviewTemplate, isUpdated = trial.isUpdated, - title = trial.title + title = trial.title, + originalTitle = trial.originalTitle ) } -- cgit v1.2.3 From d733294cb16fa83be014ce1efccbf364aa309d25 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Fri, 14 Jul 2017 15:05:12 -0700 Subject: Fix PR --- src/main/scala/xyz/driver/pdsuidomain/entities/Trial.scala | 9 +++++---- .../driver/pdsuidomain/formats/json/trial/ApiTrial.scala | 6 +++++- .../xyz/driver/pdsuidomain/services/rest/RestHelper.scala | 13 ++++--------- .../pdsuidomain/services/rest/RestMessageService.scala | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats/json/trial/ApiTrial.scala') diff --git a/src/main/scala/xyz/driver/pdsuidomain/entities/Trial.scala b/src/main/scala/xyz/driver/pdsuidomain/entities/Trial.scala index f5c6974..2f90820 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/entities/Trial.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/entities/Trial.scala @@ -70,10 +70,11 @@ object Trial { case object Lung extends Condition case object Prostate extends Condition - def fromString(condition: String): Condition = condition match { - case "Breast" => Breast - case "Lung" => Lung - case "Prostate" => Prostate + def fromString(condition: String): Option[Condition] = condition match { + case "Breast" => Some(Breast) + case "Lung" => Some(Lung) + case "Prostate" => Some(Prostate) + case _ => None } val All: Set[Condition] = Set(Breast, Lung, Prostate) diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/trial/ApiTrial.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/trial/ApiTrial.scala index 2556feb..f59836e 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/trial/ApiTrial.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/trial/ApiTrial.scala @@ -37,7 +37,11 @@ final case class ApiTrial(id: String, previousAssignee = this.previousAssignee.map(id => StringId(id)), lastActiveUserId = this.lastActiveUser.map(id => StringId(id)), lastUpdate = this.lastUpdate.toLocalDateTime(), - condition = Trial.Condition.fromString(this.condition), + condition = Trial.Condition + .fromString(this.condition) + .getOrElse( + throw new NoSuchElementException(s"unknown condition ${this.condition}") + ), phase = this.phase, hypothesisId = this.hypothesisId.map(id => UuidId(id)), studyDesignId = this.studyDesignId.map(id => LongId(id)), diff --git a/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestHelper.scala b/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestHelper.scala index 3113b21..5284ff1 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestHelper.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestHelper.scala @@ -74,21 +74,16 @@ trait RestHelper { case Some(pp) => Seq( "pageNumber" -> pp.pageNumber.toString, - "pageSize" -> pp.pageSize.toHexString + "pageSize" -> pp.pageSize.toString ) } - /** Utility method to parse responses that encode success and errors as subtypes - * of a common reply type. + /** Utility method to parse responses from records-acquisition-server. * + * Non-2xx HTTP error codes will be cause the returned future to fail with a corresponding + * `DomainException`. * @tparam ApiReply The type of the serialized reply object, contained in the HTTP entity - * @tparam DomainReply The type of the domain object that will be created from a successful reply. - * * @param response The HTTP response to parse. - * @param successMapper Transformation function from a deserialized api entity to a domain object. - * @param errorMapper Transformation function from general domain errors to - * specialized errors of the given DomainReply. Note that if a domain error - * is not explicitly handled, it will be encoded as a failure in the returned future. * @param unmarshaller An unmarshaller that converts a successful response to an api reply. */ def apiResponse[ApiReply](response: HttpResponse)( diff --git a/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestMessageService.scala b/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestMessageService.scala index 4eb48d5..1527ad5 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestMessageService.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestMessageService.scala @@ -76,7 +76,7 @@ class RestMessageService(transport: ServiceTransport, baseUri: Uri)( } def delete(messageId: LongId[Message])(implicit requestContext: AuthenticatedRequestContext): Future[DeleteReply] = { - val request = HttpRequest(HttpMethods.GET, endpointUri(baseUri, s"/v1/message/${messageId.id}")) + val request = HttpRequest(HttpMethods.DELETE, endpointUri(baseUri, s"/v1/message/${messageId.id}")) for { response <- transport.sendRequestGetResponse(requestContext)(request) reply <- apiResponse[ApiMessage](response) -- cgit v1.2.3