diff options
author | vlad <vlad@driver.xyz> | 2017-07-01 04:50:38 -0700 |
---|---|---|
committer | vlad <vlad@driver.xyz> | 2017-07-01 04:50:38 -0700 |
commit | 05543fd8a5a4fb9a63843bde9ec124d623f1c094 (patch) | |
tree | fd70fe49053d91592e05f63a7664166e4dad063f /src | |
parent | 6a198a9da5a4fbdc94b008b743246c1b0ab2c3dc (diff) | |
download | rest-query-05543fd8a5a4fb9a63843bde9ec124d623f1c094.tar.gz rest-query-05543fd8a5a4fb9a63843bde9ec124d623f1c094.tar.bz2 rest-query-05543fd8a5a4fb9a63843bde9ec124d623f1c094.zip |
Moving storages back and adding ListResponse herev0.1.17
Diffstat (limited to 'src')
6 files changed, 62 insertions, 82 deletions
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/ListResponse.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/ListResponse.scala new file mode 100644 index 0000000..9faa77f --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/ListResponse.scala @@ -0,0 +1,54 @@ +package xyz.driver.pdsuidomain.formats.json + +import java.time.LocalDateTime + +import xyz.driver.pdsuicommon.db.Pagination +import xyz.driver.pdsuicommon.json.Serialization.seqJsonFormat +import play.api.libs.functional.syntax._ +import play.api.libs.json._ + +case class ListResponse[+T](items: Seq[T], meta: ListResponse.Meta) + +object ListResponse { + + case class Meta(itemsCount: Int, pageNumber: Int, pageSize: Int, lastUpdate: Option[LocalDateTime]) + + object Meta { + def apply(itemsCount: Int, pagination: Pagination, lastUpdate: Option[LocalDateTime]): Meta = { + Meta(itemsCount, pagination.pageNumber, pagination.pageSize, lastUpdate) + } + } + + private val listResponseMetaJsonReads: Reads[Meta] = { + ((JsPath \ "itemsCount").read[Int] and + (JsPath \ "pageNumber").read[Int] and + (JsPath \ "pageSize").read[Int] and + (JsPath \ "lastUpdate").readNullable[LocalDateTime] + ).apply { (itemsCount: Int, pageNumber: Int, pageSize: Int, lastUpdate: Option[LocalDateTime]) => + Meta(itemsCount, pageNumber, pageSize, lastUpdate) + } + } + + implicit val listResponseMetaJsonWrites: Writes[Meta] = ( + (JsPath \ "itemsCount").write[Int] and + (JsPath \ "pageNumber").write[Int] and + (JsPath \ "pageSize").write[Int] and + (JsPath \ "lastUpdate").write[Option[LocalDateTime]] + ) (unlift(Meta.unapply)) + + implicit val listResponseMetaJsonFormat: Format[Meta] = Format( + listResponseMetaJsonReads, + listResponseMetaJsonWrites + ) + + implicit def listResponseJsonWrites[T](implicit f: Writes[T]): Writes[ListResponse[T]] = ( + (JsPath \ "items").write[Seq[T]] and + (JsPath \ "meta").write[Meta] + ) (unlift(ListResponse.unapply[T])) + + implicit def listResponseJsonFormat[T](implicit f: Format[T]): Format[ListResponse[T]] = ( + (JsPath \ "items").format(seqJsonFormat[T]) and + (JsPath \ "meta").format[Meta] + ) (ListResponse.apply[T], unlift(ListResponse.unapply[T])) + +} diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiDocument.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiDocument.scala index dde5fd2..859972c 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiDocument.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/document/ApiDocument.scala @@ -30,7 +30,7 @@ object ApiDocument { private val statusFormat = Format( Reads.StringReads.filter(ValidationError("unknown status")) { case x if Document.Status.fromString.isDefinedAt(x) => true - case _ => false + case _ => false }, Writes.StringWrites ) diff --git a/src/main/scala/xyz/driver/pdsuidomain/storage/MedicalRecordDocumentStorage.scala b/src/main/scala/xyz/driver/pdsuidomain/storage/MedicalRecordDocumentStorage.scala deleted file mode 100644 index 192512f..0000000 --- a/src/main/scala/xyz/driver/pdsuidomain/storage/MedicalRecordDocumentStorage.scala +++ /dev/null @@ -1,26 +0,0 @@ -package xyz.driver.pdsuidomain.storage - -import com.google.cloud.storage.StorageOptions -import com.typesafe.scalalogging.StrictLogging -import xyz.driver.pdsuidomain.entities.MedicalRecord.PdfSource - -import scala.concurrent.{ExecutionContext, Future, blocking} - -object MedicalRecordDocumentStorage extends StrictLogging { - private val storage = StorageOptions.getDefaultInstance.getService - - def fetchPdf(bucket: String, path: String)(implicit ec: ExecutionContext): Future[PdfSource] = { - logger.trace(s"fetchPdf(bucket=$bucket, path=$path)") - Future { - blocking { - Option(storage.get(bucket, path)) match { - case Some(blob) => - PdfSource.Channel(() => blob.reader()) - case None => - logger.error(s"Failed to find the pdf file $path in bucket: $bucket") - PdfSource.Empty - } - } - } - } -} diff --git a/src/main/scala/xyz/driver/pdsuidomain/storage/RequestStorage.scala b/src/main/scala/xyz/driver/pdsuidomain/storage/RequestStorage.scala deleted file mode 100644 index 6aa49a1..0000000 --- a/src/main/scala/xyz/driver/pdsuidomain/storage/RequestStorage.scala +++ /dev/null @@ -1,48 +0,0 @@ -package xyz.driver.pdsuidomain.storage - -import xyz.driver.pdsuicommon.domain.{LongId, UuidId} -import xyz.driver.pdsuicommon.logging._ -import xyz.driver.pdsuidomain.entities.{Arm, Patient} - -import scala.collection.concurrent.TrieMap - -object RequestStorage { - type Key = (UuidId[Patient], String) - type Value = Set[LongId[Arm]] -} - -class RequestStorage extends PhiLogging { - import RequestStorage._ - - private val storage = new TrieMap[Key, Value]() - - def put(patientId: UuidId[Patient], disease: String, ineligibleArms: Set[LongId[Arm]]): Unit = { - logger.debug(phi"put($patientId, ${Unsafe(disease)}, $ineligibleArms") - val key = (patientId, disease.toLowerCase) - get(patientId, disease.toLowerCase) match { - case Some(oldValue) => - logger.trace(phi"Requested ineligible arms=$oldValue, replace it") - storage.replace(key, oldValue, ineligibleArms) - case None => - logger.trace(phi"Put request into storage") - storage.put(key, ineligibleArms) - } - } - - def get(patientId: UuidId[Patient], disease: String): Option[Value] = { - logger.debug(phi"get($patientId, ${Unsafe(disease)}") - val key = (patientId, disease.toLowerCase) - storage.get(key) - } - - def contains(patientId: UuidId[Patient], disease: String, value: Set[LongId[Arm]]): Boolean = { - logger.debug(phi"contains(key=($patientId,${Unsafe(disease)}), value=$value") - get(patientId, disease.toLowerCase).contains(value) - } - - def remove(patientId: UuidId[Patient], disease: String): Unit = { - logger.debug(phi"remove($patientId,${Unsafe(disease)}") - val key = (patientId, disease.toLowerCase) - storage.remove(key) - } -} diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/ApiDocumentSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/ApiDocumentSuite.scala index 778e62e..d9d62dd 100644 --- a/src/test/scala/xyz/driver/pdsuidomain/formats/json/ApiDocumentSuite.scala +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/ApiDocumentSuite.scala @@ -75,4 +75,4 @@ class ApiDocumentSuite extends FreeSpecLike { } } -*/ + */ diff --git a/src/test/scala/xyz/driver/pdsuidomain/formats/json/ApiPartialDocumentSuite.scala b/src/test/scala/xyz/driver/pdsuidomain/formats/json/ApiPartialDocumentSuite.scala index ecd079c..7fe1658 100644 --- a/src/test/scala/xyz/driver/pdsuidomain/formats/json/ApiPartialDocumentSuite.scala +++ b/src/test/scala/xyz/driver/pdsuidomain/formats/json/ApiPartialDocumentSuite.scala @@ -233,11 +233,11 @@ class ApiPartialDocumentSuite extends FreeSpecLike { ) /** - * Without fields: - * - endDate - * - assignee - * - meta - */ + * Without fields: + * - endDate + * - assignee + * - meta + */ private def json(addFields: (String, String)*): String = { val addJson = addFields .collect { @@ -260,4 +260,4 @@ class ApiPartialDocumentSuite extends FreeSpecLike { |}""".stripMargin } } - */
\ No newline at end of file + */ |