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/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); + } } // [작성자] 퍼즐 삭제