1
+ package com .moplus .moplus_server .domain .problemset .repository ;
2
+
3
+ import com .moplus .moplus_server .admin .problemset .dto .response .ProblemSetGetResponse ;
4
+ import com .moplus .moplus_server .admin .problemset .dto .response .ProblemSummaryResponse ;
5
+ import com .moplus .moplus_server .admin .publish .domain .QPublish ;
6
+ import com .moplus .moplus_server .domain .concept .domain .QConceptTag ;
7
+ import com .moplus .moplus_server .domain .problem .domain .problem .QProblem ;
8
+ import com .moplus .moplus_server .domain .problemset .domain .ProblemSet ;
9
+ import com .querydsl .core .Tuple ;
10
+ import com .querydsl .jpa .impl .JPAQueryFactory ;
11
+ import java .time .LocalDate ;
12
+ import java .util .*;
13
+ import lombok .RequiredArgsConstructor ;
14
+ import org .springframework .stereotype .Repository ;
15
+
16
+ @ Repository
17
+ @ RequiredArgsConstructor
18
+ public class ProblemSetGetRepositoryCustom {
19
+
20
+ private final JPAQueryFactory queryFactory ;
21
+
22
+ public ProblemSetGetResponse getProblemSet (ProblemSet problemSet ) {
23
+ // 발행 날짜 조회 쿼리
24
+ List <LocalDate > publishedDates = queryFactory
25
+ .select (QPublish .publish .publishedDate )
26
+ .from (QPublish .publish )
27
+ .where (QPublish .publish .problemSetId .eq (problemSet .getId ()))
28
+ .fetch ();
29
+
30
+ // 문제 조회 쿼리 (문제 자체 정보만 조회)
31
+ List <Tuple > problemData = queryFactory
32
+ .select (
33
+ QProblem .problem .id ,
34
+ QProblem .problem .problemCustomId ,
35
+ QProblem .problem .title ,
36
+ QProblem .problem .memo ,
37
+ QProblem .problem .mainProblemImageUrl
38
+ )
39
+ .from (QProblem .problem )
40
+ .where (QProblem .problem .id .in (problemSet .getProblemIds ()))
41
+ .distinct ()
42
+ .fetch ();
43
+
44
+ // 태그 조회 쿼리 (각 문제별 태그만 조회)
45
+ Map <Long , Set <String >> conceptTagMap = queryFactory
46
+ .select (QProblem .problem .id , QConceptTag .conceptTag .name )
47
+ .from (QProblem .problem )
48
+ .leftJoin (QConceptTag .conceptTag )
49
+ .on (QConceptTag .conceptTag .id .in (QProblem .problem .conceptTagIds ))
50
+ .where (QProblem .problem .id .in (problemSet .getProblemIds ()))
51
+ .fetch ()
52
+ .stream ()
53
+ .collect (
54
+ HashMap ::new ,
55
+ (map , tuple ) -> map
56
+ .computeIfAbsent (tuple .get (QProblem .problem .id ), k -> new HashSet <>())
57
+ .add (tuple .get (QConceptTag .conceptTag .name )),
58
+ HashMap ::putAll
59
+ );
60
+
61
+ // 문제 요약 정보 생성
62
+ List <ProblemSummaryResponse > problemSummaries = problemData .stream ()
63
+ .map (tuple -> ProblemSummaryResponse .builder ()
64
+ .problemId (tuple .get (QProblem .problem .id ))
65
+ .problemCustomId (tuple .get (QProblem .problem .problemCustomId ).toString ())
66
+ .problemTitle (tuple .get (QProblem .problem .title ).toString ())
67
+ .memo (tuple .get (QProblem .problem .memo ))
68
+ .mainProblemImageUrl (tuple .get (QProblem .problem .mainProblemImageUrl ))
69
+ .tagNames (conceptTagMap .getOrDefault (tuple .get (QProblem .problem .id ), new HashSet <>()))
70
+ .build ()
71
+ )
72
+ .toList ();
73
+
74
+ return ProblemSetGetResponse .of (problemSet , publishedDates , problemSummaries );
75
+ }
76
+ }
0 commit comments