BoardSearchImpl ③ - ( BoardImage 처리 ) > SPRING_DATA_JPA

본문 바로가기
사이트 내 전체검색

SPRING_DATA_JPA

BoardSearchImpl ③ - ( BoardImage 처리 )

페이지 정보

profile_image
작성자 관리자
댓글 0건 조회 220회 작성일 24-07-01 11:18

본문

package web.repository;


import java.util.List;

import java.util.stream.Collectors;


import org.springframework.data.domain.Page;

import org.springframework.data.domain.PageImpl;

import org.springframework.data.domain.Pageable;

import org.springframework.data.jpa.repository.support.QuerydslRepositorySupport;


import com.querydsl.core.BooleanBuilder;

import com.querydsl.core.Tuple;

import com.querydsl.core.types.Projections;

import com.querydsl.jpa.JPQLQuery;


import web.domain.Board;

import web.domain.QBoard;

import web.domain.QReply;

import web.dto.BoardImageDTO;

import web.dto.BoardListAllDTO;

import web.dto.BoardListReplyCountDTO;


public class BoardSearchImpl extends QuerydslRepositorySupport implements BoardSearch{


public BoardSearchImpl() {

super(Board.class);

}


@Override

public Page<Board> searchAll(String[] types, String keyword, Pageable pageable) {

QBoard board = QBoard.board; //Q도메인 객체 선언 및 초기화

JPQLQuery<Board> query = from(board); //select ... from board (쿼리 부분)


//검색 타입과 검색어가 있다면

if( (types != null && types.length > 0) && keyword != null ) {

BooleanBuilder booleanBuilder = new BooleanBuilder();


for(String type : types) {

switch (type){

case "t":

booleanBuilder.or(board.title.contains(keyword));

break;

case "c":

booleanBuilder.or(board.content.contains(keyword));

break;

case "w":

booleanBuilder.or(board.writer.contains(keyword));

break;

} //switch

} //for


query.where(booleanBuilder);

} //if


query.where(board.bno.gt(0L));


//paging

this.getQuerydsl().applyPagination(pageable, query);


List<Board> list = query.fetch();

long count = query.fetchCount();


return new PageImpl<>(list, pageable, count);

}


@Override

public Page<Board> search1(Pageable pageable) {

QBoard board = QBoard.board; //Q도메인 객체 선언 및 초기화


JPQLQuery<Board> query = from(board); //select ... from board (쿼리 부분)

query.where(board.title.contains("1")); //where title like '%1%' (쿼리 부분)


//paging

this.getQuerydsl().applyPagination(pageable, query);


List<Board> list = query.fetch();

long count = query.fetchCount();


return null;

}


@Override

public Page<BoardListReplyCountDTO> searchWithReplyCount(String[] types, String keyword, Pageable pageable) {

QBoard board = QBoard.board;

QReply reply = QReply.reply;


JPQLQuery<Board> query = from(board);

query.leftJoin(reply).on(reply.board.eq(board));


query.groupBy(board);


if( (types != null && types.length > 0) && keyword != null ){

BooleanBuilder booleanBuilder = new BooleanBuilder();


for(String type: types){

switch (type){

case "t":

booleanBuilder.or(board.title.contains(keyword));

break;

case "c":

booleanBuilder.or(board.content.contains(keyword));

break;

case "w":

booleanBuilder.or(board.writer.contains(keyword));

break;

}

}//end for


query.where(booleanBuilder);

}


//bno > 0

query.where(board.bno.gt(0L));


JPQLQuery<BoardListReplyCountDTO> dtoQuery = 

query.select(Projections.bean(

BoardListReplyCountDTO.class,

board.bno,

board.title,

board.writer,

board.regDate,

reply.count().as("replyCount")

));


this.getQuerydsl().applyPagination(pageable,dtoQuery);


List<BoardListReplyCountDTO> dtoList = dtoQuery.fetch();


long count = dtoQuery.fetchCount();


return new PageImpl<>(dtoList, pageable, count);

}


@Override

public Page<BoardListAllDTO> searchWithAll(String[] types, String keyword, Pageable pageable) {


QBoard board = QBoard.board;

QReply reply = QReply.reply;


JPQLQuery<Board> boardJPQLQuery = from(board);

boardJPQLQuery.leftJoin(reply).on(reply.board.eq(board)); //left join


boardJPQLQuery.groupBy(board);


getQuerydsl().applyPagination(pageable, boardJPQLQuery); //paging


JPQLQuery<Tuple> tupleJPQLQuery = boardJPQLQuery.select(board, reply.countDistinct());


List<Tuple> tupleList = tupleJPQLQuery.fetch();


List<BoardListAllDTO> dtoList = tupleList.stream().map(tuple -> {


Board board1 = (Board) tuple.get(board);

Long replyCount = tuple.get(1, Long.class);


BoardListAllDTO dto = BoardListAllDTO.builder()

.bno(board1.getBno())

.title(board1.getTitle())

.writer(board1.getWriter())

.content(board1.getContent())

.regDate(board1.getRegDate())

.replyCount(replyCount)

.build();


//BoardImage를 BoardImageDTO 처리할 부분

List<BoardImageDTO> imageDTOS = board1.getImageSet().stream().sorted()

.map(boardImage -> BoardImageDTO.builder()

.uuid(boardImage.getUuid())

.fileName(boardImage.getFileName())

.ord(boardImage.getOrd())

.build()

).collect(Collectors.toList());


dto.setBoardImages(imageDTOS); //처리된 BoardImageDTO들을 추가


return dto;


}).collect(Collectors.toList());


Long totalCount = boardJPQLQuery.fetchCount();


return new PageImpl<>(dtoList, pageable, totalCount);

}


}

댓글목록

등록된 댓글이 없습니다.

회원로그인

회원가입

사이트 정보

공지사항
자유게시판
질문답변
1:1문의

 

별명 : 터푸가위
주소 : 부산시 동래구 명장로20번길 90
대표 : 박규태
메일 : dancepkt@******.com

접속자집계

오늘
100
어제
199
최대
10,760
전체
275,129
Copyright © dancePKT . All rights reserved.