1

I'm developing a REST API using Zend Framework 1.12.3.

I've got three different resources: classes, subjects and professors. Each class may have assigned multiple subjects, and each subject may have assigned one or multiple professors.

The schema would be something like this:

classes:
    class:
        id: 123
        name: foo
        subjects:
            subject:
                id: 14
                name: Chemistry
                professors:
                    professor:
                        id: 741
                        name: Jeremy Clarkson
                    professor:
                        id: 963
                        name: Richard Hammond
            subject:
                id: 16
                name: Physics
                professors:
                    professor:
                        id: 753
                        name: James May

I was wondering which is the best approach to list all the subjects and professors, both assigned and unassigned (on a page where the actual assignment is going to take place).

Should I return only the assigned subjects and professors, and then cross-reference those id to the array containing all the subjects and professors?

Or should I return an array containing all the subjects and professors, each having an "assigned" variable set to 0 or 1?

In other APIs I've seen that dealing with assigned users to certain projects is done using the first method (the API returns only the IDs of the assigned users, and then the client application cross-references those IDs with an array containing all the users). Is this the best practice regarding assignments ?

Shog9
  • 156,901
  • 35
  • 231
  • 235
Daniel
  • 667
  • 6
  • 19

3 Answers3

1

I suggest the former method since the data set required to display a concise list of subject/professors is very small compared to the latter method. If you're not doing it already, I'd recommend using JSON for the data format. This makes it very easy to call this method from client-side JS and create interesting representations.

Based on the initial list of subject/professor hierarchy it would be easy to match the ids to a full set from another call as you suggest, or make a call for each professor id in the set and get details about that individual. If you're doing them via AJAX, you can even run them in parallel: Parallel asynchronous Ajax requests using jQuery

Community
  • 1
  • 1
roktechie
  • 1,345
  • 1
  • 10
  • 15
1

in case of a REST API making a resource only for the relationship doesn't make sense. here a Subject , professor a class is a resource.

A subject can have one or more professors assigned, but it doesn't need to contains info about other professors. like wise a from a professor's point it should only contain info about the subjects assigned to it.

Send both the the lists, professors, and subjects (or the list of classes which in turn contains subject list). Let there be references to professors in the subjects they way you have put in schema sample. same why the professors can have references of subjects-class assigned to them.

For the assignment page. consider one of the list as primary (Smaller one is a good idea) and use that to cross reference the other one.

This way if at some point you want the information about a single professor. one element/document/model from the professor list can give you all you need and vice-versa for subjects.

Mohit
  • 2,239
  • 19
  • 30
1

I think you should get the object model straight before you try and create the services. Perhaps it is a culture thing (I am Dutch) but it seems like some kind of Roster object is missing from the model, and would be the resource to write the service for.

flup
  • 26,937
  • 7
  • 52
  • 74