From 3f7132074ffd4fb1f6c6aea7aa5c26adc1146876 Mon Sep 17 00:00:00 2001 From: chaeeun-Han Date: Tue, 19 Mar 2024 16:38:31 +0900 Subject: [PATCH] feat: Modify param and add try-catch (#16) Co-authored-by: Ahyun0326 --- .../controller/post/PostApiController.java | 100 +++++++++++++----- 1 file changed, 74 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/beotkkot/qtudy/controller/post/PostApiController.java b/src/main/java/com/beotkkot/qtudy/controller/post/PostApiController.java index ed8f114..20d8ad6 100644 --- a/src/main/java/com/beotkkot/qtudy/controller/post/PostApiController.java +++ b/src/main/java/com/beotkkot/qtudy/controller/post/PostApiController.java @@ -22,72 +22,120 @@ public class PostApiController { private final PostsService postsService; private final AuthService authService; - @GetMapping("/posts") - public ResponseEntity getAllPost() { - ResponseEntity response = postsService.getAllPost(); + @GetMapping("/posts/all") + public ResponseEntity getAllPost(@RequestParam("page")int page) { + ResponseEntity response = postsService.getAllPost(page); return response; } @PostMapping("/posts") public ResponseEntity save(@RequestHeader(value="Authorization") String token, @RequestBody PostsRequestDto requestDto) { - Long kakao_uid = authService.getKakaoUserInfo(token).getId(); + 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 response = postsService.savePost(kakao_uid, requestDto); return response; } - @GetMapping("/posts/{postId}") - public ResponseEntity getPost(@PathVariable Long postId) { + @GetMapping("/posts") + public ResponseEntity getPost(@RequestParam("postId") Long postId) { ResponseEntity response = postsService.getPost(postId); return response; } - @PatchMapping("/posts/{postId}") - public ResponseEntity patchPost(@PathVariable Long postId, @RequestHeader(value="Authorization") String token, @RequestBody PostsRequestDto requestDto) { - Long kakao_uid = authService.getKakaoUserInfo(token).getId(); + @PatchMapping("/posts") + public ResponseEntity 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 response = postsService.patchPost(postId, kakao_uid, requestDto); return response; } - @DeleteMapping("/posts/{postId}") - public ResponseEntity deletePost(@PathVariable Long postId, @RequestHeader(value="Authorization") String token) { - Long kakao_uid = authService.getKakaoUserInfo(token).getId(); + @DeleteMapping("/posts") + public ResponseEntity 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 response = postsService.deletePost(postId, kakao_uid); return response; } @GetMapping("/posts/my-post-list") - public ResponseEntity getMyPost(@RequestHeader(value="Authorization") String token) { - Long kakao_uid = authService.getKakaoUserInfo(token).getId(); - ResponseEntity response = postsService.getMyPost(kakao_uid); + public ResponseEntity 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 response = postsService.getMyPost(kakao_uid, page); return response; } - @GetMapping("/posts/search-list/{searchWord}") - public ResponseEntity getSearchPost(@PathVariable String searchWord) { - ResponseEntity response = postsService.getSearchPost(searchWord); + @GetMapping("/posts/search-list") + public ResponseEntity getSearchPost(@RequestParam("searchWord") String searchWord, @RequestParam("page")int page) { + ResponseEntity response = postsService.getSearchPost(searchWord, page); return response; } @GetMapping("/posts/category-list") - public ResponseEntity searchPostsByCategory(@RequestParam("category") List categories) { - ResponseEntity response = postsService.getCategorySearchPost(categories); + public ResponseEntity searchPostsByCategory(@RequestParam("categoryId") List categories, @RequestParam("page")int page) { + ResponseEntity response = postsService.getCategorySearchPost(categories, page); return response; } // 스크랩 - @PutMapping("/posts/{postId}/scrap") - public ResponseEntity putScrap(@PathVariable("postId") Long postId, @RequestHeader(value="Authorization") String token) { - Long kakao_uid = authService.getKakaoUserInfo(token).getId(); + @PutMapping("/posts/scrap") + public ResponseEntity 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 response = postsService.putScrap(postId, kakao_uid); return response; } @GetMapping("/posts/scrap-list") - public ResponseEntity getAllScrapPost(@RequestHeader(value="Authorization") String token) { - Long kakao_uid = authService.getKakaoUserInfo(token).getId(); - ResponseEntity response = postsService.getAllScrapPost(kakao_uid); + public ResponseEntity 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 response = postsService.getAllScrapPost(kakao_uid, page); return response; } }