Skip to content

Commit

Permalink
refactor : 아티스트 조회 쿼리 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
GaBaljaintheroom committed Jul 3, 2024
1 parent 54183a6 commit 2d0fc2e
Showing 1 changed file with 37 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,14 +25,7 @@ public class ArtistQuerydslRepositoryImpl implements ArtistQuerydslRepository {

@Override
public List<ArtistDetailResponse> 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(
Expand All @@ -49,31 +44,43 @@ public List<ArtistDetailResponse> findAllWithGenreNames() {

@Override
public Optional<ArtistDetailResponse> 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());
}


}

0 comments on commit 2d0fc2e

Please sign in to comment.