Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My guess would be that you split your methods so that you can create a different routes for the ones with and without body.</p> <p>It seems that the design of the code would not work even if the empty string was parsed as JSON. The map method would not be executed because there would be no user. That would cause the match on operation to never be executed.</p> <p><strong>Update</strong></p> <p>Since you mentioned DRY, I would refactor it into something like this:</p> <pre><code> type Operations = PartialFunction[String, String] val operations: Operations = { case "retrieve" =&gt; println("performing retrieve") "retrieved" case "list" =&gt; println("performing list") "listed" } def userOperations(user: User): Operations = { case "create" =&gt; println("actual create operation") "created" case "delete" =&gt; println("actual delete operation") "updated" case "update" =&gt; println("actual update operation") "updated" } def withoutUser(operation: String) = Action { execute(operation, operations andThen successResponse) } def withUser(operation: String) = Action(parse.json) { request =&gt; request.body.asOpt[User].map { user =&gt; execute(operation, userOperations(user) andThen successResponse) } .getOrElse { errorResponse("invalid user data") } } def execute(operation: String, actualOperation: PartialFunction[String, Result]) = if (actualOperation isDefinedAt operation) { try { DBGlobal.db.withTransaction { actualOperation(operation) } } catch { case _ =&gt; errorResponse(operation + " error") } } else { errorResponse(operation + " not found") } val successResponse = createResponse(Ok, "Success", _: String) val errorResponse = createResponse(BadRequest, "Error", _: String) def createResponse(httpStatus: Status, status: String, message: String): Result = httpStatus(Json.toJson(Map("status" -&gt; status, "message" -&gt; message))) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload