-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPostController.java
More file actions
47 lines (37 loc) · 1.18 KB
/
PostController.java
File metadata and controls
47 lines (37 loc) · 1.18 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
package com.example.devSns.controller;
import com.example.devSns.entity.Post;
import com.example.devSns.service.PostService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/post")
public class PostController {
private final PostService postService;
public PostController(PostService postService) {
this.postService = postService;
}
@GetMapping
public List<Post> getAllPosts(){
return postService.findAll();
}
@GetMapping("/{id}")
public ResponseEntity<Post> getPostById(@PathVariable Long id){
Post post = postService.findById(id);
return ResponseEntity.ok(post);
}
@PostMapping
public Post createPost(@RequestBody Post post){
return postService.save(post);
}
@PutMapping("/{id}")
public Post updatePost(@PathVariable Long id, @RequestBody Post updatedPost){
return postService.updatePost(id,updatedPost);
}
@DeleteMapping("/{id}")
public void deletePost(@PathVariable Long id){
postService.delete(id);
}
}