diff --git a/src/main/java/com/lesso/neverland/common/base/BaseResponseStatus.java b/src/main/java/com/lesso/neverland/common/base/BaseResponseStatus.java index de92f83..52f3e18 100644 --- a/src/main/java/com/lesso/neverland/common/base/BaseResponseStatus.java +++ b/src/main/java/com/lesso/neverland/common/base/BaseResponseStatus.java @@ -42,6 +42,8 @@ public enum BaseResponseStatus { ALREADY_DELETED_PUZZLE(false, HttpStatus.CONFLICT, "이미 삭제된 puzzle 입니다."), NO_PUZZLER(false, HttpStatus.FORBIDDEN, "해당 퍼즐의 퍼즐러가 아닙니다."), TOO_LONG_CONTENT(false, HttpStatus.BAD_REQUEST, "퍼즐피스의 길이는 100자 이하여야 합니다."), + BLANK_PUZZLE_CONTENT(false, HttpStatus.BAD_REQUEST, "퍼즐 내용이 비었습니다."), + ALREADY_HAS_ALBUM(false, HttpStatus.CONFLICT, "이미 앨범이 만들어진 퍼즐입니다."), // group diff --git a/src/main/java/com/lesso/neverland/group/application/GroupService.java b/src/main/java/com/lesso/neverland/group/application/GroupService.java index 7503756..f295f3f 100644 --- a/src/main/java/com/lesso/neverland/group/application/GroupService.java +++ b/src/main/java/com/lesso/neverland/group/application/GroupService.java @@ -159,15 +159,12 @@ public BaseResponse editGroup(Long groupIdx, MultipartFile image, EditGr userTeamRepository.save(userTeam); } } - - if (image != null) {//TODO: 이미지 삭제 및 업로드 설정 후 동작 확인하기 - // delete previous image + if (image != null && !image.isEmpty()) { imageService.deleteImage(group.getTeamImage()); - // upload new image - String imagePath = imageService.uploadImage("group", image); - group.modifyImage(imagePath); - } else throw new BaseException(NULL_GROUP_IMAGE); + String newImagePath = imageService.uploadImage("group", image); + group.modifyImage(newImagePath); + } groupRepository.save(group); return new BaseResponse<>(SUCCESS); } diff --git a/src/main/java/com/lesso/neverland/group/presentation/GroupController.java b/src/main/java/com/lesso/neverland/group/presentation/GroupController.java index 73e8b27..3239919 100644 --- a/src/main/java/com/lesso/neverland/group/presentation/GroupController.java +++ b/src/main/java/com/lesso/neverland/group/presentation/GroupController.java @@ -40,7 +40,7 @@ public BaseResponse getGroupEditView(@PathVariable Long g // [관리자] 그룹 수정 @PatchMapping("/{groupIdx}/edit") - public BaseResponse editGroup(@PathVariable Long groupIdx, @RequestPart MultipartFile image, @RequestPart EditGroupRequest editGroupRequest) { + public BaseResponse editGroup(@PathVariable Long groupIdx, @RequestPart(required = false) MultipartFile image, @RequestPart EditGroupRequest editGroupRequest) { try { return groupService.editGroup(groupIdx, image, editGroupRequest); } catch (IOException e) { diff --git a/src/main/java/com/lesso/neverland/puzzle/application/PuzzleService.java b/src/main/java/com/lesso/neverland/puzzle/application/PuzzleService.java index 5c1a110..717d554 100644 --- a/src/main/java/com/lesso/neverland/puzzle/application/PuzzleService.java +++ b/src/main/java/com/lesso/neverland/puzzle/application/PuzzleService.java @@ -211,12 +211,24 @@ private PuzzleLocation convertAddressToCoordinates(String address) throws JsonPr } // [작성자] 퍼즐 수정 - public BaseResponse editPuzzle(Long groupIdx, Long puzzleIdx, MultipartFile newImage, EditPuzzleRequest editPuzzleRequest) { + public BaseResponse editPuzzle(Long groupIdx, Long puzzleIdx, MultipartFile newImage, EditPuzzleRequest editPuzzleRequest) throws IOException { User user = userRepository.findById(userService.getUserIdxWithValidation()).orElseThrow(() -> new BaseException(INVALID_USER_IDX)); Puzzle puzzle = puzzleRepository.findById(puzzleIdx).orElseThrow(() -> new BaseException(INVALID_PUZZLE_IDX)); validateWriter(user, puzzle); + if (albumRepository.existsByPuzzle(puzzle)) throw new BaseException(ALREADY_HAS_ALBUM); + if (editPuzzleRequest.content() != null) { + if (!editPuzzleRequest.content().equals("") && !editPuzzleRequest.content().equals(" ")) + puzzle.editContent(editPuzzleRequest.content()); + else throw new BaseException(BLANK_PUZZLE_CONTENT); + } + if (newImage != null && !newImage.isEmpty()) { + imageService.deleteImage(puzzle.getPuzzleImage()); + String newImagePath = imageService.uploadImage("puzzle", newImage); + puzzle.modifyImage(newImagePath); + } + puzzleRepository.save(puzzle); return new BaseResponse<>(SUCCESS); } diff --git a/src/main/java/com/lesso/neverland/puzzle/domain/Puzzle.java b/src/main/java/com/lesso/neverland/puzzle/domain/Puzzle.java index 8864ad6..43f5bf4 100644 --- a/src/main/java/com/lesso/neverland/puzzle/domain/Puzzle.java +++ b/src/main/java/com/lesso/neverland/puzzle/domain/Puzzle.java @@ -75,4 +75,6 @@ public void delete() { this.setStatus(INACTIVE); } public void addPuzzleImage(String puzzleImage) { this.puzzleImage = puzzleImage; } + public void modifyImage(String puzzleImage) {this.puzzleImage = puzzleImage;} + public void editContent(String content) { this.content = content;} } diff --git a/src/main/java/com/lesso/neverland/puzzle/presentation/PuzzleController.java b/src/main/java/com/lesso/neverland/puzzle/presentation/PuzzleController.java index 5183646..2977a6f 100644 --- a/src/main/java/com/lesso/neverland/puzzle/presentation/PuzzleController.java +++ b/src/main/java/com/lesso/neverland/puzzle/presentation/PuzzleController.java @@ -46,7 +46,11 @@ public BaseResponse createPuzzle(@PathVariable Long groupI // [작성자] 퍼즐 수정 @GetMapping("/{puzzleIdx}/edit") public BaseResponse editPuzzle(@PathVariable("groupIdx") Long groupIdx, @PathVariable("puzzleIdx") Long puzzleIdx, @RequestPart MultipartFile newImage, @RequestPart EditPuzzleRequest editPuzzleRequest) { - return puzzleService.editPuzzle(groupIdx, puzzleIdx, newImage, editPuzzleRequest); + try { + return puzzleService.editPuzzle(groupIdx, puzzleIdx, newImage, editPuzzleRequest); + } catch (IOException e) { + throw new BaseException(IMAGE_UPLOAD_FAIL); + } } // [작성자] 퍼즐 삭제