package com.lgcns.esg.repository.system.Custom; import com.lgcns.esg.model.response.system.PageResult; import com.querydsl.core.BooleanBuilder; import com.querydsl.core.types.Projections; import com.querydsl.core.types.dsl.PathBuilderFactory; import com.querydsl.jpa.impl.JPAQuery; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import com.lgcns.esg.model.request.system.SearchSystemTypeRequest; import com.lgcns.esg.model.response.system.SystemTypeResponse; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.support.Querydsl; import org.springframework.stereotype.Repository; import java.util.List; import static com.lgcns.esg.core.util.QueryBuilder.LIKE; import static com.lgcns.esg.core.util.QueryBuilder.buildSearchPredicate; @Repository @RequiredArgsConstructor public class SystemTypeRepositoryCustom { @PersistenceContext private final EntityManager entityManager; private static final QSystemType qSystemType = QSystemType.systemType; public PageResult<SystemTypeResponse> searchSystemType(SearchSystemTypeRequest request, Pageable pageable) { Querydsl querydsl = new Querydsl(entityManager, (new PathBuilderFactory().create(SystemTypeResponse.class))); JPAQuery<SystemTypeResponse> query = new JPAQuery<>(entityManager); BooleanBuilder conditions = new BooleanBuilder(); query.select(Projections.bean(SystemTypeResponse.class, qSystemType.id, qSystemType.code, qSystemType.name)).from(qSystemType); conditions.and(qSystemType.isDeleted.eq(false)); buildSearchPredicate(conditions, LIKE, qSystemType.code, request.getSystemTypeCode()); buildSearchPredicate(conditions, LIKE, qSystemType.name, request.getSystemTypeName()); query.where(conditions).orderBy(qSystemType.createAt.desc()).distinct(); List<SystemTypeResponse> responses = querydsl.applyPagination(pageable, query).fetch(); int count = (int)query.fetchCount(); return new PageResult<>(count, responses); } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter