-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPostController.java
More file actions
47 lines (40 loc) · 1.67 KB
/
PostController.java
File metadata and controls
47 lines (40 loc) · 1.67 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.controllers;
import com.example.devSns.dto.PostDTO;
import com.example.devSns.dto.PostResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.example.devSns.services.PostService;
import java.util.List;
@RestController
@RequestMapping("/sns")
@RequiredArgsConstructor
public class PostController {
private final PostService postService;
@GetMapping("/show")
public ResponseEntity<List<PostResponse>> showPosts() {
List<PostResponse> posts = postService.findAll();
return new ResponseEntity<>(posts, HttpStatus.OK);
}
@GetMapping("/show/{userID}")
public ResponseEntity<List<PostResponse>> showPost(@PathVariable Long userID) {
List<PostResponse> post = postService.findByUserID(userID);
return new ResponseEntity<>(post, HttpStatus.OK);
}
@PostMapping("/add")
public ResponseEntity<PostResponse> addPost(@RequestBody PostDTO postDTO) {
PostResponse postResponse = postService.save(postDTO);
return new ResponseEntity<>(postResponse, HttpStatus.CREATED);
}
@PutMapping("/update/{id}")
public ResponseEntity<PostResponse> updatePost(@PathVariable Long id, @RequestBody PostDTO postDTO) {
PostResponse postResponse = postService.update(id, postDTO);
return new ResponseEntity<>(postResponse, HttpStatus.OK);
}
@DeleteMapping("/delete/{id}")
public ResponseEntity<String> deletePost(@PathVariable Long id) {
postService.delete(id);
return new ResponseEntity<>("Post deleted", HttpStatus.OK);
}
}