Let's say I am modeling blogging REST API which has resources Blog, Post and Comment.
Then I add following URIs for Blog and Post resources:
/api/blogs
/api/blogs/{blogId}
/api/blogs/{blogId}/posts
and since deep nesting should be avoided I create separate endpoint for all Posts in order to get their Comment`s:
/api/posts
/api/posts/{postId}
/api/posts/{postId}/comments
Now, since Post resource can be accessed from two different URIs like this:
- /api/posts?blogId=123
- /api/blogs/123/posts
how should I implement this in ASP.NET Core API project without unnecessary code duplication?
Eg. should I implement both of these endpoints in the same action of the same controller, or should I separate this in two controllers (eg. handle /api/posts?blogId=123 in PostsController and /api/blogs/123/posts in BlogsController)?
Also, should I implement POST, PUT and DELETE actions on both endpoints or just choose one as the primary URI?
After I understand how to do this, is it ok to generalize the same approach for other resources with the same kind of relationship (eg. Post and Comment)?
BlogandPost: learn.microsoft.com/en-us/ef/core/modeling/…