From 2d0fc2eadbc9871dc1b91d5db08bb674105f5c54 Mon Sep 17 00:00:00 2001 From: GaBaljaintheroom Date: Wed, 3 Jul 2024 15:57:12 +0900 Subject: [PATCH] =?UTF-8?q?refactor=20:=20=EC=95=84=ED=8B=B0=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=A1=B0=ED=9A=8C=20=EC=BF=BC=EB=A6=AC=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../artist/ArtistQuerydslRepositoryImpl.java | 67 ++++++++++--------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/app/domain/show-domain/src/main/java/org/example/repository/artist/ArtistQuerydslRepositoryImpl.java b/app/domain/show-domain/src/main/java/org/example/repository/artist/ArtistQuerydslRepositoryImpl.java index d54db47b..25bb1e88 100644 --- a/app/domain/show-domain/src/main/java/org/example/repository/artist/ArtistQuerydslRepositoryImpl.java +++ b/app/domain/show-domain/src/main/java/org/example/repository/artist/ArtistQuerydslRepositoryImpl.java @@ -7,6 +7,8 @@ import static org.example.entity.genre.QGenre.genre; import com.querydsl.core.types.Projections; +import com.querydsl.core.types.dsl.BooleanExpression; +import com.querydsl.jpa.impl.JPAQuery; import com.querydsl.jpa.impl.JPAQueryFactory; import java.util.List; import java.util.Optional; @@ -23,14 +25,7 @@ public class ArtistQuerydslRepositoryImpl implements ArtistQuerydslRepository { @Override public List findAllWithGenreNames() { - return jpaQueryFactory - .from(artist) - .innerJoin(artistGenre) - .on(artist.id.eq(artistGenre.artistId) - .and(artistGenre.isDeleted.isFalse())) - .innerJoin(genre).on(artistGenre.genreId.eq(genre.id) - .and(genre.isDeleted.isFalse())) - .where(artist.isDeleted.isFalse()) + return createArtistJoinArtistGenreAndGenreQuery() .transform( groupBy(artist.id).list( Projections.constructor( @@ -49,31 +44,43 @@ public List findAllWithGenreNames() { @Override public Optional findArtistWithGenreNamesById(UUID id) { - return Optional.ofNullable(jpaQueryFactory - .from(artist) - .innerJoin(artistGenre) - .on(artist.id.eq(artistGenre.artistId) - .and(artistGenre.isDeleted.isFalse())) - .innerJoin(genre).on(artistGenre.genreId.eq(genre.id) - .and(genre.isDeleted.isFalse())) - .where(artist.id.eq(id)) - .where(artist.isDeleted.isFalse()) - .transform( - groupBy(artist.id).as( - Projections.constructor( - ArtistDetailResponse.class, - artist.id, - artist.koreanName, - artist.englishName, - artist.country, - artist.artistGender, - artist.artistType, - list(genre.name) + return Optional.ofNullable( + createArtistJoinArtistGenreAndGenreQuery() + .where(artist.id.eq(id)) + .where(artist.isDeleted.isFalse()) + .transform( + groupBy(artist.id).as( + Projections.constructor( + ArtistDetailResponse.class, + artist.id, + artist.koreanName, + artist.englishName, + artist.country, + artist.artistGender, + artist.artistType, + list(genre.name) + ) ) ) - ) - .get(id) + .get(id) ); } + private JPAQuery createArtistJoinArtistGenreAndGenreQuery() { + return jpaQueryFactory + .selectFrom(artist) + .join(artistGenre).on(isArtistGenreEqualArtistIdAndIsDeletedFalse()) + .join(genre).on(isGenreEqualArtistIdAndIsDeletedFalse()) + .where(artist.isDeleted.isFalse()); + } + + private BooleanExpression isArtistGenreEqualArtistIdAndIsDeletedFalse() { + return artistGenre.artistId.eq(artist.id).and(artistGenre.isDeleted.isFalse()); + } + + private BooleanExpression isGenreEqualArtistIdAndIsDeletedFalse() { + return artistGenre.genreId.eq(genre.id).and(genre.isDeleted.isFalse()); + } + + }