ReplyServiceImpl
페이지 정보
본문
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.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import web.domain.Reply;
import web.dto.PageRequestDTO;
import web.dto.PageResponseDTO;
import web.dto.ReplyDTO;
import web.repository.ReplyRepository;
@Service
@RequiredArgsConstructor
@Log4j2
public class ReplyServiceImpl implements ReplyService {
private final ReplyRepository replyRepository;
private final ModelMapper modelMapper;
@Override
public Long register(ReplyDTO replyDTO) {
Reply reply = modelMapper.map(replyDTO, Reply.class);
Long rno = replyRepository.save(reply).getRno();
return rno;
}
@Override
public ReplyDTO read(Long rno) {
Optional<Reply> replyOptional = replyRepository.findById(rno);
Reply reply = replyOptional.orElseThrow();
return modelMapper.map(reply, ReplyDTO.class);
}
@Override
public void modify(ReplyDTO replyDTO) {
Optional<Reply> replyOptional = replyRepository.findById(replyDTO.getRno());
Reply reply = replyOptional.orElseThrow();
reply.changeText(replyDTO.getReplyText()); //댓글의 내용만 수정
replyRepository.save(reply);
}
@Override
public void remove(Long rno) {
replyRepository.deleteById(rno);
}
@Override
public PageResponseDTO<ReplyDTO> getListOfBoard(Long bno, PageRequestDTO pageRequestDTO) {
int i = pageRequestDTO.getPage() <= 0 ? 0:pageRequestDTO.getPage() -1; //현재 페이지
if(pageRequestDTO.getSize() == 0) { pageRequestDTO.setSize(5); } //페이지당 출력 게시물 수
if(pageRequestDTO.getBlock() == 0) { pageRequestDTO.setBlock(5); } //블럭 수
Pageable pageable = PageRequest.of(i, pageRequestDTO.getSize(), Sort.by("rno").descending()); //최신순
Page<Reply> result = replyRepository.listOfBoard(bno, pageable);
List<ReplyDTO> dtoList = result.getContent().stream()
.map(reply -> modelMapper.map(reply, ReplyDTO.class))
.collect(Collectors.toList());
return PageResponseDTO.<ReplyDTO>withAll()
.pageRequestDTO(pageRequestDTO)
.dtoList(dtoList)
.total((int)result.getTotalElements())
.build();
}
}
- 이전글ReplyService 24.06.21
- 다음글● 댓글 등록 처리 24.06.21
댓글목록
등록된 댓글이 없습니다.