Java

게시판 파일 수정

doopang 2023. 1. 11. 11:01

Controller

@PutMapping("/{boardSeq}")
@ApiOperation("게시판 수정")
public CommonResult updateBoard(@PathVariable Long boardSeq, BoardDTO boardDTO, 
      @RequestParam("file") List<MultipartFile> files,
      @AuthenticationPrincipal CustomUserDetails customUserDetails) {
    boardService.updateBoard(customUserDetails.getUserSeq(), boardSeq, boardDTO, files);
    return responseService.getSuccessResult();
}

 

Service

public void updateBoard(Long userSeq, Long boardSeq, BoardDTO boardDTO
      , List<MultipartFile> files) {

    // 파일 외 다른 게시판 정보 업데이트
    boardService.updateBoard(boardSeq, boardDTO);

    // 파일 업데이트
    updateFile(boardSeq, boardDTO);

    // 추가 등록한 파일이 있으면 저장
    if (!files.isEmpty()) {
        insertFile(userSeq, boardSeq, files);
    }
}

private void updateFile(long boardSeq, BoardDTO boardDTO) {
    // 기존 저장된 파일이 없으면 전체 삭제 처리
    if (boardDTO.getFileList() == null) {
      deleteFile(fileService.getFileList(boardDTO, "tb_board")
          .stream()
          .map(FileDTO::getFileSeq)
          .collect(Collectors.toList()));
    } else {
      // 기존 저장된 파일 조회
      List<Long> originFileList = filesService.getFileList(boardSeq, "tb_board")
          .stream()
          .map(FileDTO::getFileSeq)
          .collect(Collectors.toList());

      // 삭제 되지 않은 파일 조회
      List<Long> responseFileList = boardDTO.getFileList()
          .stream()
          .map(FileDTO::getFileSeq)
          .collect(Collectors.toList());

      // originFileList 에서 responseFileList 삭제 후 originFileList 삭제 처리
      if (!originFileList.isEmpty()) {
        originFileList.removeall(responseFileList);
        deleteFile(originFileList);
      }
    }
}