Foz
Foz

Reputation: 33

REST Service - Month-To-Date Calculations

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:

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:

  1. There could potentially be thousands of calls to the service to get all of the fees individually
  2. 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.

Upvotes: 3

Views: 87

Answers (1)

Alvin
Alvin

Reputation: 10468

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.

Upvotes: 1

Related Questions