CrazyNooB
CrazyNooB

Reputation: 1593

How to decide a resource in a restful way based on some tables

I need to know how to decide what is a resource and what is a sub resource. I just have some tables for which i need to make uri's. so lets say i have a student table and studentclass(which gives the name and probably some more details of the class that the student is enrolled in) table with (N:1).

I will make student a main resource and design uri like below

http://home/someapi/v1/student/1/

Following a rest approach for building uri

Qus 1: Which uri shown below do i have to make for accessing the studentclass

1.http://home/someapi/v1/studentclass?studentid=1 or
2.http://home/someapi/v1/student/1/studentclass

Do i have to consider studentclass as a separate resource..?

and

Qus 2: Suppose there is N:N relation between tables then how should the design of uri be?

Upvotes: 2

Views: 313

Answers (2)

William Durand
William Durand

Reputation: 5519

I suggest to deal with two resources: student and class.

You can get a student using the following URI:

http://home/someapi/v1/students/1

Note the plural here, read this article if you want to learn why you should use plural: http://blog.apigee.com/detail/restful_api_design_plural_nouns_and_concrete_names/.

If a student can be enrolled in one class, it could be a property of this student, so you could return the following JSON document (for example):

{
  "student": {
    "id": 1,
    "firstName": "John",
    "lastName": "Doe",
    "class": {
      "id": 10,
      "name": "Room Y"
    }
  }
}

You could get a specific class with the following URI:

http://home/someapi/v1/classes/10

You could get all students for a given class with this URI:

http://home/someapi/v1/classes/10/students

Now, if you have a N-N relation between students and classes, it would change my first proposal about the student representation in JSON. Actually, the class relation would become a collection of class resources, not an embedded property of a student, and you should use the following URI:

http://home/someapi/v1/students/1/classes

Meaning "retrieve all classes for student #1".

You can expose both class and student resources. No need to decide which one is the main one as they describe two different things. Having /students and /classes allows you to create students and classes, you can link them, etc.

Last thing, it's recommended to use query parameters (?foo=bar) for filtering purpose.

Upvotes: 2

Sando
Sando

Reputation: 667

I think StudentClass (or simply 'Class') should be a different resource as Class can be shared amoung students. So this Uri would suit your need:

http:home/someapi/v1/studentclass?studentid=1

Upvotes: 0

Related Questions