Skip to content

Commit 6d0ed4f

Browse files
authored
Merge pull request #24 from DevKor-github/refactor/participant-api
Refactor/ 참가자 정보 확인 api, 투표 수 dto에 포함
2 parents d774269 + f006b47 commit 6d0ed4f

7 files changed

Lines changed: 86 additions & 15 deletions

File tree

src/main/java/com/ODG/ODG_back/controller/ParticipantController.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,26 @@ public ResponseEntity<List<ParticipantListResponseDto>> getParticipants(
8181
linkCode);
8282
return ResponseEntity.ok(participants);
8383
}
84+
85+
@GetMapping("/me")
86+
public ResponseEntity<ParticipantListResponseDto> getMyInfo(
87+
@PathVariable String linkCode,
88+
Authentication auth
89+
) {
90+
if (auth == null || auth.getPrincipal() == null) {
91+
throw new UnauthorizedException(ErrorCode.UNAUTHORIZED);
92+
}
93+
String userId = (String) auth.getPrincipal();
94+
ParticipantListResponseDto participant = participantService.getMyInfo(linkCode, userId);
95+
return ResponseEntity.ok(participant);
96+
}
97+
98+
@GetMapping("/{participantId}")
99+
public ResponseEntity<ParticipantListResponseDto> getParticipant(
100+
@PathVariable String linkCode,
101+
@PathVariable Long participantId
102+
) {
103+
ParticipantListResponseDto participant = participantService.getParticipant(linkCode, participantId);
104+
return ResponseEntity.ok(participant);
105+
}
84106
}

src/main/java/com/ODG/ODG_back/dto/place/response/PlaceResponseDto.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ public class PlaceResponseDto {
2121
private Integer slotNo;
2222
private String url;
2323
private boolean votedByMe = false;
24+
private Integer voteCount = 0;
2425

2526
public PlaceResponseDto(String id, String name, PlaceCategory category,
2627
BigDecimal lat, BigDecimal lng, String address,
27-
int slotNo, String placeUrl) {
28+
int slotNo, String placeUrl, int voteCount) {
2829
this.placeId = id;
2930
this.name = name;
3031
this.category = category;
@@ -33,6 +34,7 @@ public PlaceResponseDto(String id, String name, PlaceCategory category,
3334
this.address = address;
3435
this.slotNo = slotNo;
3536
this.url = placeUrl;
37+
this.voteCount = voteCount;
3638
}
3739

3840
}

src/main/java/com/ODG/ODG_back/dto/vote/response/VoteResultDto.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
import lombok.AllArgsConstructor;
44
import lombok.Getter;
55

6-
@Getter
76
@AllArgsConstructor
87
public class VoteResultDto {
98

9+
@Getter
1010
private int slotNo;
1111
private Long voteCount;
12+
13+
public int getVoteCount() {
14+
return voteCount.intValue();
15+
}
16+
1217
}

src/main/java/com/ODG/ODG_back/service/ParticipantService.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,31 @@ public Long getParticipantId(String userId) {
8989
.orElseThrow(() -> new NotFoundException(ErrorCode.PARTICIPANT_NOT_FOUND));
9090
return participant.getId();
9191
}
92+
93+
// 단건 참가자 조회 (공개) - linkCode 범위 내에서 participantId로 조회
94+
public ParticipantListResponseDto getParticipant(String linkCode, Long participantId) {
95+
// 해당 모임의 참가자 목록(투표 여부 포함)을 가져온 후, id로 필터링
96+
List<ParticipantListResponseDto> participants = participantRepository.findParticipantsWithVoteStatus(linkCode);
97+
return participants.stream()
98+
.filter(p -> p.getParticipantId().equals(participantId))
99+
.findFirst()
100+
.orElseThrow(() -> new NotFoundException(ErrorCode.PARTICIPANT_NOT_FOUND));
101+
}
102+
103+
// 본인 참가자 조회 (인증 필요) - linkCode + userId 기준
104+
public ParticipantListResponseDto getMyInfo(String linkCode, String userId) {
105+
// 먼저 모임을 확인하고, userId로 참가자 엔티티를 찾아 존재를 보장
106+
Meeting meeting = meetingRepository.findByInviteCode(linkCode)
107+
.orElseThrow(() -> new NotFoundException(ErrorCode.MEETING_NOT_FOUND));
108+
109+
Participant participant = participantRepository.findByUserIdAndMeeting(userId, meeting)
110+
.orElseThrow(() -> new NotFoundException(ErrorCode.PARTICIPANT_NOT_FOUND));
111+
112+
// 투표 여부 포함 DTO
113+
List<ParticipantListResponseDto> participants = participantRepository.findParticipantsWithVoteStatus(linkCode);
114+
return participants.stream()
115+
.filter(p -> p.getParticipantId().equals(participant.getId()))
116+
.findFirst()
117+
.orElseThrow(() -> new NotFoundException(ErrorCode.PARTICIPANT_NOT_FOUND));
118+
}
92119
}

