-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostApiController.java
156 lines (139 loc) ยท 6.68 KB
/
PostApiController.java
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.beotkkot.qtudy.controller.post;
import com.beotkkot.qtudy.dto.request.posts.PostsRequestDto;
import com.beotkkot.qtudy.dto.response.posts.GetPostsAllResponseDto;
import com.beotkkot.qtudy.dto.response.posts.GetPostsResponseDto;
import com.beotkkot.qtudy.dto.response.posts.PostsResponseDto;
import com.beotkkot.qtudy.dto.response.posts.PutScrapResponseDto;
import com.beotkkot.qtudy.service.posts.PostsService;
import com.beotkkot.qtudy.service.auth.AuthService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Slf4j
@RequiredArgsConstructor
@RestController
public class PostApiController {
private final PostsService postsService;
private final AuthService authService;
@GetMapping("/posts/all")
public ResponseEntity<? super GetPostsAllResponseDto> getAllPost(@RequestParam("page")int page) {
ResponseEntity<? super GetPostsAllResponseDto> response = postsService.getAllPost(page);
return response;
}
@PostMapping("/posts")
public ResponseEntity<? super PostsResponseDto> save(@RequestHeader(value="Authorization") String token, @RequestBody PostsRequestDto requestDto) {
Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
if (kakao_uid == null)
return PostsResponseDto.noAuthentication();
} catch (Exception exception) {
log.info(exception.getMessage());
return PostsResponseDto.databaseError();
}
ResponseEntity<? super PostsResponseDto> response = postsService.savePost(kakao_uid, requestDto);
return response;
}
@GetMapping("/posts")
public ResponseEntity<? super GetPostsResponseDto> getPost(@RequestParam("postId") Long postId) {
ResponseEntity<? super GetPostsResponseDto> response = postsService.getPost(postId);
return response;
}
@PatchMapping("/posts")
public ResponseEntity<? super PostsResponseDto> patchPost(@RequestParam("postId") Long postId, @RequestHeader(value="Authorization") String token, @RequestBody PostsRequestDto requestDto) {
Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
if (kakao_uid == null)
return PostsResponseDto.noAuthentication();
} catch (Exception exception) {
log.info(exception.getMessage());
return PostsResponseDto.databaseError();
}
ResponseEntity<? super PostsResponseDto> response = postsService.patchPost(postId, kakao_uid, requestDto);
return response;
}
@DeleteMapping("/posts")
public ResponseEntity<? super PostsResponseDto> deletePost(@RequestParam("postId") Long postId, @RequestHeader(value="Authorization") String token) {
Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
if (kakao_uid == null)
return PostsResponseDto.noAuthentication();
} catch (Exception exception) {
log.info(exception.getMessage());
return PostsResponseDto.databaseError();
}
ResponseEntity<? super PostsResponseDto> response = postsService.deletePost(postId, kakao_uid);
return response;
}
@GetMapping("/posts/my-post-list")
public ResponseEntity<? super GetPostsAllResponseDto> getMyPost(@RequestHeader(value="Authorization") String token, @RequestParam("page")int page) {
Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
if (kakao_uid == null)
return PostsResponseDto.noAuthentication();
} catch (Exception exception) {
log.info(exception.getMessage());
return PostsResponseDto.databaseError();
}
ResponseEntity<? super GetPostsAllResponseDto> response = postsService.getMyPost(kakao_uid, page);
return response;
}
@GetMapping("/posts/search-list")
public ResponseEntity<? super GetPostsAllResponseDto> getSearchPost(@RequestParam("searchWord") String searchWord, @RequestParam("page")int page) {
ResponseEntity<? super GetPostsAllResponseDto> response = postsService.getSearchPost(searchWord, page);
return response;
}
@GetMapping("/posts/category-list")
public ResponseEntity<? super GetPostsAllResponseDto> searchPostsByCategory(@RequestParam("categoryId") List<Long> categories, @RequestParam("page")int page) {
ResponseEntity<? super GetPostsAllResponseDto> response = postsService.getCategorySearchPost(categories, page);
return response;
}
// ์คํฌ๋ฉ
@PutMapping("/posts/scrap")
public ResponseEntity<? super PutScrapResponseDto> putScrap(@RequestParam("postId") Long postId, @RequestHeader(value="Authorization") String token) {
Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
if (kakao_uid == null)
return PostsResponseDto.noAuthentication();
} catch (Exception exception) {
log.info(exception.getMessage());
return PostsResponseDto.databaseError();
}
ResponseEntity<? super PutScrapResponseDto> response = postsService.putScrap(postId, kakao_uid);
return response;
}
@GetMapping("/posts/scrap-list")
public ResponseEntity<? super GetPostsAllResponseDto> getAllScrapPost(@RequestHeader(value="Authorization") String token, @RequestParam("page")int page) {
Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
if (kakao_uid == null)
return PostsResponseDto.noAuthentication();
} catch (Exception exception) {
log.info(exception.getMessage());
return PostsResponseDto.databaseError();
}
ResponseEntity<? super GetPostsAllResponseDto> response = postsService.getAllScrapPost(kakao_uid, page);
return response;
}
@GetMapping("/posts/all-scrap-list")
public ResponseEntity<? super GetPostsAllResponseDto> getAllScrapPostNoPage(@RequestHeader(value="Authorization") String token) {
Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
if (kakao_uid == null)
return PostsResponseDto.noAuthentication();
} catch (Exception exception) {
log.info(exception.getMessage());
return PostsResponseDto.databaseError();
}
ResponseEntity<? super GetPostsAllResponseDto> response = postsService.getAllScrapPostNoPage(kakao_uid);
return response;
}
}