diff --git a/src/main/java/com/example/developjeans/entity/res/GetChartResponse.java b/src/main/java/com/example/developjeans/entity/res/GetChartResponse.java index effae50..6e9045b 100644 --- a/src/main/java/com/example/developjeans/entity/res/GetChartResponse.java +++ b/src/main/java/com/example/developjeans/entity/res/GetChartResponse.java @@ -32,14 +32,14 @@ private GetChartResponse(List contents, long totalElemen } public static GetChartResponse of(ScrollPaginationCollection photoScroll, long totalElements) { - GetChartResponse GetChartResponse; + GetChartResponse response; if (photoScroll.isLastScroll()) { - GetChartResponse = newLastScroll(photoScroll.getCurrentScrollItems(), totalElements); + response = newLastScroll(photoScroll.getCurrentScrollItems(), totalElements); } else { - GetChartResponse = newScrollHasNext(photoScroll.getCurrentScrollItems(), totalElements, photoScroll.getNextCursor().getId()); + response = newScrollHasNext(photoScroll.getCurrentScrollItems(), totalElements, photoScroll.getNextCursor().getId()); } - return GetChartResponse; + return response; } private static GetChartResponse newLastScroll(List photoScroll, long totalElements) { @@ -51,9 +51,12 @@ private static GetChartResponse newScrollHasNext(List photoScroll, long t return new GetChartResponse(getContents(photoScroll), totalElements, nextCursor); } + /** + * 조회한 데이터를 클라이언트에게 전달할 데이터로 가공 + */ private static List getContents(List photoScroll) { return photoScroll.stream() - .map(feed -> new PhotoDto.PhotoChartDto(feed.getId(), feed.getImgUrl(), feed.getLikes())) + .map(photo -> new PhotoDto.PhotoChartDto(photo.getId(), photo.getImgUrl(), photo.getLikes())) .collect(Collectors.toList()); } diff --git a/src/main/java/com/example/developjeans/service/PhotoService.java b/src/main/java/com/example/developjeans/service/PhotoService.java index 6ec393f..d464399 100644 --- a/src/main/java/com/example/developjeans/service/PhotoService.java +++ b/src/main/java/com/example/developjeans/service/PhotoService.java @@ -176,15 +176,12 @@ public PhotoDto.PhotoLikeDto likePhoto(Long photoId, Long userId){ throw new BusinessLogicException(ExceptionCode.INVALID_USER_JWT); } - User _user = userRepository.findById(userId) + User user = userRepository.findById(userId) .orElseThrow(() -> new EntityNotFoundException("존재하지 않은 유저 ID: " + userId)); Photo photo = photoRepository.findById(photoId) .orElseThrow(() -> new EntityNotFoundException("존재하지 않은 사진 ID: " + photoId)); - User user = User.builder() - .id(userId) - .build(); Optional optionalLike = photoLikeRepository.findByPhotoAndUser(photo, user); @@ -193,19 +190,10 @@ public PhotoDto.PhotoLikeDto likePhoto(Long photoId, Long userId){ photoLikeRepository.delete(photoLike); photo.setLikes(photo.getLikes() - 1); String message = "좋아요 취소"; -// return new PhotoLikeRes(photoId, photo.getLikes(), message); PhotoDto.PhotoResponseDto photoResponseDto = photoMapper.toResponseDto(photo); return new PhotoDto.PhotoLikeDto(photoResponseDto.getId(), photoResponseDto.getLikes(), message); } - /* - if(photoLikeRepository.existsByPhotoAndUserAndStatus(photo, user, Status.A)) { - photoLikeRepository.deleteById(photo); - photo.setLikes(photo.getLikes() - 1); - System.out.println("좋아요가 취소되었습니다."); - return new PhotoLikeRes(photoId, photo.getLikes()); - } */ - PhotoLike photoLike = PhotoLike .builder() .user(user) @@ -219,18 +207,14 @@ public PhotoDto.PhotoLikeDto likePhoto(Long photoId, Long userId){ String message = "좋아요"; PhotoDto.PhotoResponseDto photoResponseDto = photoMapper.toResponseDto(photo); return new PhotoDto.PhotoLikeDto(photoResponseDto.getId(), photoResponseDto.getLikes(), message); -// return new PhotoLikeRes(photoId, photo.getLikes(), message); } catch (Exception e){ throw new BusinessLogicException(ExceptionCode.DATABASE_ERROR); } - - - } @Transactional(readOnly = true) - public GetChartResponse getChart(String sort, int size, Long lastPageId) { + public GetChartResponse getChart(String sort, int size, Long lastPhotoId) { // 페이지 요청 객체 생성 PageRequest pageRequest = PageRequest.of(0, size + 1); @@ -238,8 +222,8 @@ public GetChartResponse getChart(String sort, int size, Long lastPageId) { if ("likes".equals(sort) || "latest".equals(sort)) { // 정렬 기준에 따라 쿼리 실행 page = "likes".equals(sort) ? - photoRepository.findAllByOrderByLikesDesc(lastPageId, pageRequest) : - photoRepository.findAllByOrderByCreatedAtDesc(lastPageId,pageRequest); + photoRepository.findAllByOrderByLikesDesc(lastPhotoId, pageRequest) : + photoRepository.findAllByOrderByCreatedAtDesc(lastPhotoId,pageRequest); } else { // 유효하지 않은 sort값이 온 경우 예외 처리 throw new BusinessLogicException(ExceptionCode.INVALID_SORT);