๐Ÿ”ฅ TIL - Day 62

Kim Dae Hyunยท2021๋…„ 11์›” 22์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
73/93

3์ฐจ ํ”„๋กœ์ ํŠธ 1์ผ์ฐจ
ํ”„๋กœ์ ํŠธ ์š”๊ตฌ์‚ฌํ•ญ๊ณผ ๋„๋ฉ”์ธ ์„ค๊ณ„๋ฅผ ๋งˆ์น˜๊ณ  ์˜ค๋Š˜๋ถ€ํ„ฐ API ๊ตฌํ˜„์„ ์‹œ์ž‘ํ–ˆ๋‹ค.
์˜ค๋Š˜ ๋‚ด๊ฐ€ ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ•œ ๊ธฐ๋Šฅ์€ ์ฝ”๋“œ๋ฆฌ๋ทฐ ๋ชฉ๋ก์„ ๋ณด์—ฌ์ฃผ๋Š” ๋ถ€๋ถ„์ด๋‹ค.

์ด๋ฒˆ์— ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•˜๋ฉด์„œ ๋™์ ์ฟผ๋ฆฌ๋Š” Querydsl์„ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ํŽ˜์ด์ง•, ๊ฒ€์ƒ‰ ๋“ฑ์ด ํ•„์š”ํ•œ ๋ชฉ๋ก์กฐํšŒ API๋Š” Querydsl Jpa๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.

@Slf4j
@RequiredArgsConstructor
public class ReviewRequestDaoImpl implements ReviewRequestDao {

    private final JPAQueryFactory query;

    @Override
    public Page<ReviewRequestResponseDto> findSearchByTitleOrCommentAdvanced(String keyword, Pageable pageable, boolean isAsc) {

        List<ReviewRequestResponseDto> results = query
                .select(new QReviewRequestResponseDto(
                        user.username,
                        reviewRequest.title,
                        reviewRequest.comment,
                        reviewRequest.status.stringValue(),
                        reviewRequest.createdAt)
                )
                .from(reviewRequest)
                .join(reviewRequest.requestUser, user).fetchJoin()
                .where(containingTitleOrComment(keyword))
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .orderBy(isAsc ? reviewRequest.createdAt.desc() : reviewRequest.createdAt.asc())
                .fetch();

        JPAQuery<ReviewRequestResponseDto> jpaQuery = query
                .select(new QReviewRequestResponseDto(
                        user.username,
                        reviewRequest.title,
                        reviewRequest.comment,
                        reviewRequest.status.stringValue(),
                        reviewRequest.createdAt)
                )
                .from(reviewRequest)
                .join(reviewRequest.requestUser, user)
                .where(containingTitleOrComment(keyword));

        return PageableExecutionUtils.getPage(results, pageable, jpaQuery::fetchCount);
    }
}


โœ… PageableExecutionUtils
Querydsl๋ฅผ ์ด์šฉํ•ด์„œ ํŽ˜์ด์ง•์„ ๊ตฌํ˜„ํ•  ๋•Œ ๋งŽ์€ ์ž๋ฃŒ์—์„œ PageableExecutionUtils๋ฅผ ํ†ตํ•œ ์„ฑ๋Šฅ๊ฐœ์„ ์„ ๊ถŒ์žฅํ•˜๊ณ  ์žˆ๋‹ค. ํŽ˜์ด์ง• ์‹œ ์ „์ฒด ์š”์†Œ์ˆ˜๋ฅผ ์•Œ๊ธฐ ์œ„ํ•ด ๋งค ์š”์ฒญํ•˜๋‹ค count์š”์ฒญ์„ ๋‚ ๋ฆฌ๋Š”๋ฐ ์ด ๋ถ€๋ถ„์„ PageableExecutionUtils๋ฅผ ์ด์šฉํ•˜๋ฉด ์กฐ๊ธˆ ๊ฐœ์„ ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

์œ„์™€ ๊ฐ™์ด ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ถ€๋ถ„๊ณผ ์นด์šดํŒ…์„ ์œ„ํ•œ ๋ถ€๋ถ„์„ ๋ถ„๋ฆฌํ•˜๋ฉด ์ฒซ ํŽ˜์ด์ง€์™€ ๋ ํŽ˜์ด์ง€์— ๋Œ€ํ•ด ์นด์šดํŠธ ์ฟผ๋ฆฌ๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.


โœ… ๋™์  ์ •๋ ฌ์กฐ๊ฑด? dynamic sort?
ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ์ •๋ ฌ์กฐ๊ฑด์„ ๋™์ ์œผ๋กœ ์ ์šฉ์‹œํ‚ค๊ณ  ์‹ถ์—ˆ๋‹ค.
( ๋‚ ์งœ๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ -> ์˜ค๋ฅธ์ฐจ์ˆœ -> ๋‚ด๋ฆผ์ฐจ์ˆœ ... ์ด๋Ÿฐ์‹์œผ๋กœ )
Service ๊ณ„์ธต์—์„œ Pageable์— ์ •๋ ฌ์กฐ๊ฑด์„ ํฌํ•จํ•ด์„œ ๋„˜๊ฒจ์ฃผ๊ณ  Querydsl์—์„œ ํ•ด๋‹น Pageable์„ ๋„ฃ์–ด์ฃผ๋ฉด ์•Œ์•„์„œ ๋  ์ค„ ์•Œ์•˜๋Š”๋ฐ ์•ˆ ๋œ๋‹ค. ใ… ใ… 

๊ฒ€์ƒ‰์„ ํ•ด๋ณด๋‹ˆ ์กฐ๊ธˆ ๋” ๋ณต์žกํ•œ ๊ฒฝ์šฐ OrderSpecifier๋ฅผ ์ง์ ‘ ์ปค์Šคํ…€ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ ๋‚˜๋Š” ์กฐ๊ธˆ ๋” ์‹ฌํ”Œํ•˜๊ฒŒ ํ•ด๋ดค๋‹ค.

 .orderBy(isAsc ? reviewRequest.createdAt.desc() : reviewRequest.createdAt.asc())

์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ–ˆ๋‹ค.
ํ™•์žฅ์ด ๋งค์šฐ ๋–จ์–ด์ง€์ง€๋งŒ.. ใ…Ž ์–ด์จŒ๋“  ๋™์ž‘์€ ํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ Java์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์„œ ๋‹ค์–‘ํ•œ ์†”๋ฃจ์…˜์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด Querydsl์˜ ๋˜๋‹ค๋ฅธ ์žฅ์ ์ด๋ผ ์ƒ๊ฐ๋œ๋‹ค.

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

0๊ฐœ์˜ ๋Œ“๊ธ€