diff options
author | Kseniya Tomskikh <ktomskih@datamonsters.co> | 2017-10-19 17:34:26 +0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-19 17:34:26 +0700 |
commit | c04b779341e9d54e5b519db085420c9a1ea66610 (patch) | |
tree | 3101844311cddfb7e9efa953311ca1f725c2350a /src | |
parent | 12cee94e187ada6413adc78e0fcc46ed930c306a (diff) | |
parent | 828cccb152ed525f6decf859283cc1b2589d106c (diff) | |
download | rest-query-0.9.3.tar.gz rest-query-0.9.3.tar.bz2 rest-query-0.9.3.zip |
Merge pull request #49 from drivergroup/TRIAL-54v0.9.3
TRIAL-54 Study designs as enum
Diffstat (limited to 'src')
12 files changed, 69 insertions, 31 deletions
diff --git a/src/main/scala/xyz/driver/pdsuidomain/entities/Intervention.scala b/src/main/scala/xyz/driver/pdsuidomain/entities/Intervention.scala index f02bedc..5e743b7 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/entities/Intervention.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/entities/Intervention.scala @@ -5,6 +5,9 @@ import xyz.driver.pdsuicommon.logging._ import xyz.driver.pdsuidomain.entities.InterventionType.DeliveryMethod import xyz.driver.pdsuidomain.entities.InterventionType.DeliveryMethod._ +import scalaz.syntax.equal._ +import scalaz.Scalaz.stringInstance + sealed trait InterventionType { val id: LongId[InterventionType] val name: String @@ -78,16 +81,8 @@ object InterventionType { ) } - def typeFromString: PartialFunction[String, InterventionType] = { - case "Radiation therapy" => RadiationTherapy - case "Chemotherapy" => Chemotherapy - case "Targeted therapy" => TargetedTherapy - case "Immunotherapy" => Immunotherapy - case "Surgery" => Surgery - case "Hormone therapy" => HormoneTherapy - case "Other" => Other - case "Radiation" => Radiation - case "Surgery/Procedure" => SurgeryProcedure + def typeFromString(txt: String): Option[InterventionType] = { + All.values.find(_.name === txt) } sealed trait DeliveryMethod diff --git a/src/main/scala/xyz/driver/pdsuidomain/entities/Trial.scala b/src/main/scala/xyz/driver/pdsuidomain/entities/Trial.scala index ac5feef..e734779 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/entities/Trial.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/entities/Trial.scala @@ -7,9 +7,41 @@ import xyz.driver.pdsuicommon.logging._ import xyz.driver.pdsuicommon.utils.Utils import xyz.driver.pdsuidomain.entities.Trial.Status -final case class StudyDesign(id: LongId[StudyDesign], name: String) +import scalaz.syntax.equal._ +import scalaz.Scalaz.stringInstance + +sealed trait StudyDesign { + val id: LongId[StudyDesign] + val name: String +} object StudyDesign { + + final case object Randomized extends StudyDesign { + val id: LongId[StudyDesign] = LongId[StudyDesign](1) + val name: String = "Randomized" + } + + final case object NonRandomized extends StudyDesign { + val id: LongId[StudyDesign] = LongId[StudyDesign](2) + val name: String = "Non-randomized" + } + + final case object SingleGroupAssignment extends StudyDesign { + val id: LongId[StudyDesign] = LongId[StudyDesign](3) + val name: String = "Single-group assignment" + } + + val All = Seq[StudyDesign]( + Randomized, + NonRandomized, + SingleGroupAssignment + ) + + def fromString(txt: String): Option[StudyDesign] = { + All.find(_.name === txt) + } + implicit def toPhiString(x: StudyDesign): PhiString = { import x._ phi"StudyDesign(id=$id, category=${Unsafe(name)})" diff --git a/src/main/scala/xyz/driver/pdsuidomain/fakes/entities/trialcuration.scala b/src/main/scala/xyz/driver/pdsuidomain/fakes/entities/trialcuration.scala index 84dcb88..0ec1782 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/fakes/entities/trialcuration.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/fakes/entities/trialcuration.scala @@ -131,10 +131,7 @@ object trialcuration { description = generators.nextString() ) - def nextStudyDesign(): StudyDesign = StudyDesign( - id = nextLongId[StudyDesign], - name = generators.nextString() - ) + def nextStudyDesign(): StudyDesign = generators.oneOf[StudyDesign](StudyDesign.All: _*) def nextInterventionType(): InterventionType = generators.oneOf[InterventionType]( RadiationTherapy, diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/document.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/document.scala index e05e841..f742915 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/document.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/document.scala @@ -34,7 +34,7 @@ object document { val name = fields .get("name") .map(_.convertTo[String]) - .getOrElse(deserializationError(s"Intervention type json object does not contain `name` field: $json")) + .getOrElse(deserializationError(s"Document type json object does not contain `name` field: $json")) DocumentType .fromString(name) diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala index bd68566..e557e47 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala @@ -128,7 +128,9 @@ object intervention { .map(_.convertTo[String]) .getOrElse(deserializationError(s"Intervention type json object does not contain `name` field: $json")) - InterventionType.typeFromString(name) + InterventionType + .typeFromString(name) + .getOrElse(deserializationError(s"Unknown intervention type: $name")) case _ => deserializationError(s"Expected Json Object as Intervention type, but got $json") } diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/record.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/record.scala index eac5ea7..09517e6 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/record.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/record.scala @@ -42,13 +42,13 @@ object record { val name = fields .get("name") .map(_.convertTo[String]) - .getOrElse(deserializationError(s"Intervention type json object does not contain `name` field: $json")) + .getOrElse(deserializationError(s"Provider type json object does not contain `name` field: $json")) ProviderType .fromString(name) .getOrElse(deserializationError(s"Unknown provider type: $name")) - case _ => deserializationError(s"Expected Json Object as Intervention type, but got $json") + case _ => deserializationError(s"Expected Json Object as Provider type, but got $json") } override def write(obj: ProviderType) = diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/studydesign.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/studydesign.scala index e801666..3002b38 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/studydesign.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/studydesign.scala @@ -7,6 +7,23 @@ object studydesign { import DefaultJsonProtocol._ import common._ - implicit val studyDesignFormat: RootJsonFormat[StudyDesign] = jsonFormat2(StudyDesign.apply) + implicit val studyDesignFormat: RootJsonFormat[StudyDesign] = new RootJsonFormat[StudyDesign] { + override def read(json: JsValue): StudyDesign = json match { + case JsObject(fields) => + val name = fields + .get("name") + .map(_.convertTo[String]) + .getOrElse(deserializationError(s"Study design json object does not contain `name` field: $json")) + + StudyDesign + .fromString(name) + .getOrElse(deserializationError(s"Unknown study design: $name")) + + case _ => deserializationError(s"Expected Json Object as Study design, but got $json") + } + + override def write(obj: StudyDesign) = + JsObject("id" -> obj.id.toJson, "name" -> obj.name.toJson) + } } diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/studydesign/ApiStudyDesign.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/studydesign/ApiStudyDesign.scala index 1e9a226..12d40df 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/studydesign/ApiStudyDesign.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/studydesign/ApiStudyDesign.scala @@ -3,11 +3,10 @@ package xyz.driver.pdsuidomain.formats.json.studydesign import xyz.driver.pdsuidomain.entities.StudyDesign import play.api.libs.functional.syntax._ import play.api.libs.json.{Format, JsPath} -import xyz.driver.pdsuicommon.domain.LongId final case class ApiStudyDesign(id: Long, name: String) { - def toDomain = StudyDesign(id = LongId[StudyDesign](id), name = name) + def toDomain: Option[StudyDesign] = StudyDesign.fromString(name) } object ApiStudyDesign { diff --git a/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestInterventionTypeService.scala b/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestInterventionTypeService.scala index 87f32ec..ce7ac10 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestInterventionTypeService.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestInterventionTypeService.scala @@ -27,7 +27,7 @@ class RestInterventionTypeService(transport: ServiceTransport, baseUri: Uri)( reply <- apiResponse[ListResponse[ApiInterventionType]](response) } yield { { - val domain = reply.items.map(_.toDomain) + val domain = reply.items.flatMap(_.toDomain) GetListReply.EntityList(domain.toList, reply.meta.itemsCount) } } diff --git a/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestStudyDesignService.scala b/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestStudyDesignService.scala index 98b397d..b1684eb 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestStudyDesignService.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestStudyDesignService.scala @@ -26,7 +26,7 @@ class RestStudyDesignService(transport: ServiceTransport, baseUri: Uri)( response <- transport.sendRequestGetResponse(requestContext)(request) reply <- apiResponse[ListResponse[ApiStudyDesign]](response) } yield { - GetListReply.EntityList(reply.items.map(_.toDomain), reply.meta.itemsCount) + GetListReply.EntityList(reply.items.flatMap(_.toDomain), reply.meta.itemsCount) } } diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/InterventionFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/InterventionFormatSuite.scala index 5891e1c..4532da4 100644 --- a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/InterventionFormatSuite.scala +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/InterventionFormatSuite.scala @@ -59,7 +59,7 @@ class InterventionFormatSuite extends FlatSpec with Matchers { } "Json format for InterventionType" should "read and write correct JSON" in { - val interventionType = InterventionType.typeFromString("Surgery/Procedure") + val interventionType = InterventionType.SurgeryProcedure val writtenJson = interventionTypeFormat.write(interventionType) writtenJson should be( diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/StudyDesignFormatSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/StudyDesignFormatSuite.scala index 9cf9b5f..e847445 100644 --- a/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/StudyDesignFormatSuite.scala +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/StudyDesignFormatSuite.scala @@ -2,20 +2,16 @@ package xyz.driver.pdsuidomain.formats.json.sprayformats import spray.json._ import org.scalatest.{FlatSpec, Matchers} -import xyz.driver.pdsuicommon.domain.LongId import xyz.driver.pdsuidomain.entities.StudyDesign class StudyDesignFormatSuite extends FlatSpec with Matchers { import studydesign._ "Json format for StudyDesign" should "read and write correct JSON" in { - val studyDesign = StudyDesign( - id = LongId(10), - name = "study design name" - ) + val studyDesign = StudyDesign.Randomized val writtenJson = studyDesignFormat.write(studyDesign) - writtenJson should be("""{"id":10,"name":"study design name"}""".parseJson) + writtenJson should be("""{"id":1,"name":"Randomized"}""".parseJson) val parsedStudyDesign = studyDesignFormat.read(writtenJson) parsedStudyDesign should be(studyDesign) |