From 3075d39606c76ac5cfc40d679df1fb8019669c58 Mon Sep 17 00:00:00 2001 From: thkim Date: Mon, 18 Mar 2024 15:28:38 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EA=B4=80=EB=A6=AC=EC=9E=90=20-=20?= =?UTF-8?q?=ED=8C=9D=EC=97=85=20=EA=B4=80=EB=A6=AC=20>=20=ED=8A=B9?= =?UTF-8?q?=EC=A0=95=20=EA=B8=80=20=EC=88=98=EC=A0=95=20=EC=8B=9C,=20?= =?UTF-8?q?=ED=8A=B9=EC=A0=95=20file=20=EC=82=AD=EC=A0=9C=ED=95=98?= =?UTF-8?q?=EB=8A=94=20API=20=EC=B6=94=EA=B0=80=20=EA=B1=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../popUp/controller/PopUpApiController.java | 41 +++++++- .../popUp/service/PopUpApiService.java | 4 +- .../service/impl/PopUpApiServiceImpl.java | 76 ++++++++++++--- .../kcscbackend/file/service/FileService.java | 96 ++++++++++++------- 4 files changed, 166 insertions(+), 51 deletions(-) diff --git a/kcsc-back-end/src/main/java/com/dbnt/kcscbackend/admin/contents/popUp/controller/PopUpApiController.java b/kcsc-back-end/src/main/java/com/dbnt/kcscbackend/admin/contents/popUp/controller/PopUpApiController.java index acb0b56..95a1be7 100644 --- a/kcsc-back-end/src/main/java/com/dbnt/kcscbackend/admin/contents/popUp/controller/PopUpApiController.java +++ b/kcsc-back-end/src/main/java/com/dbnt/kcscbackend/admin/contents/popUp/controller/PopUpApiController.java @@ -129,6 +129,7 @@ public class PopUpApiController { HttpServletRequest request, @AuthenticationPrincipal LoginVO loginVO, UpdatePopupVO updatePopupVO, + @RequestParam(required = false) long[] survivingFiles, @RequestParam(required = false) MultipartFile[] files, @PathVariable("popupId") Long popupId ) throws Exception { @@ -136,7 +137,7 @@ public class PopUpApiController { ResultVO resultVO = new ResultVO(); try { - resultVO = popUpApiService.contentsApiPopUpManageUpdate(resultVO, request, loginVO, updatePopupVO, files, popupId); + resultVO = popUpApiService.contentsApiPopUpManageUpdate(resultVO, request, loginVO, updatePopupVO, files, survivingFiles, popupId); } catch (Exception e) { resultVO.setResultCode(ResponseCode.FAILED.getCode()); resultVO.setResultMessage(e.getMessage()); @@ -233,7 +234,6 @@ public class PopUpApiController { ); return resultVO; - } @Operation( @@ -273,6 +273,43 @@ public class PopUpApiController { return resultVO; } + @Operation( + summary = "팝업 관리에서 특정 첨부 파일을 삭제하는 API", + description = "관리자 단에서 '컨텐츠 관리' > '팝업 관리' 페이지에서 특정 팝업 수정 후 첨부된 file 삭제하는 API.", + tags = {"PopUpApiController"} + ) + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "등록 성공"), + @ApiResponse(responseCode = "403", description = "인가된 사용자가 아님"), + }) + @DeleteMapping(value = "/contents/api/popup-manage/file/{fileSeq}") + public ResultVO deleteFileContentsApiPopUpManage + ( + @AuthenticationPrincipal LoginVO user, + HttpServletRequest request, + @PathVariable("fileSeq") Long fileSeq + ) throws Exception { + + ResultVO resultVO = new ResultVO(); + try { + resultVO = popUpApiService.deleteFileContentsApiPopUpManage(resultVO, request, user, fileSeq); + } catch (Exception e) { + resultVO.setResultCode(ResponseCode.FAILED.getCode()); + resultVO.setResultMessage(e.getMessage()); + } + + System.out.println( + "\n--------------------------------------------------------------\n" + + request.getRequestURI() + " OUT:" + + "\n--------------------------------------------------------------\n" + + "resultVO.toString():" + "\n" + + resultVO.toString() + "\n" + + "\n--------------------------------------------------------------\n" + ); + + return resultVO; + } + diff --git a/kcsc-back-end/src/main/java/com/dbnt/kcscbackend/admin/contents/popUp/service/PopUpApiService.java b/kcsc-back-end/src/main/java/com/dbnt/kcscbackend/admin/contents/popUp/service/PopUpApiService.java index c621b66..457a3fa 100644 --- a/kcsc-back-end/src/main/java/com/dbnt/kcscbackend/admin/contents/popUp/service/PopUpApiService.java +++ b/kcsc-back-end/src/main/java/com/dbnt/kcscbackend/admin/contents/popUp/service/PopUpApiService.java @@ -18,8 +18,8 @@ public interface PopUpApiService { public ResultVO contentsApiPopUpManageList(ResultVO resultVO, HttpServletRequest request, LoginVO user, Pageable pageable) throws Exception; public ResultVO contentsApiPopUpManageCreate(ResultVO resultVO, HttpServletRequest request, LoginVO user, final MultipartHttpServletRequest multiRequest, CreatePopupVO createPopupVO, MultipartFile[] files) throws Exception; public ResultVO contentsApiPopUpManageRead(ResultVO resultVO, HttpServletRequest request, LoginVO user, Long popupSeq) throws Exception; - public ResultVO contentsApiPopUpManageUpdate(ResultVO resultVO, HttpServletRequest request, LoginVO user, UpdatePopupVO updatePopupVO, MultipartFile[] files, Long popupSeq) throws Exception; + public ResultVO contentsApiPopUpManageUpdate(ResultVO resultVO, HttpServletRequest request, LoginVO user, UpdatePopupVO updatePopupVO, MultipartFile[] files, long[] survivingFiles, Long popupSeq) throws Exception; public ResultVO contentsApiPopUpManageDelete(ResultVO resultVO, HttpServletRequest request, LoginVO user, Long popupSeq) throws Exception; public ResultVO contentsApiPopUpManageUpdateActivationSwitch(ResultVO resultVO, HttpServletRequest request, LoginVO user, String checked, Long popupSeq) throws Exception; - + public ResultVO deleteFileContentsApiPopUpManage(ResultVO resultVO, HttpServletRequest request, LoginVO user, Long fileSeq) throws Exception; } \ No newline at end of file diff --git a/kcsc-back-end/src/main/java/com/dbnt/kcscbackend/admin/contents/popUp/service/impl/PopUpApiServiceImpl.java b/kcsc-back-end/src/main/java/com/dbnt/kcscbackend/admin/contents/popUp/service/impl/PopUpApiServiceImpl.java index 202b52e..9881ea4 100644 --- a/kcsc-back-end/src/main/java/com/dbnt/kcscbackend/admin/contents/popUp/service/impl/PopUpApiServiceImpl.java +++ b/kcsc-back-end/src/main/java/com/dbnt/kcscbackend/admin/contents/popUp/service/impl/PopUpApiServiceImpl.java @@ -40,8 +40,6 @@ public class PopUpApiServiceImpl extends EgovAbstractServiceImpl implements PopU private final TnPopupMngRepository tnPopupMngRepository; private final TnPopupMngRepositoryWithoutPopupContents tnPopupMngRepositoryWithoutPopupContents; - private final TnAttachFileRepository tnAttachFileRepository; - private final FileService fileService; @@ -173,7 +171,7 @@ public class PopUpApiServiceImpl extends EgovAbstractServiceImpl implements PopU dto.put("schdulEndde", tnPopupMng.getPopupEndDate().plusHours(9).format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))); // 날짜/시간의 종료 일시 - yyyyMMddHHmmss //첨부파일명을 가져온다. - List tnAttachFileList = tnAttachFileRepository.findByFileGrpId(tnPopupMng.getFileGrpId()).orElse(null); + List tnAttachFileList = fileService.findByFileGrpId(tnPopupMng.getFileGrpId()); if( tnAttachFileList != null ) { List> files = new ArrayList>(); @@ -200,7 +198,7 @@ public class PopUpApiServiceImpl extends EgovAbstractServiceImpl implements PopU } @Override - public ResultVO contentsApiPopUpManageUpdate(ResultVO resultVO, HttpServletRequest request, LoginVO user, UpdatePopupVO updatePopupVO, MultipartFile[] files, Long popupSeq) throws Exception { + public ResultVO contentsApiPopUpManageUpdate(ResultVO resultVO, HttpServletRequest request, LoginVO user, UpdatePopupVO updatePopupVO, MultipartFile[] files, long[] survivingFiles, Long popupSeq) throws Exception { System.out.println( "\n--------------------------------------------------------------\n" + request.getRequestURI() + " IN:" + @@ -223,12 +221,46 @@ public class PopUpApiServiceImpl extends EgovAbstractServiceImpl implements PopU throw new Exception("종료일시는 시작일시보다 앞 설 수 없습니다."); } - // 기존 첨부된 file이 있다면 기존 fileGrpId을 활용한다. + // 기존 첨부된 file이 있다면 기존 fileGrpId을 활용한다. TnPopupMng tnPopupMng = tnPopupMngRepository.findByPopupSeq(popupSeq); String fileGrpId = tnPopupMng.getFileGrpId(); + List tnAttachFileList = fileService.findByFileGrpId(tnPopupMng.getFileGrpId()); + + if( survivingFiles == null ) { + + //기존 file을 모두 삭제한다. + if( fileGrpId != null ) { + if( tnAttachFileList != null ) { + for (TnAttachFile item : tnAttachFileList) { + fileService.deleteTnAttachFile(request, user, item.getFileSeq().longValue()); + } + } + fileGrpId = null; + } + } else { + // 살아남은 file을 제외한 나머지 file을 삭제한다. + if( tnAttachFileList != null ) { + boolean isFound = false; + for (TnAttachFile item : tnAttachFileList) { + for( long oldFileSeq : survivingFiles) { + if( oldFileSeq == item.getFileSeq() ) { + isFound = true; + break; + } + } + if( !isFound ) { + fileService.deleteTnAttachFile(request, user, item.getFileSeq().longValue()); + } + isFound = false; + } + } + + } + fileGrpId = fileService.addTnAttachFile(request, user, files, this.getMiddlePath(), fileGrpId); + Map response = tnPopupMngRepository.spUpdateTnPopupMng( popupSeq.intValue(), updatePopupVO.getTitle(), @@ -258,12 +290,12 @@ public class PopUpApiServiceImpl extends EgovAbstractServiceImpl implements PopU @Override public ResultVO contentsApiPopUpManageDelete(ResultVO resultVO, HttpServletRequest request, LoginVO user, Long popupId) throws Exception { System.out.println( - "\n--------------------------------------------------------------\n" + - request.getRequestURI() + " IN:" + - "\n--------------------------------------------------------------\n" + - "popupId:" + "\n" + - popupId + "\n" + - "\n--------------------------------------------------------------\n" + "\n--------------------------------------------------------------\n" + + request.getRequestURI() + " IN:" + + "\n--------------------------------------------------------------\n" + + "popupId:" + "\n" + + popupId + "\n" + + "\n--------------------------------------------------------------\n" ); @@ -338,6 +370,28 @@ public class PopUpApiServiceImpl extends EgovAbstractServiceImpl implements PopU return resultVO; } + @Override + public ResultVO deleteFileContentsApiPopUpManage(ResultVO resultVO, HttpServletRequest request, LoginVO user, Long fileSeq) throws Exception { + System.out.println( + "\n--------------------------------------------------------------\n" + + request.getRequestURI() + " IN:" + + "\n--------------------------------------------------------------\n" + + "fileSeq:" + "\n" + + fileSeq + "\n" + + "\n--------------------------------------------------------------\n" + ); + + Map dto = new HashMap(); + + fileService.deleteTnAttachFile(request, user, fileSeq); + + resultVO.setResult(dto); + resultVO.setResultCode(ResponseCode.SUCCESS.getCode()); + resultVO.setResultMessage(ResponseCode.SUCCESS.getMessage()); + + return resultVO; + } + /** * 파일이 저장될 중간 경로를 생성한다. diff --git a/kcsc-back-end/src/main/java/com/dbnt/kcscbackend/file/service/FileService.java b/kcsc-back-end/src/main/java/com/dbnt/kcscbackend/file/service/FileService.java index 78d6c9d..ce4098c 100644 --- a/kcsc-back-end/src/main/java/com/dbnt/kcscbackend/file/service/FileService.java +++ b/kcsc-back-end/src/main/java/com/dbnt/kcscbackend/file/service/FileService.java @@ -42,18 +42,6 @@ public class FileService { - /** - * TN_ATTACH_FILE table에 insert 후, 파일 그룹 ID를 return 한다. - * TN_ATTACH_FILE 참고. - * @param request - * @param user - * @param files - * @param middlePath 파일이 저장될 중간 경로다. - * D:/kcscUploadFiles/XXXX/abc.jpg XXXX에 해당하는 경로다. - * 참고로 D:/kcscUploadFiles 값은 application-xxx.properties에 있는 Globals.fileStorePath를 통해 얻는다. - * @return 파일 그룹 ID - * @throws Exception - */ public String addTnAttachFile(HttpServletRequest request, LoginVO user, MultipartFile[] files, String middlePath) throws Exception { return this.addTnAttachFile(request, user, files, middlePath, null); } @@ -76,6 +64,10 @@ public class FileService { * @throws Exception */ public String addTnAttachFile(HttpServletRequest request, LoginVO user, MultipartFile[] files, String middlePath, String fileGrpId) throws Exception { + + if( files == null ) { + return fileGrpId; + } String ipAddress = NetworkUtil.getClientIpAddress(request); @@ -85,41 +77,73 @@ public class FileService { } - for (MultipartFile file : files) {if( file != null && !file.isEmpty()) { + for (MultipartFile file : files) { + if( file != null && !file.isEmpty()) { - Map filesMap = new HashMap(); - filesMap.put("file", file); + Map filesMap = new HashMap(); + filesMap.put("file", file); - List fileVoList = fileUtil.parseFileInf(filesMap, "", 0, middlePath, null); + List fileVoList = fileUtil.parseFileInf(filesMap, "", 0, middlePath, null); - int nCount = 1; - // 업로드된 file을 tnAttachFile에 insert한다. - for (Iterator iter = fileVoList.iterator(); iter.hasNext(); nCount++) { + int nCount = 1; + // 업로드된 file을 tnAttachFile에 insert한다. + for (Iterator iter = fileVoList.iterator(); iter.hasNext(); nCount++) { - FileVO item = iter.next(); + FileVO item = iter.next(); - tnAttachFileRepository.spAddTnAttachFile( - fileGrpId, - nCount, - item.getOrignlFileNm(), - item.getStreFileNm() + "." + item.getFileExtsn(), - (item.getFileStreCours() + File.separator + item.getAtchFileId()).replaceAll("\\\\", "/"), - Long.parseLong(item.getFileMg()), - item.getFileExtsn(), - ipAddress, - user.getId(), - null, - null, - null - ); + tnAttachFileRepository.spAddTnAttachFile( + fileGrpId, + nCount, + item.getOrignlFileNm(), + item.getStreFileNm() + "." + item.getFileExtsn(), + (item.getFileStreCours() + File.separator + item.getAtchFileId()).replaceAll("\\\\", "/"), + Long.parseLong(item.getFileMg()), + item.getFileExtsn(), + ipAddress, + user.getId(), + null, + null, + null + ); + } } } - } - return fileGrpId; } + + /** + * 특정 파일 하나를 삭제한다. + * @param request + * @param user + * @param fileSeq + * @return 삭제 성공 시, true. 실패 시, 예외 + * @throws Exception 예외 + */ + public boolean deleteTnAttachFile(HttpServletRequest request, LoginVO user, Long fileSeq) throws Exception { + + //파일을 삭제한다. + + TnAttachFile tnAttachFile = tnAttachFileRepository.findById(fileSeq.intValue()).orElse(null); + + if( tnAttachFile == null ) { + throw new Exception("대상이 존재하지 않습니다."); + } + + String fileFullPath = tnAttachFile.getFilePath() + tnAttachFile.getFileNewName(); + + File file = new File(fileFullPath); + file.delete(); + + tnAttachFileRepository.deleteById(fileSeq.intValue()); + return true; + } + + public List findByFileGrpId(String fileGrpId) throws Exception { + return tnAttachFileRepository.findByFileGrpId(fileGrpId).orElse(null); + } + }