-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPostController.java
More file actions
51 lines (42 loc) · 1.51 KB
/
PostController.java
File metadata and controls
51 lines (42 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.example.devSns.controller;
import com.example.devSns.entity.Post;
import com.example.devSns.service.PostService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/posts")
public class PostController {
private final PostService postService;
public PostController(PostService postService) {
this.postService = postService;
}
@PostMapping
public ResponseEntity<Post> createPost(@RequestBody Post post) {
Post createdPost = postService.createPost(post);
return ResponseEntity.status(HttpStatus.CREATED).body(createdPost);
}
@GetMapping
public ResponseEntity<List<Post>> getAllPosts() {
List<Post> posts = postService.getAllPosts();
return ResponseEntity.ok(posts);
}
@GetMapping("/{id}")
public ResponseEntity<Post> getPostById(@PathVariable Long id) {
Post post = postService.getPostById(id);
return ResponseEntity.ok(post);
}
@PutMapping("/{id}")
public ResponseEntity<Post> updatePost(
@PathVariable Long id,
@RequestBody Post updatedPost) {
Post post = postService.updatePost(id, updatedPost);
return ResponseEntity.ok(post);
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deletePost(@PathVariable Long id) {
postService.deletePost(id);
return ResponseEntity.noContent().build();
}
}