All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.kodeworks.kvarg.util.Directives.scala Maven / Gradle / Ivy

There is a newer version: 0.7
Show newest version
package no.kodeworks.kvarg.util

import akka.http.scaladsl.server.Directives._
import no.kodeworks.kvarg.message._
import no.kodeworks.kvarg.patch.Patch
//import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._
//import io.circe._
//import io.circe.generic.extras.auto._
//import JsonUtil.circeCfg
import akka.http.scaladsl.marshalling.ToResponseMarshaller

import scala.concurrent.Future

object Directives {
  def processGet[T](g: Future[Any])
                   (implicit
                    tr0: ToResponseMarshaller[T],
                    tr1: ToResponseMarshaller[Patch[T]],
                    tr2: ToResponseMarshaller[List[T]]
                   ) =
    onSuccess(g.mapTo[GetReply]) {
      case success: GetReplyList[_] => complete(success.ts.asInstanceOf[List[T]])
      case success: GetReplyComplete[_] => complete(success.t.asInstanceOf[T])
      case success: GetReplyPatch[_] => complete(success.t.asInstanceOf[Patch[T]])
      case error => complete(500 -> error.toString)
    }

  def processSaveOrUpdate[T](sou: Future[Any])
                            (implicit
                             tr0: ToResponseMarshaller[SaveInserted[T]],
                             tr1: ToResponseMarshaller[SaveUpdated[T]],
                             tr2: ToResponseMarshaller[IncompletelyUpdated[T]],
                             tr3: ToResponseMarshaller[UpdateStashed[T]]
                            ) =
    onSuccess(sou.mapTo[SaveReply[T]]) {
      case success: SaveInserted[T] => complete(success)
      case success: SaveUpdated[T] => complete(success)
      case success: IncompletelyUpdated[T] => complete(success)
      //cannot send 400 as it will not be processed in js
      case clientError: UpdateStashed[T] => complete(clientError)
      case error => complete(500 -> error.toString)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy