Hi all,
This is my first post in this group. I am currently designing an API with CJ and I have been reading other posts related to subcollections and the like. I think I have understood the basics and the flexibility it provides.
However, I have some doubts about some specific aspects of my implementation, especially the ones related with individual items inside an assorted collection. Please, excuse me if this has already been addressed in other posts.
Let's assume this scenario: a list of companies and a list of visits to each of those companies. A user can do CRUD operations to companies and visits. The collection would be like this one:
{
"collection": {
"version": "1.0",
"href": "/companies",
"items": [
{
"href": "/companies/1",
"data": [
{"name": "kind", "value": "company"},
{"name": "name", "value": "My company"}
],
"links": [
{"rel": "template", "name": "add_visit", "href": "/companies/1/visits"}
]
},
{
"href": "/companies/1/visits/1",
"data": [
{"name": "kind", "value": "visit"},
{"name": "date", "value": "2015/01/01"},
{"name": "purpose", "value": "Visit no 1."}
]
},
{
"href": "/companies/1/visits/2",
"data": [
{"name": "kind", "value": "visit"},
{"name": "date", "value": "2015/01/05"},
{"name": "purpose", "value": "Visit no 2."}
]
},
{
"href": "/companies/2",
"data": [
{"name": "kind", "value": "company"},
{"name": "name", "value": "Another company"}
],
"links": [
{"rel": "template", "name": "add_visit", "href": "/companies/2/visits"}
]
},
{
"href": "/companies/2/visits/1",
"data": [
{"name": "kind", "value": "visit"},
{"name": "date", "value": "2015/05/01"},
{"name": "purpose", "value": "Visit no 1."}
]
}
],
"template": {
"data": [
{"name": "name", "value": ""}
]
}
}
}
I have used the template in the collection to create companies. To create visits, I use a link in each company item that points to a template representation.
And finally, my questions:
1) Does this approach meet CJ requirements? Do I need to include the link to the template in each item or should I include a simple link to /companies/X/visits and then leave the clients the option to do a POST request to it? What I am trying to accomplish with that link is the option to create visits from the collection directly.
2) I a bit lost with the concept collection vs item. I know both are represented with a collection object, but the spec says that an item representation contains only one item in the "items" array. So, in this example, if I want to represent a company and its associated visits as an item representation, how should I do it? I have thought of four options:
a) Create a new link in each "company" item that points to a new collection. Something like:
{
"href": "/companies/1",
"data": [
{"name": "kind", "value": "company"},
{"name": "name", "value": "My company"}
],
"links": [
{"rel": "template", "name": "add_visit", "href": "/companies/1/visits"},
{"rel": "collection", "name": "individual_company", "href": "/companies/1/full_data"}
]
}
With this approach, the new collection would allow the creation of more visits (as it is a collection, it allows POST requests) and it could also display more detailed data than in the original collection.
b) Create a new link in each "company" item that points to a query to the actual collection. Something like:
{
"href": "/companies/1",
...
"links": [
{"rel": "template", "name": "add_visit", "href": "/companies/1/visits"},
{"rel": "collection", "name": "individual_company", "href": "/companies?id=1"}
]
}
With this approach the data display for that company would be the same showed in the original collection. So, I would have to display all data in the original collection.
c) Create a new link in each "company" item that points to the item url of that company:
{
"href": "/companies/1",
...
"links": [
{"rel": "template", "name": "add_visit", "href": "/companies/1/visits"},
{"rel": "collection", "name": "individual_company", "href": "/companies/1"}
]
}
I mean, the same option than a) but with the url of the company item. Is this possible? I mean, that URL would act as a collection (allow POST) and item (allow PUT) url.
d) Flatten the hierachy and return an item with the flattened properties of companies and visits.
Is there a "better" way than these four? (I know there is no "right" and absolute way to do things, but I mean "more elegant" or "simpler").
3) Lastly, when deleting a company item, all its associated visits would be deleted as well. Should the server tell clients about that? Or only return 204?
Thanks in advance. I think CJ is really cool, BTW.
Regards,
Pedro Prieto