Skip to content

Commit 0b636f2

Browse files
committed
Refactor[poll]:pollvote object->dto
1 parent 1066851 commit 0b636f2

File tree

4 files changed

+108
-65
lines changed

4 files changed

+108
-65
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.ai.lawyer.domain.poll.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
import lombok.Builder;
7+
8+
@Data
9+
@AllArgsConstructor
10+
@NoArgsConstructor
11+
@Builder
12+
public class PollTopDto {
13+
private Long pollId;
14+
private Long voteCount;
15+
}
16+

backend/src/main/java/com/ai/lawyer/domain/poll/repository/PollVoteRepositoryCustom.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
import com.ai.lawyer.domain.poll.entity.PollVote;
55
import org.springframework.data.domain.Pageable;
66
import java.util.List;
7+
import com.ai.lawyer.domain.poll.dto.PollAgeStaticsDto;
8+
import com.ai.lawyer.domain.poll.dto.PollGenderStaticsDto;
9+
import com.ai.lawyer.domain.poll.dto.PollTopDto;
10+
import com.ai.lawyer.domain.poll.dto.PollStaticsDto;
711

812
public interface PollVoteRepositoryCustom {
9-
List<Object[]> findTopPollByStatus(Poll.PollStatus status);
10-
List<Object[]> findTopNPollByStatus(Poll.PollStatus status, Pageable pageable);
13+
List<PollTopDto> findTopPollByStatus(Poll.PollStatus status);
14+
List<PollTopDto> findTopNPollByStatus(Poll.PollStatus status, Pageable pageable);
1115
Long countByPollId(Long pollId);
1216
Long countByPollOptionId(Long pollOptionId);
13-
List<Object[]> countStaticsByPollOptionIds(List<Long> pollOptionIds);
14-
List<Object[]> getOptionAgeStatics(Long pollId);
15-
List<Object[]> getOptionGenderStatics(Long pollId);
17+
List<PollAgeStaticsDto.AgeGroupCountDto> getOptionAgeStatics(Long pollId);
18+
List<PollGenderStaticsDto.GenderCountDto> getOptionGenderStatics(Long pollId);
19+
List<PollStaticsDto> countStaticsByPollOptionIds(List<Long> pollOptionIds);
1620
}

backend/src/main/java/com/ai/lawyer/domain/poll/repository/PollVoteRepositoryImpl.java

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ai.lawyer.domain.poll.repository;
22

3+
import com.ai.lawyer.domain.member.entity.Member;
34
import com.ai.lawyer.domain.poll.entity.Poll;
45
import com.ai.lawyer.domain.poll.entity.QPoll;
56
import com.ai.lawyer.domain.poll.entity.QPollOptions;
@@ -13,6 +14,11 @@
1314

1415
import java.util.List;
1516

