From ec28b99e24f036c7409c86d530e8eeecb9e9431a Mon Sep 17 00:00:00 2001 From: ls-rain Date: Thu, 15 Feb 2024 23:36:13 +0900 Subject: [PATCH 1/2] =?UTF-8?q?#127=20Feat=20:=20=EC=A0=80=EC=9E=A5?= =?UTF-8?q?=ED=95=9C=20=EA=B2=8C=EC=8B=9C=EB=AC=BC(=EB=82=B4=20=EC=B9=B4?= =?UTF-8?q?=ED=85=8C=EA=B3=A0=EB=A6=AC)=20=EC=83=81=EC=84=B8=EB=B3=B4?= =?UTF-8?q?=EA=B8=B0=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../spring/converter/MyPageConverter.java | 22 +++++++++++++++++++ .../spring/repository/PostRepository.java | 6 +++++ .../friend/spring/service/MyPageService.java | 3 +++ .../spring/service/MyPageServiceImpl.java | 14 ++++++++++++ .../web/controller/MyPageRestController.java | 22 +++++++++++++++++++ .../spring/web/dto/MyPageResponseDTO.java | 19 ++++++++++++++++ 6 files changed, 86 insertions(+) diff --git a/src/main/java/friend/spring/converter/MyPageConverter.java b/src/main/java/friend/spring/converter/MyPageConverter.java index 15aeb34..e081af8 100644 --- a/src/main/java/friend/spring/converter/MyPageConverter.java +++ b/src/main/java/friend/spring/converter/MyPageConverter.java @@ -114,4 +114,26 @@ public static MyPageResponseDTO.MyInquiryRes toMyInquiryRes(Inquiry inquiry){ .inquiry_id(inquiry.getId()) .build(); } + + public static MyPageResponseDTO.SavedPostCategoryDetailRes toSavedPostCategoryDetailRes(Post post){ + long diffTime = post.getCreatedAt().until(LocalDateTime.now(), ChronoUnit.SECONDS); // now보다 이후면 +, 전이면 - + diffTime = diffTime / SECOND; + diffTime = diffTime / MINUTE; + diffTime = diffTime / HOUR; + return MyPageResponseDTO.SavedPostCategoryDetailRes.builder() + .ago(diffTime) + .title(post.getTitle()) + .content(post.getContent()) + .postLike(post.getPostLikeList().size()) + .comment(post.getCommentList().size()) + .build(); + } + + public static MyPageResponseDTO.SavedPostCategoryDetailListRes toSavedPostCategoryDetailListRes(Page postList, Category category){ + List postCategoryDetailResList = postList.stream().map(MyPageConverter::toSavedPostCategoryDetailRes).collect(Collectors.toList()); + return MyPageResponseDTO.SavedPostCategoryDetailListRes.builder() + .name(category.getName()) + .postList(postCategoryDetailResList) + .build(); + } } diff --git a/src/main/java/friend/spring/repository/PostRepository.java b/src/main/java/friend/spring/repository/PostRepository.java index 30ce950..ddeeea8 100644 --- a/src/main/java/friend/spring/repository/PostRepository.java +++ b/src/main/java/friend/spring/repository/PostRepository.java @@ -4,6 +4,7 @@ import friend.spring.domain.Post; import friend.spring.domain.enums.PostState; import friend.spring.domain.enums.PostType; +import io.lettuce.core.dynamic.annotation.Param; import org.springframework.data.domain.Page; import friend.spring.domain.User; import org.springframework.data.domain.Page; @@ -23,4 +24,9 @@ public interface PostRepository extends JpaRepository { @Query(value = "SELECT * FROM post WHERE post.created_at >= DATE_ADD(now(), INTERVAL -7 DAY) ORDER BY post.point DESC", nativeQuery = true) Page findBestPosts(Pageable pageable); Page findAllByOrderByCreatedAtDesc(Pageable pageable); + + @Query(value = "SELECT * FROM post p join post_scrap ps ON p.user_id = ps.user_id join category c on p.category_id = c.id", nativeQuery = true) + Page findCategoryDetail(@Param("user_id") Long userId, + @Param("category_id") Long categoryId, + PageRequest pageRequest); } diff --git a/src/main/java/friend/spring/service/MyPageService.java b/src/main/java/friend/spring/service/MyPageService.java index ed1a384..f550174 100644 --- a/src/main/java/friend/spring/service/MyPageService.java +++ b/src/main/java/friend/spring/service/MyPageService.java @@ -33,4 +33,7 @@ public interface MyPageService { User editUserSecurity(Long userId, MyPageRequestDTO.ProfileEditSecurityReq profileEditSecurityReq); Inquiry createInquiry(Long userId, MyPageRequestDTO.MyInquiryReq myInquiryReq); + Page getCategoryDetailList(Long userId, Long categoryId, Integer page); + + Category getCategory(Long categoryId); } diff --git a/src/main/java/friend/spring/service/MyPageServiceImpl.java b/src/main/java/friend/spring/service/MyPageServiceImpl.java index 4ff416a..ad6aac4 100644 --- a/src/main/java/friend/spring/service/MyPageServiceImpl.java +++ b/src/main/java/friend/spring/service/MyPageServiceImpl.java @@ -169,5 +169,19 @@ public Inquiry createInquiry(Long userId, MyPageRequestDTO.MyInquiryReq myInquir return inquiryRepository.save(inquiry); } + @Override + public Page getCategoryDetailList(Long userId, Long categoryId, Integer page) { + User user = userRepository.findById(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND)); + Category category = categoryRepository.findById(categoryId).orElseThrow(() -> new GeneralException(ErrorStatus.POST_CATGORY_NOT_FOUND)); + Page detailList = postRepository.findCategoryDetail(userId, categoryId, PageRequest.of(page, 10)); + return detailList; + } + + @Override + public Category getCategory(Long categoryId) { + Category category = categoryRepository.findById(categoryId).orElseThrow(() -> new GeneralException(ErrorStatus.POST_CATGORY_NOT_FOUND)); + return category; + } + } diff --git a/src/main/java/friend/spring/web/controller/MyPageRestController.java b/src/main/java/friend/spring/web/controller/MyPageRestController.java index b010d6e..4309fd0 100644 --- a/src/main/java/friend/spring/web/controller/MyPageRestController.java +++ b/src/main/java/friend/spring/web/controller/MyPageRestController.java @@ -235,4 +235,26 @@ public ApiResponse editUserSecurity( Inquiry inquiry = myPageService.createInquiry(userId, myInquiryReq); return ApiResponse.onSuccess(MyPageConverter.toMyInquiryRes(inquiry)); } + + @GetMapping(value = "/post/{category-id}") + @Operation(summary = "사용자 글 카테고리 상세보기 조회 API", description = "카테고리별로 상세화면을 조회하는 API입니다. ex) /user/my-page/post/{category-id}") + @ApiResponses({ + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 요청에 성공했습니다. "), + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "USER4001",description = "NOT_FOUND, 사용자를 찾을 수 없습니다."), + }) + @Parameters({ + @Parameter(name = "atk", description = "RequestHeader - 로그인한 사용자의 accessToken"), + @Parameter(name = "page", description = "query string(RequestParam) - 몇번째 페이지인지 가리키는 page 변수 (0부터 시작)"), + @Parameter(name = "category-id", description = "path variable - 카테고리 아이디"), + }) + public ApiResponse GetUserCategoryDetail( + @RequestHeader(name = "atk") String atk, + @PathVariable("category-id") Long categoryId, + @RequestParam(name = "page", defaultValue = "0") Integer page, + HttpServletRequest request){ + Long userId = jwtTokenService.JwtToId(request); + Page categoryDetailList = myPageService.getCategoryDetailList(userId, categoryId, page); + Category category = myPageService.getCategory(categoryId); + return ApiResponse.onSuccess(MyPageConverter.toSavedPostCategoryDetailListRes(categoryDetailList, category)); + } } diff --git a/src/main/java/friend/spring/web/dto/MyPageResponseDTO.java b/src/main/java/friend/spring/web/dto/MyPageResponseDTO.java index f850b12..609ec99 100644 --- a/src/main/java/friend/spring/web/dto/MyPageResponseDTO.java +++ b/src/main/java/friend/spring/web/dto/MyPageResponseDTO.java @@ -99,4 +99,23 @@ public static class ProfileEditPasswordRes{ public static class MyInquiryRes{ Long inquiry_id; } + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class SavedPostCategoryDetailRes{ + Long ago; + String title; + String content; + Integer postLike; + Integer comment; + } + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class SavedPostCategoryDetailListRes{ + String name; + List postList; + } } From d94fae30e93e9170638215d4f6a598e5308adbd7 Mon Sep 17 00:00:00 2001 From: ls-rain Date: Fri, 16 Feb 2024 22:14:17 +0900 Subject: [PATCH 2/2] =?UTF-8?q?#127=20Feat=20:=20=EC=A0=80=EC=9E=A5?= =?UTF-8?q?=ED=95=9C=20=EA=B2=8C=EC=8B=9C=EB=AC=BC(=EB=82=B4=20=EC=B9=B4?= =?UTF-8?q?=ED=85=8C=EA=B3=A0=EB=A6=AC)=20=EC=83=81=EC=84=B8=EB=B3=B4?= =?UTF-8?q?=EA=B8=B0=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/friend/spring/repository/PostRepository.java | 6 ++---- .../friend/spring/web/controller/MyPageRestController.java | 2 ++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/friend/spring/repository/PostRepository.java b/src/main/java/friend/spring/repository/PostRepository.java index ddeeea8..3ac3789 100644 --- a/src/main/java/friend/spring/repository/PostRepository.java +++ b/src/main/java/friend/spring/repository/PostRepository.java @@ -25,8 +25,6 @@ public interface PostRepository extends JpaRepository { Page findBestPosts(Pageable pageable); Page findAllByOrderByCreatedAtDesc(Pageable pageable); - @Query(value = "SELECT * FROM post p join post_scrap ps ON p.user_id = ps.user_id join category c on p.category_id = c.id", nativeQuery = true) - Page findCategoryDetail(@Param("user_id") Long userId, - @Param("category_id") Long categoryId, - PageRequest pageRequest); + @Query(value = "SELECT p FROM Post p JOIN p.postScrapList s JOIN p.category c WHERE s.user.id = :userId and c.id = :categoryId") + Page findCategoryDetail(Long userId, Long categoryId, PageRequest pageRequest); } diff --git a/src/main/java/friend/spring/web/controller/MyPageRestController.java b/src/main/java/friend/spring/web/controller/MyPageRestController.java index 4309fd0..89a3f4c 100644 --- a/src/main/java/friend/spring/web/controller/MyPageRestController.java +++ b/src/main/java/friend/spring/web/controller/MyPageRestController.java @@ -241,6 +241,8 @@ public ApiResponse editUserSecurity( @ApiResponses({ @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 요청에 성공했습니다. "), @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "USER4001",description = "NOT_FOUND, 사용자를 찾을 수 없습니다."), + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "POST4004",description = "NOT_FOUND, 글에 대한 스크랩 데이터를 찾을 수 없습니다."), + }) @Parameters({ @Parameter(name = "atk", description = "RequestHeader - 로그인한 사용자의 accessToken"),