Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

메모장

게시판 파일 수정 본문

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);
      }
    }
}

'Java' 카테고리의 다른 글

Gmail SMTP 메일 발송  (0) 2023.01.18
Swagger  (0) 2023.01.13
@AuthenticationPrincipal 이용한 게시판 접근 제한  (0) 2023.01.10
CustomUserDetails, CustomUserDetailsService  (0) 2023.01.10
Optional  (0) 2023.01.09