I am creating a REST API and I have, for example, Authors and Posts.
So I can get the posts published by an author with:
/authors/123/posts
Or
/posts?authorId=123
I think building a flexible API might be a better option. To get posts by author I would do:
/posts?authorId=123&published=true&sort=created&expand=tags,category
So in this case I am getting all posts from authorId=123 that are published sort them by created date and also get the tags and category of each post.
Basically, I create a query language that kind of maps to each database table.
Then for common queries I create specific endpoints:
/posts/recent
Would return recent posts independently of the author ...
I think /authors/123/posts
might become complex when using many levels.
What do you think?
UPDATE
After a few answers my idea is the following:
When Posts and Authors are two resources I would have (example for posts):
GET /posts?authorId=123&published=true
POST /posts
PUT /posts
DELETE /posts/123
If there is hierarchical dependency between Posts and Authors and I often need posts by author I would also add the following:
GET /authors/123/posts&published=true
If posts would not exist outside of resource Author then the 2 previous options would be replaced by:
GET /authors/123/posts?published=true
POST /authors/123/posts
PUT /authors/123/posts
DELETE /authors/123/posts/123
What do you think?