I'm quite new into REST API development. I have decided to create a Blog application using Spring Boot and I'm really struggling with the design and structure of my app.
Right now my app consists of Post and Comment models and repositories. For both models, I have created service classes (PostService and CommentService). In these classes, I have all the business logic (just simple CRUD right now).
Now I am scratching my head about the design of my @RestControler for Posts. In PostController I have exposed these actions:
@PostMapping("/api/posts/create")
public Post create(@RequestBody Post post) { ... }
@GetMapping("/api/posts")
public List<Post> findAll() { ... }
@GetMapping("/api/posts/{id}")
public Post findById(@PathVariable("id") Long id) { ... }
@PutMapping("/api/posts/{id}")
public Post update(@RequestBody Post post) { ... }
@DeleteMapping("/api/posts/{id}")
public void delete(@PathVariable Long id) { ... }
Now I'm getting to my question. I am wondering what is correct design of adding a Comment to the Post.
- Should I expose all CRUD method for Comment using CommentController class and use create method?
- Is it ok to add a new method
addCommentto PostController which will create a new Comment?
In my head adding a Comment to the Post belongs to the Post, but I really don't know.
Could some of give me some advice regarding this matter?
Thanks a lot!
Bye, Tom