17+
import com.ai.lawyer.domain.poll.dto.PollAgeStaticsDto.AgeGroupCountDto;
18+
import com.ai.lawyer.domain.poll.dto.PollGenderStaticsDto.GenderCountDto;
19+
import com.ai.lawyer.domain.poll.dto.PollTopDto;
20+
import com.ai.lawyer.domain.poll.dto.PollStaticsDto;
21+
1622
@Repository
1723
@RequiredArgsConstructor
1824
public class PollVoteRepositoryImpl implements PollVoteRepositoryCustom {
@@ -23,19 +29,24 @@ public class PollVoteRepositoryImpl implements PollVoteRepositoryCustom {
2329
private final QMember member = QMember.member;
2430

2531
@Override
26-
public List<Object[]> findTopPollByStatus(Poll.PollStatus status) {
32+
public List<PollTopDto> findTopPollByStatus(Poll.PollStatus status) {
2733
List<Tuple> tuples = queryFactory.select(poll.getPollId(), pollVote.count())
2834
.from(pollVote)
2935
.join(pollVote.getPoll(), poll)
3036
.where(poll.getStatus().eq(status))
3137
.groupBy(poll.getPollId())
3238
.orderBy(pollVote.count().desc())
3339
.fetch();
34-
return tuples.stream().map(Tuple::toArray).toList();
40+
return tuples.stream()
41+
.map(t -> new PollTopDto(
42+
t.get(0, Long.class),
43+
t.get(1, Long.class)
44+
))
45+
.toList();
3546
}
3647

3748
@Override
38-
public List<Object[]> findTopNPollByStatus(Poll.PollStatus status, Pageable pageable) {
49+
public List<PollTopDto> findTopNPollByStatus(Poll.PollStatus status, Pageable pageable) {
3950
List<Tuple> tuples = queryFactory.select(poll.getPollId(), pollVote.count())
4051
.from(pollVote)
4152
.join(pollVote.getPoll(), poll)
@@ -45,7 +56,12 @@ public List<Object[]> findTopNPollByStatus(Poll.PollStatus status, Pageable page
4556
.offset(pageable.getOffset())
4657
.limit(pageable.getPageSize())
4758
.fetch();
48-
return tuples.stream().map(Tuple::toArray).toList();
59+
return tuples.stream()
60+
.map(t -> new PollTopDto(
61+
t.get(0, Long.class),
62+
t.get(1, Long.class)
63+
))
64+
.toList();
4965
}
5066

5167
@Override
@@ -67,19 +83,47 @@ public Long countByPollOptionId(Long pollOptionId) {
6783
}
6884

6985
@Override
70-
public List<Object[]> countStaticsByPollOptionIds(List<Long> pollOptionIds) {
71-
List<Tuple> tuples = queryFactory.select(pollOptions.getPollItemsId(), member.getGender(), member.getAge(), pollVote.count())
86+
public List<PollStaticsDto> countStaticsByPollOptionIds(List<Long> pollOptionIds) {
87+
List<Tuple> tuples = queryFactory.select(
88+
pollOptions.getPollItemsId(),
89+
member.getGender(),
90+
member.getAge(),
91+
pollVote.count())
7292
.from(pollVote)
7393
.join(pollVote.getPollOptions(), pollOptions)
7494
.join(pollVote.getMember(), member)
7595
.where(pollOptions.getPollItemsId().in(pollOptionIds))
7696
.groupBy(pollOptions.getPollItemsId(), member.getGender(), member.getAge())
7797
.fetch();
78-
return tuples.stream().map(Tuple::toArray).toList();
98+
return tuples.stream()
99+
.map(t -> {
100+
String gender = t.get(1, String.class);
101+
Integer age = t.get(2, Integer.class);
102+
String ageGroup = getAgeGroup(age);
103+
Long voteCount = t.get(3, Long.class);
104+
return PollStaticsDto.builder()
105+
.gender(gender)
106+
.ageGroup(ageGroup)
107+
.voteCount(voteCount)
108+
.build();
109+
})
110+
.toList();
111+
}
112+
113+
private String getAgeGroup(Integer age) {
114+
if (age == null) return "기타";
115+
if (age < 20) return "10대";
116+
if (age < 30) return "20대";
117+
if (age < 40) return "30대";
118+
if (age < 50) return "40대";
119+
if (age < 60) return "50대";
120+
if (age < 70) return "60대";
121+
if (age < 80) return "70대";
122+
return "80대 이상";
79123
}
80124

81125
@Override
82-
public List<Object[]> getOptionAgeStatics(Long pollId) {
126+
public List<AgeGroupCountDto> getOptionAgeStatics(Long pollId) {
83127
List<Tuple> tuples = queryFactory.select(
84128
pollOptions.getOption(),
85129
new com.querydsl.core.types.dsl.CaseBuilder()
@@ -107,11 +151,17 @@ public List<Object[]> getOptionAgeStatics(Long pollId) {
107151
.when(member.getAge().lt(80)).then("70대")
108152
.otherwise("80대 이상"))
109153
.fetch();
110-
return tuples.stream().map(Tuple::toArray).toList();
154+
return tuples.stream()
155+
.map(t -> new AgeGroupCountDto(
156+
t.get(0, String.class),
157+
t.get(1, String.class),
158+
t.get(2, Long.class)
159+
))
160+
.toList();
111161
}
112162

113163
@Override
114-
public List<Object[]> getOptionGenderStatics(Long pollId) {
164+
public List<GenderCountDto> getOptionGenderStatics(Long pollId) {
115165
List<Tuple> tuples = queryFactory.select(
116166
pollOptions.getOption(),
117167
member.getGender(),
@@ -122,6 +172,12 @@ public List<Object[]> getOptionGenderStatics(Long pollId) {
122172
.where(pollOptions.getPoll().getPollId().eq(pollId))
123173
.groupBy(pollOptions.getOption(), member.getGender())
124174
.fetch();
125-
return tuples.stream().map(Tuple::toArray).toList();
175+
return tuples.stream()
176+
.map(t -> new GenderCountDto(
177+
t.get(0, String.class),
178+
t.get(1, Member.Gender.class).name(),
179+
t.get(2, Long.class)
180+
))
181+
.toList();
126182
}
127183
}

backend/src/main/java/com/ai/lawyer/domain/poll/service/PollServiceImpl.java

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.ai.lawyer.domain.poll.service;
22

3+
import com.ai.lawyer.domain.poll.dto.*;
34
import com.ai.lawyer.domain.poll.entity.*;
45
import com.ai.lawyer.domain.poll.repository.*;
5-
import com.ai.lawyer.domain.poll.dto.PollDto;
66
import com.ai.lawyer.domain.member.entity.Member;
77
import com.ai.lawyer.domain.post.dto.PostDto;
88
import com.ai.lawyer.domain.post.entity.Post;
@@ -16,21 +16,12 @@
1616
import org.springframework.web.server.ResponseStatusException;
1717

1818
import java.util.*;
19-
import com.ai.lawyer.domain.poll.dto.PollCreateDto;
20-
import com.ai.lawyer.domain.poll.dto.PollForPostDto;
21-
import com.ai.lawyer.domain.poll.dto.PollOptionCreateDto;
22-
import com.ai.lawyer.domain.poll.dto.PollStaticsDto;
23-
import com.ai.lawyer.domain.poll.dto.PollOptionDto;
24-
import com.ai.lawyer.domain.poll.dto.PollVoteDto;
25-
import com.ai.lawyer.domain.poll.dto.PollUpdateDto;
19+
2620
import com.ai.lawyer.domain.poll.entity.Poll;
2721

2822
import org.springframework.data.domain.Pageable;
2923
import java.time.LocalDateTime;
3024

31-
import com.ai.lawyer.domain.poll.dto.PollGenderStaticsDto;
32-
import com.ai.lawyer.domain.poll.dto.PollStaticsResponseDto;
33-
import com.ai.lawyer.domain.poll.dto.PollAgeStaticsDto;
3425
import com.ai.lawyer.global.util.AuthUtil;
3526

3627
@Service
@@ -178,19 +169,12 @@ public PollStaticsResponseDto getPollStatics(Long pollId) {
178169
PollOptions opt = options.get(i);
179170
optionMap.put(opt.getOption(), opt);
180171
}
181-
// age 통계 그룹핑
182-
List<Object[]> optionAgeRaw = pollVoteRepository.getOptionAgeStatics(pollId);
172+
List<PollAgeStaticsDto.AgeGroupCountDto> optionAgeRaw = pollVoteRepository.getOptionAgeStatics(pollId);
183173
java.util.Map<Long, java.util.List<PollAgeStaticsDto.AgeGroupCountDto>> ageGroupMap = new java.util.HashMap<>();
184-
for (Object[] arr : optionAgeRaw) {
185-
String option = arr[0] != null ? arr[0].toString() : null;
186-
PollOptions opt = optionMap.get(option);
174+
for (PollAgeStaticsDto.AgeGroupCountDto dto : optionAgeRaw) {
175+
PollOptions opt = optionMap.get(dto.getOption());
187176
if (opt == null) continue;
188177
Long pollItemsId = opt.getPollItemsId();
189-
PollAgeStaticsDto.AgeGroupCountDto dto = PollAgeStaticsDto.AgeGroupCountDto.builder()
190-
.option(option)
191-
.ageGroup(arr[1] != null ? arr[1].toString() : null)
192-
.voteCount(arr[2] != null ? ((Number) arr[2]).longValue() : 0L)
193-
.build();
194178
ageGroupMap.computeIfAbsent(pollItemsId, k -> new java.util.ArrayList<>()).add(dto);
195179
}
196180
java.util.List<PollAgeStaticsDto> optionAgeStatics = new java.util.ArrayList<>();
@@ -202,19 +186,12 @@ public PollStaticsResponseDto getPollStatics(Long pollId) {
202186
.ageGroupCounts(ageGroupMap.getOrDefault(opt.getPollItemsId(), java.util.Collections.emptyList()))
203187
.build());
204188
}
205-
// gender 통계 그룹핑
206-
List<Object[]> optionGenderRaw = pollVoteRepository.getOptionGenderStatics(pollId);
189+
List<PollGenderStaticsDto.GenderCountDto> optionGenderRaw = pollVoteRepository.getOptionGenderStatics(pollId);
207190
java.util.Map<Long, java.util.List<PollGenderStaticsDto.GenderCountDto>> genderGroupMap = new java.util.HashMap<>();
208-
for (Object[] arr : optionGenderRaw) {
209-
String option = arr[0] != null ? arr[0].toString() : null;
210-
PollOptions opt = optionMap.get(option);
191+
for (PollGenderStaticsDto.GenderCountDto dto : optionGenderRaw) {
192+
PollOptions opt = optionMap.get(dto.getOption());
211193
if (opt == null) continue;
212194
Long pollItemsId = opt.getPollItemsId();
213-
PollGenderStaticsDto.GenderCountDto dto = PollGenderStaticsDto.GenderCountDto.builder()
214-
.option(option)
215-
.gender(arr[1] != null ? arr[1].toString() : null)
216-
.voteCount(arr[2] != null ? ((Number) arr[2]).longValue() : 0L)
217-
.build();
218195
genderGroupMap.computeIfAbsent(pollItemsId, k -> new java.util.ArrayList<>()).add(dto);
219196
}
220197
java.util.List<PollGenderStaticsDto> optionGenderStatics = new java.util.ArrayList<>();
@@ -236,7 +213,7 @@ public PollStaticsResponseDto getPollStatics(Long pollId) {
236213
.build();
237214
}
238215

239-
// 최대 7일 동안 투표 가능 (초기 요구사항)
216+
// 최대 7일 동안 투표 가능
240217
@Override
241218
public void closePoll(Long pollId) {
242219
Poll poll = pollRepository.findById(pollId)
@@ -268,7 +245,7 @@ public void deletePoll(Long pollId, Long memberId) {
268245

269246
@Override
270247
public PollDto getTopPollByStatus(PollDto.PollStatus status, Long memberId) {
271-
List<Object[]> result = pollVoteRepository.findTopPollByStatus(Poll.PollStatus.valueOf(status.name()));
248+
List<PollTopDto> result = pollVoteRepository.findTopPollByStatus(Poll.PollStatus.valueOf(status.name()));
272249
if (result.isEmpty()) {
273250
// 종료된 투표가 없으면 빈 PollDto 반환
274251
return PollDto.builder()
@@ -282,18 +259,18 @@ public PollDto getTopPollByStatus(PollDto.PollStatus status, Long memberId) {
282259
.totalVoteCount(0L)
283260
.build();
284261
}
285-
Long pollId = (Long) result.get(0)[0];
262+
Long pollId = result.get(0).getPollId();
286263
return getPoll(pollId, memberId);
287264
}
288265

289266
@Override
290267
public List<PollDto> getTopNPollsByStatus(PollDto.PollStatus status, int n, Long memberId) {
291268
Pageable pageable = org.springframework.data.domain.PageRequest.of(0, n);
292-
List<Object[]> result = pollVoteRepository.findTopNPollByStatus(
269+
List<PollTopDto> result = pollVoteRepository.findTopNPollByStatus(
293270
com.ai.lawyer.domain.poll.entity.Poll.PollStatus.valueOf(status.name()), pageable);
294271
List<PollDto> pollDtos = new java.util.ArrayList<>();
295-
for (Object[] row : result) {
296-
Long pollId = (Long) row[0];
272+
for (PollTopDto row : result) {
273+
Long pollId = row.getPollId();
297274
pollDtos.add(status == PollDto.PollStatus.CLOSED
298275
? getPollWithStatistics(pollId, memberId)
299276
: getPoll(pollId, memberId));
@@ -455,18 +432,8 @@ private PollDto convertToDto(Poll poll, Long memberId, boolean withStatistics) {
455432
}
456433
List<PollStaticsDto> statics = null;
457434
if (withStatistics && poll.getStatus() == Poll.PollStatus.CLOSED) {
458-
List<Object[]> staticsRaw = pollVoteRepository.countStaticsByPollOptionIds(List.of(option.getPollItemsId()));
459-
statics = staticsRaw.stream()
460-
.map(arr -> {
461-
String gender = arr[1] != null ? arr[1].toString() : null;
462-
Integer age = arr[2] != null ? ((Number) arr[2]).intValue() : null;
463-
String ageGroup = getAgeGroup(age);
464-
return PollStaticsDto.builder()
465-
.gender(gender)
466-
.ageGroup(ageGroup)
467-
.voteCount((Long) arr[3])
468-
.build();
469-
}).toList();
435+
List<PollStaticsDto> staticsRaw = pollVoteRepository.countStaticsByPollOptionIds(List.of(option.getPollItemsId()));
436+
statics = staticsRaw;
470437
}
471438
optionDtos.add(PollOptionDto.builder()
472439
.pollItemsId(option.getPollItemsId())

0 commit comments

Comments
 (0)