From 5832f63b84d7388441d1200f2442dc1e9de0225c Mon Sep 17 00:00:00 2001 From: vlad Date: Tue, 27 Jun 2017 17:13:02 -0700 Subject: All PDS UI domain models, API case classes, service traits and necessary utils moved to pdsui-common --- .../formats/json/criterion/ApiCriterion.scala | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/main/scala/xyz/driver/pdsuidomain/formats/json/criterion/ApiCriterion.scala (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats/json/criterion/ApiCriterion.scala') diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/criterion/ApiCriterion.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/criterion/ApiCriterion.scala new file mode 100644 index 0000000..0f3b76e --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/criterion/ApiCriterion.scala @@ -0,0 +1,40 @@ +package xyz.driver.pdsuidomain.formats.json.criterion + +import xyz.driver.pdsuicommon.json.Serialization.seqJsonFormat +import play.api.libs.functional.syntax._ +import play.api.libs.json._ +import xyz.driver.pdsuidomain.formats.json.label.ApiCriterionLabel +import xyz.driver.pdsuidomain.services.CriterionService.RichCriterion + +final case class ApiCriterion(id: Long, + meta: Option[String], + arms: Seq[Long], + text: Option[String], + isCompound: Boolean, + labels: Seq[ApiCriterionLabel], + trialId: String) + +object ApiCriterion { + + implicit val format: Format[ApiCriterion] = ( + (JsPath \ "id").format[Long] and + (JsPath \ "meta").formatNullable(Format(Reads { x => + JsSuccess(Json.stringify(x)) + }, Writes[String](Json.parse))) and + (JsPath \ "arms").format(seqJsonFormat[Long]) and + (JsPath \ "text").formatNullable[String] and + (JsPath \ "isCompound").format[Boolean] and + (JsPath \ "labels").format(seqJsonFormat[ApiCriterionLabel]) and + (JsPath \ "trialId").format[String] + ) (ApiCriterion.apply, unlift(ApiCriterion.unapply)) + + def fromDomain(richCriterion: RichCriterion) = ApiCriterion( + id = richCriterion.criterion.id.id, + meta = Option(richCriterion.criterion.meta), + arms = richCriterion.armIds.map(_.id), + text = richCriterion.criterion.text, + isCompound = richCriterion.criterion.isCompound, + labels = richCriterion.labels.map(ApiCriterionLabel.fromDomain), + trialId = richCriterion.criterion.trialId.id + ) +} -- cgit v1.2.3