3

我正在尝试使用 PlayFramework 编写一个 DRY CRUD 宁静服务。这是它的代码。

def crudUsers(operation: String) = Action(parse.json) { request =>
 (request.body).asOpt[User].map { jsonUser =>
  try {
    DBGlobal.db.withTransaction {
      val queryResult = operation match {
        case "create" =>
           UsersTable.forInsert.insertAll(jsonUser)
           Json.generate(Map("status" -> "Success", "message" -> "Account inserted"))

        case "update" =>
           val updateQ = UsersTable.where(_.email === jsonUser.email.bind).map(_.forInsert)
           println(updateQ.selectStatement)
           updateQ.update(jsonUser)
           Json.generate(Map("status" -> "Success", "message" -> "Account updated"))

        case "retrieve" =>
           val retrieveQ = for(r <- UsersTable) yield r
           println(retrieveQ.selectStatement)
           Json.generate(retrieveQ.list)

        case "delete" =>
           val deleteQ = UsersTable.where(_.email === jsonUser.email)
           deleteQ.delete
           Json.generate(Map("status" -> "Success", "message" -> "Account deleted"))
      }
      Ok(queryResult)
    }
  } catch {
    case _ =>
      val errMsg: String = operation + " error"
      BadRequest(Json.generate(Map("status" -> "Error", "message" -> errMsg)))
  }
}.getOrElse(BadRequest(Json.generate(Map("status" -> "Error", "message" -> "error"))))

} }

我注意到更新、删除和创建操作运行良好。但是,检索操作失败并显示For request 'GET /1/users' [Invalid Json]. 我很确定这是因为 JSON 解析器不能容忍在正文中没有传递 JSON 的 GET 请求。

有没有办法在不丢失我在这里开始的 DRY 方法的情况下对 GET/Retrieve 操作进行特殊处理?

4

1 回答 1

2

我的猜测是你拆分了你的方法,这样你就可以为有和没有 body 的方法创建不同的路由。

即使将空字符串解析为 JSON,代码的设计似乎也不起作用。map 方法不会被执行,因为没有用户。这将导致匹配操作永远不会被执行。

更新

既然你提到了 DRY,我会把它重构成这样的:

  type Operations = PartialFunction[String, String]

  val operations: Operations = {
    case "retrieve" =>
      println("performing retrieve")
      "retrieved"
    case "list" =>
      println("performing list")
      "listed"
  }

  def userOperations(user: User): Operations = {
    case "create" =>
      println("actual create operation")
      "created"
    case "delete" =>
      println("actual delete operation")
      "updated"
    case "update" =>
      println("actual update operation")
      "updated"
  }

  def withoutUser(operation: String) = Action {
    execute(operation, operations andThen successResponse)
  }

  def withUser(operation: String) = Action(parse.json) { request =>
    request.body.asOpt[User].map { user =>
      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 _ => 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" -> status, "message" -> message)))
于 2012-06-18T02:09:43.877 回答