Skip to content

Commit

Permalink
Merge pull request #111 from TeamPINGLE/feat/109
Browse files Browse the repository at this point in the history
[feat] 장소 랭킹 api 생성
  • Loading branch information
Parkjyun authored Feb 15, 2024
2 parents 677b568 + c8cafcf commit 30c238b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.pingle.pingleserver.dto.common.ApiResponse;
import org.pingle.pingleserver.dto.reponse.MeetingResponse;
import org.pingle.pingleserver.dto.reponse.PinResponse;
import org.pingle.pingleserver.dto.response.RankingResponse;
import org.pingle.pingleserver.dto.type.SuccessMessage;
import org.pingle.pingleserver.service.PinService;
import org.springframework.web.bind.annotation.*;
Expand All @@ -34,4 +35,10 @@ public ApiResponse<List<MeetingResponse>> getMeetings(@UserId Long userId,
@Nullable @RequestParam MCategory category) {
return ApiResponse.success(SuccessMessage.OK, pinService.getMeetings(pinId, userId, category));
}

@GetMapping("/ranking")
public ApiResponse<RankingResponse> getRankings(@PathVariable Long teamId) {
return ApiResponse.success(SuccessMessage.OK, pinService.getRankings(teamId));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.pingle.pingleserver.dto.response;

import java.time.LocalDateTime;

public record RankingIndividualResponse (String name, LocalDateTime latestVisitedDate, Long locationCount) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.pingle.pingleserver.dto.response;

import java.util.List;

public record RankingResponse (int meetingCount, List<RankingIndividualResponse> locations) {
public static RankingResponse of (List<RankingIndividualResponse> responses) {
return new RankingResponse(getSumOfMeetings(responses), responses);
}

private static int getSumOfMeetings (List<RankingIndividualResponse> responses) {
int sum = 0;
for (RankingIndividualResponse response : responses) {
sum += response.locationCount();
}
return sum;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.pingle.pingleserver.domain.Team;
import org.pingle.pingleserver.domain.Point;
import org.pingle.pingleserver.domain.enums.MCategory;
import org.pingle.pingleserver.dto.response.RankingIndividualResponse;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

Expand All @@ -17,6 +18,12 @@ public interface PinRepository extends JpaRepository<Pin, Long> {
List<Pin> findPinsWithCategoryAndTimeBefore(Long teamId, MCategory category);
@Query("SELECT DISTINCT p FROM Pin p WHERE p.team.id = :teamId AND p.id IN (SELECT m.pin.id FROM Meeting m WHERE m.startAt > CURRENT_TIMESTAMP)")
List<Pin> findPinsAndTimeBefore(Long teamId);
@Query("select new org.pingle.pingleserver.dto.response.RankingIndividualResponse(p.name, max(m.endAt), count(p)) " +
"from Meeting m join m.pin p " +
"WHERE m.endAt < CURRENT_TIMESTAMP AND p.team.id = :teamId " +
"group by p " +
"order by count(p) desc, max(m.endAt) desc")
List<RankingIndividualResponse> findPinsWithMeetingsBeforeCurrentTimestampAndTeamId(Long teamId);

// todo: use native query to solve n+1 problem
// @Query(value = "SELECT p.*, COUNT(m.id) FROM pin p LEFT JOIN meeting m ON p.id = m.pin_id WHERE p.team_id = :teamId AND m.start_at > CURRENT_TIMESTAMP AND m.category = :category GROUP BY p.id", nativeQuery = true)
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/pingle/pingleserver/service/PinService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.pingle.pingleserver.domain.Address;
import org.pingle.pingleserver.domain.Point;
import org.pingle.pingleserver.dto.request.MeetingRequest;
import org.pingle.pingleserver.dto.response.RankingIndividualResponse;
import org.pingle.pingleserver.dto.response.RankingResponse;
import org.pingle.pingleserver.dto.type.ErrorMessage;
import org.pingle.pingleserver.exception.CustomException;
import org.pingle.pingleserver.repository.MeetingRepository;
Expand Down Expand Up @@ -161,4 +163,10 @@ private boolean isParticipating(Long userId, Meeting meeting) {
private boolean isOwner(Long userId, Long meetingId) {
return userMeetingRepository.existsByUserIdAndMeetingIdAndMeetingRole(userId, meetingId, MRole.OWNER);
}

public RankingResponse getRankings(Long teamId) {
if(!teamRepository.existsById(teamId)) throw new CustomException(ErrorMessage.RESOURCE_NOT_FOUND);
List<RankingIndividualResponse> response = pinRepository.findPinsWithMeetingsBeforeCurrentTimestampAndTeamId(teamId);
return RankingResponse.of(response);
}
}

0 comments on commit 30c238b

Please sign in to comment.