0

I have Department Resource and each department can have multiple classes. So, for example:

Dept # 100 can have class = 1, 2, 3, 4, 5 Dept # 200 can have class = 2, 3, 9, 12

I want to get classes by specific class id, multiple class id, specific department id, multiple department ids. Here is what I think about making the URL:

   /depts    -->         displays all departments

   /depts?_id = 100 & _id = 200       -->      displays departments with id 100 and 200

   /depts/100                --> display department with id = 100

   /classes   --> displays all classes

   /classes?_id = 1 & _id = 2 -->  displays classes with id = 1 and id = 2

   /classes/1 -->display class with id = 1

Now,

  1. How would I display all classes in a specific department ( ex: all classes in department 100).

would this work?

  /classes?dept=/depts/{100}

or

  /depts/100/classes
  1. How would I display all classes in multiple departments ( ex: all classes in department 100 and department 200).

would this work?

  /classes?dept=/depts/{100}&dept=/depts/{200}
Opal
  • 81,889
  • 28
  • 189
  • 210
user3528213
  • 1,415
  • 3
  • 15
  • 18
  • Have a look at this: http://stackoverflow.com/questions/20951419/what-are-best-practices-for-rest-nested-resources – EJK Jun 17 '15 at 16:48

1 Answers1

0

You can follow the following rules:

  1. GET /depts/ returns departments
  2. GET/depts/{id}/ returns department with given ID
  3. GET /depts/{id}/classes/ returns classes for department with given ID
  4. GET/classes/ returns all classes
  5. GET /clasess/{id}/ returns a class for given ID

The tricky part is to get classes that belong to multiple depts. It can be done with

  1. GET /classes/?deptID=1&deptID=2 - a simple GET request with filtering via query params
  2. POST /classes/filter - POST request with all the necessary params sent in body.

Both 1. and 2. are equally good for such filtering.

Opal
  • 81,889
  • 28
  • 189
  • 210
  • @user3528213, is my answer helpful? – Opal Jun 18 '15 at 12:19
  • Yes! I do have a quick question: to get all the classes by multiple ids, /classes?_id = 1& _id = 2 would work – user3528213 Jun 18 '15 at 19:53
  • Yes it's ok as well. It's just a kind of GET filtering. There're really many ways it can be done - it's important to make it consistently. If my answer was useful, please accept and upvote it :) – Opal Jun 18 '15 at 19:55
  • to get all the classes by multiple sub-department id,would this work? /classes?_sub = 21 & _sub = 22 or should I do /classes/sub?_id = 21 & _id = 22 – user3528213 Jun 18 '15 at 19:55
  • If you need complicated filtering of classes it's much better to use POST /classes/filter and send all the filter details in body. It looks much better. – Opal Jun 18 '15 at 19:57