-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPostController.java
More file actions
50 lines (41 loc) · 1.53 KB
/
PostController.java
File metadata and controls
50 lines (41 loc) · 1.53 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
package com.example.devSns.controller;
import com.example.devSns.dto.PostCreateRequest;
import com.example.devSns.dto.PostResponse;
import com.example.devSns.dto.PostUpdateRequest;
import com.example.devSns.entity.Post;
import com.example.devSns.service.PostService;
import org.springframework.data.domain.Page;
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;
}
@GetMapping
public Page<Post> getPosts(@RequestParam int page, @RequestParam int size){
return PostService.FindAll(page, size);
}
@GetMapping("/{id}")
public ResponseEntity<PostResponse> getPostById(@PathVariable Long id){
Post post = PostService.FindById(id);
return ResponseEntity.ok(new PostResponse(post));
}
@PostMapping
public PostResponse createPost(@RequestBody PostCreateRequest request){
Post created = PostService.CreatePost(request);
return new PostResponse(created);
}
@PatchMapping("/{id}")
public PostResponse updatePost(@PathVariable Long id, @RequestBody PostUpdateRequest request){
Post updated = PostService.UpdatePost(id, request);
return new PostResponse(updated);
}
@DeleteMapping("/{id}")
public void deletePost(@PathVariable Long id){
PostService.delete(id);
}
}