JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Application structure question

未关闭
#129 46 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Ruby
星标
2.3k
派生
546
PR 合并指标
30 天内没有已合并 PR

描述

This is a very interesting project and executes well something that many other projects have attempted in the past.

I have seen the sample contacts application, read the documentation, and I am currently reading through the gem source but there are still some things which are unclear to me. The most significant of which is where non-CRUD behaviour should be included.

For example, if I were building an application to sell T-shirts using Stripe, I might have an Order class with various LineItems that would consist of Products with corresponding quantities. Now, I could easily manage to create all of the relevant records via a create request to the OrdersController. However, where would I put the call to create my Stripe charge, schedule fulfilment, etc.?

Normally, I would leave my Order and corresponding models fairly simple, and organize charging and fulfilment via services. I would have, say, a PurchaseService which would be instantiated by a PurchaseController. This would arrange for the various models to be created, trigger the Stripe charge, and if successful, schedule fulfilment (possibly by delegating to another service).

The code in the PurchaseController would be very minimal and would not do anything more than pass parameters to the PurchaseService, make a single call on it, and then take care of response and error handling.

It seems like the intention with JR would be to leave the default implementation for the resource controllers, and add charging and purchasing behaviour to the resource code. For example, I would do:

class PurchaseController < JSONAPI::ResourceController
end

and

class PurchaseResource < JSONAPI::Resource
  def create
    # call to Stripe, etc. goes here
  end
end

Does this make sense? Would you structure things differently?

One the one hand this seems like a good place to include the behaviour without needing to reproduce a lot of error and response handling code if I were to override the corresponding controller method. On the other, I've seen the callback support in JSONAPI::Resource but I'm not clear if/when the corresponding lifecycle methods are actually called within the resource code. Additionally, if the code were structured this way, the PurchaseResource would also have to coordinate validating and creating Orders, LineItems, etc. rather than relying on the default JSONAPI::Resource functionality to do so.

Anyhow, apologies for the long-winded and naive question but I feel like I am missing something obvious. Any help in clarifying would be appreciated.

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 JSONAPI::ResourceController、JSONAPI::Resource,以及讨论中提到的 resource callback 和 lifecycle 入口点开始。跟踪与 create 相关的方法和 callback 何时运行,然后记录充费和履约等非 CRUD 工作应放在哪里,以及推荐的结构是什么。

由索引模型根据 Issue 内容生成。

评估

技术栈
rails, ruby
领域
backend-api-design, documentation
Issue 类型
文档
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
20/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。