Skip to content

Commit

Permalink
Merge pull request #136 from shared-solutions/feat#127
Browse files Browse the repository at this point in the history
#127 Feat : 저장한 게시물(내 카테고리) 상세보기 API 구현
  • Loading branch information
pswoo0323 authored Feb 16, 2024
2 parents 77365d1 + d94fae3 commit cd6004c
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/java/friend/spring/converter/MyPageConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Post> postList, Category category){
List<MyPageResponseDTO.SavedPostCategoryDetailRes> postCategoryDetailResList = postList.stream().map(MyPageConverter::toSavedPostCategoryDetailRes).collect(Collectors.toList());
return MyPageResponseDTO.SavedPostCategoryDetailListRes.builder()
.name(category.getName())
.postList(postCategoryDetailResList)
.build();
}
}
4 changes: 4 additions & 0 deletions src/main/java/friend/spring/repository/PostRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,4 +24,7 @@ public interface PostRepository extends JpaRepository<Post, Long> {
@Query(value = "SELECT * FROM post WHERE post.created_at >= DATE_ADD(now(), INTERVAL -7 DAY) ORDER BY post.point DESC", nativeQuery = true)
Page<Post> findBestPosts(Pageable pageable);
Page<Post> findAllByOrderByCreatedAtDesc(Pageable pageable);

@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<Post> findCategoryDetail(Long userId, Long categoryId, PageRequest pageRequest);
}
3 changes: 3 additions & 0 deletions src/main/java/friend/spring/service/MyPageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ public interface MyPageService {
User editUserSecurity(Long userId, MyPageRequestDTO.ProfileEditSecurityReq profileEditSecurityReq);

Inquiry createInquiry(Long userId, MyPageRequestDTO.MyInquiryReq myInquiryReq);
Page<Post> getCategoryDetailList(Long userId, Long categoryId, Integer page);

Category getCategory(Long categoryId);
}
14 changes: 14 additions & 0 deletions src/main/java/friend/spring/service/MyPageServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,19 @@ public Inquiry createInquiry(Long userId, MyPageRequestDTO.MyInquiryReq myInquir
return inquiryRepository.save(inquiry);
}

@Override
public Page<Post> 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<Post> 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;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,28 @@ public ApiResponse<MyPageResponseDTO.MyInquiryRes> 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, 사용자를 찾을 수 없습니다."),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "POST4004",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<MyPageResponseDTO.SavedPostCategoryDetailListRes> 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<Post> categoryDetailList = myPageService.getCategoryDetailList(userId, categoryId, page);
Category category = myPageService.getCategory(categoryId);
return ApiResponse.onSuccess(MyPageConverter.toSavedPostCategoryDetailListRes(categoryDetailList, category));
}
}
19 changes: 19 additions & 0 deletions src/main/java/friend/spring/web/dto/MyPageResponseDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<SavedPostCategoryDetailRes> postList;
}
}

0 comments on commit cd6004c

Please sign in to comment.