Skip to content

Commit

Permalink
Merge pull request #196 from lemonssoju/feature/87-editPuzzle
Browse files Browse the repository at this point in the history
[feature/87-editPuzzle] 퍼즐 수정 API 구현
  • Loading branch information
JoongHyun-Kim authored Jun 11, 2024
2 parents 8a6e3e4 + 00f817f commit 02ae3ac
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,24 @@ private PuzzleLocation convertAddressToCoordinates(String address) throws JsonPr
}

// [작성자] 퍼즐 수정
public BaseResponse<String> editPuzzle(Long groupIdx, Long puzzleIdx, MultipartFile newImage, EditPuzzleRequest editPuzzleRequest) {
public BaseResponse<String> 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);
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/lesso/neverland/puzzle/domain/Puzzle.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ public BaseResponse<CreatePuzzleResponse> createPuzzle(@PathVariable Long groupI
// [작성자] 퍼즐 수정
@GetMapping("/{puzzleIdx}/edit")
public BaseResponse<String> 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);
}
}

// [작성자] 퍼즐 삭제
Expand Down

0 comments on commit 02ae3ac

Please sign in to comment.