Troubleshooting: QueryDSL

Sungju Kim·2024년 10월 17일

Sparta_Coding_Camp_TIL

목록 보기
50/53
post-thumbnail

When I was developing a query that allow user to search the tickets (for context, its like a todo list) based on boardId, deadline, keyword (title or content), and managerName, I encountered a problem in which the computer was unable to locate ticket.kanban.board.workspace.

What's more interesting is that it was able to locate up to ticket.kanban.board but keep finding null when looking for the workspace.

Turns out it was because workspace is connected with members as well. I think because both members and board reference workspace computer had a sort of confusion on accessing workspace.
업로드중..

The problem was eventually resolved by accessing workspaceId via member.

package com.sparta.springtrello.domain.ticket.repository;
imports...

@Repository
@RequiredArgsConstructor
public class TicketQueryDslRepositoryImpl implements TicketQueryDslRepository{
    private final JPAQueryFactory queryFactory;

    @Override
    public Page<TicketResponseDto> searchTickets(
            long workspaceId,
            String ticketKeyword,
            String managerName,
            String deadline,
            String boardId,
            Pageable pageable
    ) {
        List<TicketResponseDto> results = queryFactory
                .select(
                        Projections.constructor(
                                TicketResponseDto.class,
                                ticket.title,
                                ticket.contents,
                                ticket.deadline,
                                ticket.kanban.id
                        )
                )
                .from(ticket)
                .leftJoin(ticket.member, member)
                .leftJoin(ticket.kanban, kanban)
                .leftJoin(kanban.board, board)
                .leftJoin(board.workspace, workspace)

                .where(
                        sameWorkspace(workspaceId),
                        titleOrContentContains(ticketKeyword),
                        dueAt(deadline),
                        managerName(managerName)
                )
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .orderBy(ticket.id.desc())
                .fetch();

        Long totalCount = queryFactory
                .select(Wildcard.count)
                .from(ticket)
                .where(
                        sameWorkspace(workspaceId),
                        sameBoard(boardId),
                        titleOrContentContains(ticketKeyword),
                        dueAt(deadline),
                        managerName(managerName)
                ).fetchOne();

        return new PageImpl<>(results, pageable, totalCount);
    }

    private BooleanExpression sameWorkspace(long id) {
        // tickets that are in my current workspace
        return ticket.member.workspace.id.eq(id);
    }
    private BooleanExpression titleOrContentContains(String keyword) {
        if (keyword == null) return null;
        return ticket.title.containsIgnoreCase(keyword)
                .or(ticket.contents.containsIgnoreCase(keyword));
    }

    private BooleanExpression dueAt(String deadline) {
        return deadline != null ? ticket.deadline.eq(deadline) : null;
    }

    private BooleanExpression sameBoard(String id) {

        return id != null ? ticket.kanban.board.id.eq(Long.valueOf(id)) : null;
    }

    private BooleanExpression managerName(String managerName) {
        return managerName != null ? member.user.name.containsIgnoreCase(managerName) : null;
    }
}
profile
Fully ✨committed✨ developer, always eager to learn!

0개의 댓글