1

我一直在努力将JSON-API资源与服务对象一起应用。这是我的草稿,

class V1::AccountCreateController < JSONAPI::ResourceController
  def create
    # Form object for the parameter validation
    form = AccountCreateCustomerForm.new customer_params
    form.validate!

    # Use my service object instead of JR's 1:1 mapping to the ActiveRecord model to create it.
    account = AccountCreateService.call(form.attributes)
    if account.errors.blank?
        # selialize the result and rednder
        serializer = JSONAPI::ResourceSerializer.new(resource_klass, ...)
       render json: serializer.serialize_to_hash(resource_klass.new(account)), status: 201
    else
       render render json: 'error message', status: 422
    end
  end
end

这实际上有效。但是我不认为它是干净的。

我认为 JSON-API 资源不应该与服务对象一起使用。

我需要发布第三方 API(Stripe)来进行数据处理,而不仅仅是本地数据库,这让我的生活更容易开发强大且可测试的支付系统。(没有回调。)

然而,使用 JSON-API 处理服务对象非常困难。(用于创建/更新/销毁操作。)

有什么想法可以解决这个问题吗?

如果您也可以发布一些示例,那将非常有帮助。

PS我对表单对象使用干验证

4

0 回答 0