-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathNewsController.java
More file actions
100 lines (91 loc) · 4.24 KB
/
NewsController.java
File metadata and controls
100 lines (91 loc) · 4.24 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.example.solidconnection.news.controller;
import com.example.solidconnection.common.resolver.AuthorizedUser;
import com.example.solidconnection.news.dto.NewsCommandResponse;
import com.example.solidconnection.news.dto.NewsCreateRequest;
import com.example.solidconnection.news.dto.NewsListResponse;
import com.example.solidconnection.news.dto.NewsUpdateRequest;
import com.example.solidconnection.news.service.NewsCommandService;
import com.example.solidconnection.news.service.NewsLikeService;
import com.example.solidconnection.news.service.NewsQueryService;
import com.example.solidconnection.security.annotation.RequireRoleAccess;
import com.example.solidconnection.siteuser.domain.Role;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequiredArgsConstructor
@RequestMapping("/news")
public class NewsController {
private final NewsQueryService newsQueryService;
private final NewsCommandService newsCommandService;
private final NewsLikeService newsLikeService;
// todo: 추후 Slice 적용
@GetMapping
public ResponseEntity<NewsListResponse> findNews(
@AuthorizedUser(required = false) Long siteUserId,
@RequestParam(value = "author-id", required = false) Long authorId
) {
NewsListResponse newsListResponse = newsQueryService.findNews(siteUserId, authorId);
return ResponseEntity.ok(newsListResponse);
}
@RequireRoleAccess(roles = {Role.ADMIN, Role.MENTOR})
@PostMapping
public ResponseEntity<NewsCommandResponse> createNews(
@AuthorizedUser long siteUserId,
@Valid @RequestPart("newsCreateRequest") NewsCreateRequest newsCreateRequest,
@RequestParam(value = "file", required = false) MultipartFile imageFile
) {
NewsCommandResponse newsCommandResponse = newsCommandService.createNews(siteUserId, newsCreateRequest, imageFile);
return ResponseEntity.ok(newsCommandResponse);
}
@RequireRoleAccess(roles = {Role.ADMIN, Role.MENTOR})
@PutMapping("/{news-id}")
public ResponseEntity<NewsCommandResponse> updateNews(
@AuthorizedUser long siteUserId,
@PathVariable("news-id") Long newsId,
@Valid @RequestPart(value = "newsUpdateRequest") NewsUpdateRequest newsUpdateRequest,
@RequestParam(value = "file", required = false) MultipartFile imageFile
) {
NewsCommandResponse newsCommandResponse = newsCommandService.updateNews(
siteUserId,
newsId,
newsUpdateRequest,
imageFile);
return ResponseEntity.ok(newsCommandResponse);
}
@RequireRoleAccess(roles = {Role.ADMIN, Role.MENTOR})
@DeleteMapping("/{news-id}")
public ResponseEntity<NewsCommandResponse> deleteNewsById(
@AuthorizedUser long siteUserId,
@PathVariable("news-id") Long newsId
) {
NewsCommandResponse newsCommandResponse = newsCommandService.deleteNewsById(siteUserId, newsId);
return ResponseEntity.ok(newsCommandResponse);
}
@PostMapping("/{news-id}/like")
public ResponseEntity<Void> addNewsLike(
@AuthorizedUser long siteUserId,
@PathVariable("news-id") Long newsId
) {
newsLikeService.addNewsLike(siteUserId, newsId);
return ResponseEntity.ok().build();
}
@DeleteMapping("/{news-id}/like")
public ResponseEntity<Void> cancelNewsLike(
@AuthorizedUser long siteUserId,
@PathVariable("news-id") Long newsId
) {
newsLikeService.cancelNewsLike(siteUserId, newsId);
return ResponseEntity.ok().build();
}
}