I have following code snippet where dataService returns Option[LocationDataResult]. I would like to set NotFound when dataService returns None and send the data back in case of Some( ...).
I have following code:
val route: Route = {
pathPrefix("service" / "data") {
pathPrefix( "infobox") {
get {
parameters(('mes.as[String], 'oks.as[String])) {
(me, okr) =>
val resp = dataService.ask(GetocationInfoboxData(me,okr)).mapTo[LocationInfoboxDataResult]
.map(remapInfoboxToResponseObject(_)).map { r =>
r match {
case None => StatusCodes.NotFound
case Some(dataToRespond) => dataToRespond
}
}
complete {
resp
}
}
}
}
}
}
implicit val responseMarhaller: Marshaller[LocationInfobox] = Marshaller.of[WikiLocationInfobox](ContentTypes.`application/json`) { (value, contentType, ctx) =>
val result: String = mapper.writeValueAsString(value)
ctx.marshalTo(HttpEntity(contentType, result))
}
I am not able to find a proper way from marshaller and from route via complete function I am not able to make it work.
Could someone more experienced give me a hint? Am I missing some important concept here?
Thx
UPDATE: Error message" Expression of type Future[Object] doesn't conform to expected type ToResponseMarsallable.
This works for me fine taking advantage of MetaMarshallers