Skip to content

Commit

Permalink
Merge pull request #79 from Alpha-Damyo/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
BlueBerrySoda authored Jun 5, 2024
2 parents 04421d9 + c2f34d7 commit a9b2c07
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.damyo.alpha.api.auth.domain.UserDetailsImpl;
import com.damyo.alpha.api.contest.service.ContestService;
import com.damyo.alpha.api.picture.controller.dto.AllRankResponse;
import com.damyo.alpha.api.picture.controller.dto.PictureSliceResponse;
import com.damyo.alpha.api.picture.service.PictureService;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -78,17 +79,16 @@ public ResponseEntity<?> unlikeContestPicture(@AuthenticationPrincipal UserDetai
.ok(200);
}

//TODO 챌린지 랭킹 기능
@PutMapping("/ranking")
@GetMapping("/ranking")
@Operation()
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "사진 URL 리스트 반환에 성공하였습니다.", content = @Content(mediaType = "application/json")),
@ApiResponse(responseCode = "200", description = "랭킹 반환에 성공하였습니다.", content = @Content(mediaType = "application/json")),
})
public ResponseEntity<?> unlikeContestPicture(@AuthenticationPrincipal UserDetailsImpl userDetails) {
UUID userId = userDetails.getId();
pictureService.getContestRanking(userId);
AllRankResponse response = pictureService.getContestRanking(userId);

return ResponseEntity
.ok(200);
.ok(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class InfoController {
private final InfoService infoService;

//TODO 리뷰 작성시 사진 저장
// TODO 리뷰 작성시 사진 저장
@PostMapping("/postInfo")
@Operation(summary="리뷰 작성하기", description = "리뷰를 작성한다.")
@ApiResponses(value = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.damyo.alpha.api.picture.controller.dto;

import java.util.List;

public record AllRankResponse(
List<LikesRankResponse> topRankResponse,
List<LikesRankResponse> nearRankResponse
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.damyo.alpha.api.picture.controller.dto;

import java.util.UUID;

public record LikesRankResponse(
UUID userId,
String name,
String profileUrl,
Long likeCount,
Long ranking
) {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.damyo.alpha.api.picture.domain;

import com.damyo.alpha.api.picture.controller.dto.LikesRankResponse;
import com.damyo.alpha.api.picture.controller.dto.PictureSliceResponse;

import java.util.List;
Expand All @@ -10,6 +11,5 @@ public interface PictureCustomRepository {
List<Picture> findPicturesBySmokingArea_Id(String areaId, Long count);
Long getLikeCountById(Long id);

void getTopRanking();
void getNearRanking(UUID userID);
List<LikesRankResponse> getRanking();
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.damyo.alpha.api.picture.domain;

import com.damyo.alpha.api.contest.domain.QContestLike;
import com.damyo.alpha.api.picture.controller.dto.LikesRankResponse;
import com.damyo.alpha.api.picture.controller.dto.PictureResponse;
import com.damyo.alpha.api.picture.controller.dto.PictureSliceResponse;
import com.damyo.alpha.api.smokingarea.domain.QSmokingArea;
import com.damyo.alpha.api.user.domain.QUser;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Projections;
Expand All @@ -15,13 +18,16 @@
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@RequiredArgsConstructor
public class PictureRepositoryImpl implements PictureCustomRepository {
private final JPAQueryFactory jpaQueryFactory;
private QPicture picture = QPicture.picture;
private QSmokingArea smokingArea = QSmokingArea.smokingArea;
private QContestLike contestLike = QContestLike.contestLike;
private QUser user = QUser.user;

@Override
public PictureSliceResponse getPictureListByPaging(Long cursorId, Long pageSize, String sortBy, String region, UUID userId) {
Expand Down Expand Up @@ -86,19 +92,26 @@ public Long getLikeCountById(Long id) {
}

@Override
public void getTopRanking() {
jpaQueryFactory
.select(picture.user.id, picture.likes.sum())
.from(picture)
public List<LikesRankResponse> getRanking() {
List<Tuple> result = jpaQueryFactory
.select(user.id, user.name, user.profileUrl, picture.likes.sum())
.from(user)
.leftJoin(picture)
.on(user.eq(picture.user))
.groupBy(user.id, user.name, user.profileUrl)
.orderBy(picture.likes.sum().desc().nullsLast())
.fetch();
}

@Override
public void getNearRanking(UUID userId) {
jpaQueryFactory
.select(picture.user.id, picture.likes.sum())
.from(picture)
.fetch();
List<LikesRankResponse> rankedUsers = IntStream.range(0, result.size())
.mapToObj(i -> new LikesRankResponse(
result.get(i).get(user.id),
result.get(i).get(user.name),
result.get(i).get(user.profileUrl),
result.get(i).get(picture.likes.sum()),
i + 1L))
.collect(Collectors.toList());

return rankedUsers;
}

private BooleanExpression cursorId(Long cursorId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.damyo.alpha.api.picture.service;

import com.damyo.alpha.api.picture.controller.dto.PictureSliceResponse;
import com.damyo.alpha.api.picture.controller.dto.*;
import com.damyo.alpha.api.picture.domain.Picture;
import com.damyo.alpha.api.picture.exception.PictureErrorCode;
import com.damyo.alpha.api.picture.exception.PictureException;
import com.damyo.alpha.api.smokingarea.domain.SmokingArea;
import com.damyo.alpha.api.user.domain.User;
import com.damyo.alpha.api.picture.controller.dto.UploadPictureRequest;
import com.damyo.alpha.api.picture.controller.dto.PictureResponse;
import com.damyo.alpha.api.picture.domain.PictureRepository;
import com.damyo.alpha.api.smokingarea.domain.SmokingAreaRepository;
import com.damyo.alpha.api.user.domain.UserRepository;
Expand All @@ -23,6 +21,8 @@

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static com.damyo.alpha.api.picture.exception.PictureErrorCode.PICTURE_NOT_FOUND;

Expand Down Expand Up @@ -83,9 +83,32 @@ public PictureSliceResponse getPageContestPicture(Long cursorId, String sortBy,
return pictureSliceResponse;
}

public void getContestRanking(UUID userId) {
pictureRepository.getTopRanking();
pictureRepository.getNearRanking(userId);
public AllRankResponse getContestRanking(UUID userId) {
List<LikesRankResponse> rank = pictureRepository.getRanking();

int targetIdx = IntStream.range(0, rank.size())
.filter(i -> rank.get(i).userId().equals(userId))
.findFirst()
.orElse(-1);

if (targetIdx == -1) {
System.out.println("out");
throw new IllegalArgumentException("User ID not found");
}

int startIndex = Math.max(0, targetIdx - 3);
int endIndex = Math.min(rank.size(), targetIdx + 4);
int topIndex = Math.min(4, rank.size());

List<LikesRankResponse> topUsers = IntStream.range(0, topIndex)
.mapToObj(rank::get)
.collect(Collectors.toList());

List<LikesRankResponse> surroundingUsers = IntStream.range(startIndex, endIndex)
.mapToObj(rank::get)
.collect(Collectors.toList());

return new AllRankResponse(topUsers, surroundingUsers);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private BooleanExpression longitudeBt(BigDecimal min, BigDecimal max) {

private BooleanExpression wordEq(String word) {
if(StringUtils.hasText(word)) {
return smokingArea.name.like(word+"%");
return smokingArea.name.like("%"+word+"%");
}
return null;
}
Expand Down

0 comments on commit a9b2c07

Please sign in to comment.