BoardServiceImpl
페이지 정보
본문
package web.service;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import web.domain.Board;
import web.dto.BoardDTO;
import web.dto.BoardListAllDTO;
import web.dto.BoardListReplyCountDTO;
import web.dto.PageRequestDTO;
import web.dto.PageResponseDTO;
import web.repository.BoardRepository;
@Service
@Log4j2
@RequiredArgsConstructor
@Transactional
public class BoardServiceImpl implements BoardService {
private final ModelMapper modelMapper;
private final BoardRepository boardRepository;
@Override
public Long register(BoardDTO boardDTO) {
//Board board = modelMapper.map(boardDTO, Board.class);
Board board = dtoToEntity(boardDTO);
Long bno = boardRepository.save(board).getBno();
return bno;
}
@Override
public BoardDTO readOne(Long bno) {
//Optional<Board> result = boardRepository.findById(bno);
//board_image까지 조인 처리 적용
Optional<Board> result = boardRepository.findByIdWithImages(bno);
Board board = result.orElseThrow();
//BoardDTO boardDTO = modelMapper.map(board, BoardDTO.class);
BoardDTO boardDTO = entityToDTO(board);
return boardDTO;
}
@Override
public void modify(BoardDTO boardDTO) {
Optional<Board> result = boardRepository.findById(boardDTO.getBno());
Board board = result.orElseThrow();
board.change(boardDTO.getTitle(), boardDTO.getContent()); //제목,내용 수정
boardRepository.save(board);
}
@Override
public void remove(Long bno) {
boardRepository.deleteById(bno);
}
@Override
public PageResponseDTO<BoardDTO> list(PageRequestDTO pageRequestDTO) {
String[] types = pageRequestDTO.getTypes();
String keyword = pageRequestDTO.getKeyword();
Pageable pageable = pageRequestDTO.getPageable("bno");
Page<Board> result = boardRepository.searchAll(types, keyword, pageable);
List<BoardDTO> dtoList = result.getContent().stream()
.map(board -> modelMapper.map(board, BoardDTO.class))
.collect(Collectors.toList());
return PageResponseDTO.<BoardDTO>withAll()
.pageRequestDTO(pageRequestDTO)
.dtoList(dtoList)
.total((int)result.getTotalElements())
.build();
}
@Override
public PageResponseDTO<BoardListReplyCountDTO> listWithReplyCount(PageRequestDTO pageRequestDTO) {
String[] types = pageRequestDTO.getTypes();
String keyword = pageRequestDTO.getKeyword();
Pageable pageable = pageRequestDTO.getPageable("bno");
Page<BoardListReplyCountDTO> result = boardRepository.searchWithReplyCount(types, keyword, pageable);
return PageResponseDTO.<BoardListReplyCountDTO>withAll()
.pageRequestDTO(pageRequestDTO)
.dtoList(result.getContent())
.total((int)result.getTotalElements())
.build();
}
@Override
public PageResponseDTO<BoardListAllDTO> listWithAll(PageRequestDTO pageRequestDTO) {
// Querydsl의 처리 후 수정
return null;
}
}
- 이전글BoardService 24.07.02
- 다음글BoardServiceTests 24.07.02
댓글목록
등록된 댓글이 없습니다.