diff options
author | Kseniya Tomskikh <ktomskih@datamonsters.co> | 2017-10-23 15:50:10 +0700 |
---|---|---|
committer | Kseniya Tomskikh <ktomskih@datamonsters.co> | 2017-10-23 15:50:10 +0700 |
commit | 683bf697e0f6844202f7e61eea0ef67498ed6260 (patch) | |
tree | fda3422f7e0dca72d70710bb8e46dff3d6970ae1 /src/main/scala/xyz/driver/pdsuidomain/services | |
parent | e9f8fbb7f8f379e6885331c94364f6e7d41ffbb2 (diff) | |
parent | d1537fa8bbf7c7097fe7ddb410e40c82381d79f2 (diff) | |
download | rest-query-683bf697e0f6844202f7e61eea0ef67498ed6260.tar.gz rest-query-683bf697e0f6844202f7e61eea0ef67498ed6260.tar.bz2 rest-query-683bf697e0f6844202f7e61eea0ef67498ed6260.zip |
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuidomain/services')
-rw-r--r-- | src/main/scala/xyz/driver/pdsuidomain/services/HypothesisService.scala | 39 | ||||
-rw-r--r-- | src/main/scala/xyz/driver/pdsuidomain/services/rest/RestHypothesisService.scala | 36 |
2 files changed, 69 insertions, 6 deletions
diff --git a/src/main/scala/xyz/driver/pdsuidomain/services/HypothesisService.scala b/src/main/scala/xyz/driver/pdsuidomain/services/HypothesisService.scala index 52cd6c8..572edb6 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/services/HypothesisService.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/services/HypothesisService.scala @@ -2,12 +2,21 @@ package xyz.driver.pdsuidomain.services import xyz.driver.pdsuicommon.auth.AuthenticatedRequestContext import xyz.driver.pdsuicommon.db.Sorting +import xyz.driver.pdsuicommon.domain.UuidId import xyz.driver.pdsuicommon.error.DomainError import xyz.driver.pdsuidomain.entities.Hypothesis import scala.concurrent.Future object HypothesisService { + trait DefaultNotFoundError { + def userMessage: String = "Hypothesis not found" + } + + trait DefaultAccessDeniedError { + def userMessage: String = "Access denied" + } + sealed trait GetListReply object GetListReply { final case class EntityList(xs: Seq[Hypothesis], totalFound: Int) extends GetListReply @@ -16,6 +25,32 @@ object HypothesisService { def userMessage: String = "Access denied" } } + + sealed trait CreateReply + object CreateReply { + final case class Created(x: Hypothesis) extends CreateReply + + type Error = CreateReply with DomainError + + case object AuthorizationError + extends CreateReply with DefaultAccessDeniedError with DomainError.AuthorizationError + + final case class CommonError(userMessage: String) extends CreateReply with DomainError + } + + sealed trait DeleteReply + object DeleteReply { + case object Deleted extends DeleteReply + + type Error = DeleteReply with DomainError + + case object NotFoundError extends DeleteReply with DefaultNotFoundError with DomainError.NotFoundError + + case object AuthorizationError + extends DeleteReply with DefaultAccessDeniedError with DomainError.AuthorizationError + + final case class CommonError(userMessage: String) extends DeleteReply with DomainError + } } trait HypothesisService { @@ -24,4 +59,8 @@ trait HypothesisService { def getAll(sorting: Option[Sorting] = None)( implicit requestContext: AuthenticatedRequestContext): Future[GetListReply] + + def create(draftHypothesis: Hypothesis)(implicit requestContext: AuthenticatedRequestContext): Future[CreateReply] + + def delete(id: UuidId[Hypothesis])(implicit requestContext: AuthenticatedRequestContext): Future[DeleteReply] } diff --git a/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestHypothesisService.scala b/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestHypothesisService.scala index 1b8c943..9cef4c8 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestHypothesisService.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestHypothesisService.scala @@ -1,14 +1,18 @@ package xyz.driver.pdsuidomain.services.rest import scala.concurrent.{ExecutionContext, Future} - +import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ +import akka.http.scaladsl.marshalling.Marshal import akka.http.scaladsl.model._ import akka.stream.Materializer import xyz.driver.core.rest._ import xyz.driver.pdsuicommon.auth._ import xyz.driver.pdsuicommon.db._ -import xyz.driver.pdsuidomain.formats.json.ListResponse -import xyz.driver.pdsuidomain.formats.json.hypothesis.ApiHypothesis +import xyz.driver.pdsuicommon.domain.UuidId +import xyz.driver.pdsuidomain.entities.Hypothesis +import xyz.driver.pdsuidomain.ListResponse +import xyz.driver.pdsuidomain.formats.json.sprayformats.listresponse._ +import xyz.driver.pdsuidomain.formats.json.sprayformats.hypothesis._ import xyz.driver.pdsuidomain.services.HypothesisService class RestHypothesisService(transport: ServiceTransport, baseUri: Uri)( @@ -16,7 +20,6 @@ class RestHypothesisService(transport: ServiceTransport, baseUri: Uri)( protected val exec: ExecutionContext) extends HypothesisService with RestHelper { - import xyz.driver.pdsuicommon.serialization.PlayJsonSupport._ import xyz.driver.pdsuidomain.services.HypothesisService._ def getAll(sorting: Option[Sorting] = None)( @@ -24,9 +27,30 @@ class RestHypothesisService(transport: ServiceTransport, baseUri: Uri)( val request = HttpRequest(HttpMethods.GET, endpointUri(baseUri, "/v1/hypothesis", sortingQuery(sorting))) for { response <- transport.sendRequestGetResponse(requestContext)(request) - reply <- apiResponse[ListResponse[ApiHypothesis]](response) + reply <- apiResponse[ListResponse[Hypothesis]](response) + } yield { + GetListReply.EntityList(reply.items, reply.meta.itemsCount) + } + } + + def create(draftHypothesis: Hypothesis)(implicit requestContext: AuthenticatedRequestContext): Future[CreateReply] = { + for { + entity <- Marshal(draftHypothesis).to[RequestEntity] + request = HttpRequest(HttpMethods.POST, endpointUri(baseUri, "/v1/hypothesis")).withEntity(entity) + response <- transport.sendRequestGetResponse(requestContext)(request) + reply <- apiResponse[Hypothesis](response) + } yield { + CreateReply.Created(reply) + } + } + + def delete(id: UuidId[Hypothesis])(implicit requestContext: AuthenticatedRequestContext): Future[DeleteReply] = { + val request = HttpRequest(HttpMethods.DELETE, endpointUri(baseUri, s"/v1/hypothesis/$id")) + for { + response <- transport.sendRequestGetResponse(requestContext)(request) + _ <- apiResponse[HttpEntity](response) } yield { - GetListReply.EntityList(reply.items.map(_.toDomain), reply.meta.itemsCount) + DeleteReply.Deleted } } |