-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPostController.java
More file actions
42 lines (33 loc) · 1.09 KB
/
PostController.java
File metadata and controls
42 lines (33 loc) · 1.09 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
package com.example.controller;
import com.example.entity.Post;
import com.example.service.PostService;
import com.example.dto.PostDto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequiredArgsConstructor
@RequestMapping("/posts")
public class PostController {
private final PostService postService;
@PostMapping
public Long createPost(@RequestBody PostDto postDto) {
return postService.createPost(postDto);
}
@GetMapping
public List<PostDto> getPosts(){
return postService.findAllPosts().stream().map(PostDto::fromEntity).toList();
}
@GetMapping("/{id}")
public PostDto getPostById(@PathVariable Long postId){
Post post = postService.getPostById(postId);
return PostDto.fromEntity(post);
}
@DeleteMapping("/{id}")
public void deletePost(@PathVariable Long postId, @RequestParam Long authorId){
postService.deletePost(postId, authorId);
}
}