TIL day 52

최병은·2024년 3월 14일
  1. 코딩테스트 (크레인 인형 뽑기)
import java.util.Stack;

class Solution {
    public int solution(int[][] board, int[] moves) {
        int answer = 0;
        Stack s = new Stack<>();
        s.push(0);

        for (int m : moves) {
            for (int i = 0; i < board.length; i++) {
                if(board[i][m-1] != 0) {
                    if(s.peek().equals(board[i][m-1])) {
                        s.pop();
                        answer+=2;
                    } else{
                        s.push(board[i][m-1]);
                    }
                    board[i][m-1] = 0;
                    break;
                }
            }
        }

        return answer;
    }
}

  1. QueryDSL 과제 중
@RequiredArgsConstructor
public class ProductRepositoryQueryImpl implements ProductRepositoryQuery{

    private final JPAQueryFactory jpaQueryFactory;

    @Override
    public Page<Product> search(ProductSearchCond cond, Pageable pageable) {
        var query = query(product,cond)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize());

        var products = query.fetch();
        long totalSize = countQuery(cond).fetch().get(0);

        return PageableExecutionUtils.getPage(products,pageable,() -> totalSize);
    }

    private <T> JPAQuery<T> query(Expression<T> expr, ProductSearchCond cond) {
        return jpaQueryFactory.select(expr)
                .from(product)
                .leftJoin(product.user).fetchJoin()
                .leftJoin(product.comments).fetchJoin()
                .where(
                       productCategoryEq(cond.getCategory())
                );
    }

    private JPAQuery<Long> countQuery(ProductSearchCond cond) {
        return jpaQueryFactory.select(Wildcard.count)
                .from(product)
                .where(
                        productCategoryEq(cond.getCategory())
                );
    }

    private BooleanExpression productCategoryEq(CategoryEnum categoryEnum) {
        return Objects.nonNull(categoryEnum) ? product.category.eq(CategoryEnum.Electronics) : null;
    }
}

카테고리 별로 물건의 정보를 가져오고 있는데 물건에 달린 댓글은 못 가져오고 있다. 댓글이 달린 물건들만 가져오고는 있는데 그 물건 데이터 안에 댓글 size는 0이라고 표시되어 있다. 아직 좀더 알아봐야할 것 같다.

profile
안녕하세요

0개의 댓글