REST服务

编程入门 行业动态 更新时间:2024-10-27 20:35:37
REST服务 - 按月进行的计算(REST Service - Month-To-Date Calculations)

我是REST服务的新手,很抱歉,如果这是一个明显的问题。

在我的系统中,我有一个Staff对象和一个Fee对象。 工作人员向客户收取费用,用于完成一项工作。 每位员工都可以获得许多费用。

通常的CRUD方法可通过以下方式获得:

mydomain.com/Staff mydomain.com/Fee

有业务需要显示给定员工的每周,每月至今和年初至今的费用总额。

如果这是一个SOAP服务,那么我可能会创建一个像GetToDateTotals(staff)这样的方法来计算和返回所有的数字。 我不知道这个逻辑应该在REST服务中的位置。

我知道我可以在客户端通过单独GET所有适当的费用并进行计算来完成它但我看到了这种方法的两个缺点:

可能有数千个电话要求服务单独获得所有费用 逻辑在客户端,因此如果3个客户端使用该服务,则逻辑重复3次

另一种可能的解决方案是使用mydomain.com/Staff/FeesToDate或类似的东西,但这似乎不适合REST架构风格。

I am new to REST services so sorry if this is an obvious question.

In my system, I have a Staff object and a Fee object. A Fee is charged by a Staff member to a client for doing a unit of work. Each Staff can have many Fees associated to it.

The usual CRUD methods are available through:

mydomain.com/Staff mydomain.com/Fee

There is a business need to show week-to-date, month-to-date, and year-to-date Fee totals for a given Staff.

If this was a SOAP service then I would probably create a method like GetToDateTotals(staff) which would calculate and return all of the figures. I don't know where this logic should go in a REST service though.

I know I could do it on the client side by GETting all of the appropriate Fees individually and doing the calculations but I see 2 downsides with that approach:

There could potentially be thousands of calls to the service to get all of the fees individually The logic is on the client side so if 3 clients use the service, the logic is duplicated 3 times

Another possible solution could be to have a mydomain.com/Staff/FeesToDate or something similar but this doesn't seem to fit the REST architecture style.

最满意答案

我会有以下REST接口:

GET mydomain.com/staff/<staffId>/fees //returns paginated list of fees of a staff GET mydomain.com/staff/<staffId>/fees/summary //returns week-to-date, month-to-date, and year to-date

您可以选择让查询参数只返回一种类型的摘要:

GET mydomain.com/staff/<staffId>/fees/summary?type=month-to-date

您在计算客户端的总和时是正确的,在进行计算之前,您必须多次调用才能获得所有费用,这是不可取的。 最好在服务器端执行计算,以便进行缓存。

I would have the following REST interface:

GET mydomain.com/staff/<staffId>/fees //returns paginated list of fees of a staff GET mydomain.com/staff/<staffId>/fees/summary //returns week-to-date, month-to-date, and year to-date

You can optionally have query parameter to only return one type of summary:

GET mydomain.com/staff/<staffId>/fees/summary?type=month-to-date

You are right about calculating the sums at client side that you will have to make many calls to get all the fees before doing the calculation, which is not desirable. It's better to perform the calculation at the server side so you can do caching.

更多推荐

本文发布于:2023-07-20 11:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1198691.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:REST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!