-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathPostController.java
More file actions
77 lines (68 loc) · 2.48 KB
/
PostController.java
File metadata and controls
77 lines (68 loc) · 2.48 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.example.springbootassignment.controller;
import com.example.springbootassignment.dto.PostCreateRequest;
import com.example.springbootassignment.dto.PostListResponse;
import com.example.springbootassignment.dto.PostResponse;
import com.example.springbootassignment.dto.PostUpdateRequest;
import com.example.springbootassignment.service.PostService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/posts")
public class PostController {
private final PostService postService;
public PostController(PostService postService) {
this.postService = postService;
}
/**
* 게시글 목록 조회
* GET /posts?page=1&size=10&sort=createdAt&order=desc
*/
@GetMapping
public ResponseEntity<PostListResponse> getPostList(
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "10") int size,
@RequestParam(defaultValue = "createdAt") String sort,
@RequestParam(defaultValue = "desc") String order) {
PostListResponse response = postService.getPostList(page, size, sort, order);
return ResponseEntity.ok(response);
}
/**
* 게시글 상세 조회
* GET /posts/{postId}
*/
@GetMapping("/{postId}")
public ResponseEntity<PostResponse> getPostDetail(@PathVariable Long postId) {
PostResponse response = postService.getPostDetail(postId);
return ResponseEntity.ok(response);
}
/**
* 게시글 작성
* POST /posts
*/
@PostMapping
public ResponseEntity<PostResponse> createPost(@RequestBody PostCreateRequest request) {
PostResponse response = postService.createPost(request);
return ResponseEntity.status(HttpStatus.CREATED).body(response);
}
/**
* 게시글 수정
* PUT /posts/{postId}
*/
@PutMapping("/{postId}")
public ResponseEntity<PostResponse> updatePost(
@PathVariable Long postId,
@RequestBody PostUpdateRequest request) {
PostResponse response = postService.updatePost(postId, request);
return ResponseEntity.ok(response);
}
/**
* 게시글 삭제
* DELETE /posts/{postId}
*/
@DeleteMapping("/{postId}")
public ResponseEntity<Void> deletePost(@PathVariable Long postId) {
postService.deletePost(postId);
return ResponseEntity.noContent().build();
}
}