src/main/java/com/ODG/ODG_back/service/PlaceService.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,20 @@ public GroupedPlacesResponse getPlacesByMidpointGrouped(
132132
}
133133
var myVoteSlotSet = new java.util.HashSet<>(myVoteSlotNos);
134134

135+
// 🔢 Prefetch vote counts for this meeting (slotNo -> count)
136+
Map<Integer, Integer> voteCountMap = new java.util.HashMap<>();
137+
try {
138+
var counts = voteRepository.findVoteCountsByMeeting(meeting); // returns DTO/projection with slotNo & voteCount(Long)
139+
for (var c : counts) {
140+
int slot = c.getSlotNo();
141+
int cnt = c.getVoteCount();
142+
voteCountMap.put(slot, cnt);
143+
}
144+
} catch (Exception e) {
145+
log.warn("[PlaceService] Failed to prefetch vote counts: {}", e.toString());
146+
}
147+
148+
135149
List<PlaceSectionDto> sections = new ArrayList<>();
136150
if (meeting.getType() == MeetingType.SOCIAL) {
137151
log.info("[PlaceService] Processing SOCIAL meeting - page={}", pageLocal);
@@ -169,6 +183,10 @@ public GroupedPlacesResponse getPlacesByMidpointGrouped(
169183
}
170184
}
171185

186+
for (PlaceResponseDto p : items) {
187+
p.setVoteCount(voteCountMap.getOrDefault(p.getSlotNo(), 0));
188+
}
189+
172190
for (PlaceResponseDto p : items) {
173191
upsertCandidate(meeting, p.getSlotNo(), sec, "category", seedJson);
174192
}
@@ -210,6 +228,10 @@ public GroupedPlacesResponse getPlacesByMidpointGrouped(
210228
}
211229
}
212230

231+
for (PlaceResponseDto p : items) {
232+
p.setVoteCount(voteCountMap.getOrDefault(p.getSlotNo(), 0));
233+
}
234+
213235
for (PlaceResponseDto p : items) {
214236
upsertCandidate(meeting, p.getSlotNo(), PlaceSection.STUDY, "keyword", projectSeed);
215237
}

src/main/java/com/ODG/ODG_back/service/PlaceSlotAssigner.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,11 @@ public List<PlaceResponseDto> assignAndBuild(
4040
doc.getAddress_name(),
4141
startSlotNo + i, // ✅ 주어진 시작 슬롯부터 연속 할당
4242
doc.getPlace_url(),
43-
false
43+
false,
44+
0
4445
));
4546
}
4647
return items;
4748
}
4849

49-
50-
static double haversine(double lat1, double lng1, double lat2, double lng2) {
51-
double R=6371000d, dLat=Math.toRadians(lat2-lat1), dLng=Math.toRadians(lng2-lng1);
52-
double a=Math.sin(dLat/2)*Math.sin(dLat/2)+
53-
Math.cos(Math.toRadians(lat1))*Math.cos(Math.toRadians(lat2))*
54-
Math.sin(dLng/2)*Math.sin(dLng/2);
55-
return 2*R*Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
56-
}
57-
5850
}

src/main/java/com/ODG/ODG_back/service/VoteService.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public List<PlaceResponseDto> getVoteResults(String inviteCode) {
122122
if (chosen == null) continue;
123123

124124
// 6) DTO로 추가 (저장 X)
125-
PlaceResponseDto dto = toDto(chosen, slotNo);
125+
PlaceResponseDto dto = toDto(chosen, slotNo, r.getVoteCount());
126126
finalPlaces.add(dto);
127127
}
128128

@@ -196,7 +196,7 @@ private KakaoPlaceDoc chooseDoc(SeedParams params, List<KakaoPlaceDoc> docs, int
196196
return chosen;
197197
}
198198

199-
private PlaceResponseDto toDto(KakaoPlaceDoc chosen, int slotNo) {
199+
private PlaceResponseDto toDto(KakaoPlaceDoc chosen, int slotNo, int voteCount) {
200200
if (chosen == null) return null;
201201
return new PlaceResponseDto(
202202
chosen.getId(),
@@ -206,7 +206,8 @@ private PlaceResponseDto toDto(KakaoPlaceDoc chosen, int slotNo) {
206206
BigDecimal.valueOf(chosen.getX()),
207207
chosen.getAddress_name(),
208208
slotNo,
209-
chosen.getPlace_url()
209+
chosen.getPlace_url(),
210+
voteCount
210211
);
211212
}
212213
}

0 commit comments

Comments
 (0)