-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPostController.java
More file actions
52 lines (43 loc) · 1.57 KB
/
PostController.java
File metadata and controls
52 lines (43 loc) · 1.57 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
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 jakarta.persistence.PostUpdate;
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<PostResponse> getAllPosts(){
return postService.findAll().stream().map(PostResponse::new).toList();
}
@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);
}
}