Skip to content

Commit

Permalink
fix: block duplicate error in image creation
Browse files Browse the repository at this point in the history
스크랩당 이미지 생성 하나만 하게 수정
  • Loading branch information
Relaxed-Mind committed Dec 9, 2023
1 parent a90d36f commit 424f927
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import capstone.bookdiary.exception.error.ErrorCode;
import capstone.bookdiary.exception.exceptions.DataNotFoundException;
import capstone.bookdiary.exception.exceptions.EmailDuplicateException;
import capstone.bookdiary.exception.exceptions.ImageAlreadyExistException;
import capstone.bookdiary.exception.exceptions.UserNotFoundException;
import capstone.bookdiary.exception.response.ApiErrorResponse;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -37,4 +38,9 @@ ResponseEntity<ApiErrorResponse> emailDuplicationExceptionHandle(EmailDuplicateE
protected ResponseEntity handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
return new ResponseEntity<>(e.getBindingResult().getFieldErrors().get(0).getDefaultMessage() , HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(ImageAlreadyExistException.class)
ResponseEntity<ApiErrorResponse> imageAlreadyExistException(ImageAlreadyExistException e){
return ApiErrorResponse.toResponseEntity(e.getErrorCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public enum ErrorCode {
EMAIL_DUPLICATE(HttpStatus.CONFLICT, "해당 이메일은 이미 존재합니다."),
DATA_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 데이터가 존재하지 않습니다."),
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "접근 권한이 없습니다."),
API_REQUEST_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "도서관 정보나루 API에 문제가 있습니다");
API_REQUEST_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "외부 API 호출에 문제가 있습니다"),
IMAGE_ALREADY_EXIST(HttpStatus.CONFLICT, "해당 스크랩은 이미 이미지가 생성 됐습니다.");

private final HttpStatus httpStatus;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package capstone.bookdiary.exception.exceptions;

import capstone.bookdiary.exception.error.ErrorCode;

public class ImageAlreadyExistException extends CustomException{
public ImageAlreadyExistException() {
super(ErrorCode.IMAGE_ALREADY_EXIST);
}

@Override
public ErrorCode getErrorCode() {
return super.getErrorCode();
}
}
32 changes: 21 additions & 11 deletions src/main/java/capstone/bookdiary/service/ImageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import capstone.bookdiary.domain.entity.Image;
import capstone.bookdiary.domain.entity.Scrap;
import capstone.bookdiary.exception.exceptions.DataNotFoundException;
import capstone.bookdiary.exception.exceptions.ImageAlreadyExistException;
import capstone.bookdiary.feign.ImageClient;
import capstone.bookdiary.repository.ImageRepository;
import capstone.bookdiary.repository.ScrapRepository;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -22,20 +24,28 @@ public class ImageService {

public Map<String, Object> generateImage(ImageRequestDto imageRequestDto) {
Map<String, String> data = new HashMap<>();
String messageString = imageRequestDto.getContent() + " " + imageRequestDto.getMemo();
data.put("message", messageString);

//AI로 전송 using FeignClient
ImageResponseDto imageResponseDto = imageClient.getImageUrl(data);
String imageUrl = imageResponseDto.getBody().substring(1, imageResponseDto.getBody().length() - 1);

Scrap scrap = scrapRepository.findById(imageRequestDto.getScrapId())
.orElseThrow(DataNotFoundException::new);
Image savedImage = imageRepository.save(new Image(scrap, imageUrl));
Optional<Image> optionalImage = imageRepository.findByScrap(scrap);

if(optionalImage.isPresent()){
throw new ImageAlreadyExistException();
}else{
String messageString = imageRequestDto.getContent() + " " + imageRequestDto.getMemo();
data.put("message", messageString);

//AI로 전송 using FeignClient
ImageResponseDto imageResponseDto = imageClient.getImageUrl(data);
String imageUrl = imageResponseDto.getBody().substring(1, imageResponseDto.getBody().length() - 1);

Image savedImage = imageRepository.save(new Image(scrap, imageUrl));
Map<String, Object> response = new HashMap<>();
response.put("imageId", savedImage.getImageId());
response.put("imageUrl", savedImage.getImageUrl());

//return: DB에 저장된 ImageId
Map<String, Object> response = new HashMap<>();
response.put("imageId", savedImage.getImageId());
return response;
//return: DB에 저장된 ImageId
return response;
}
}
}

0 comments on commit 424f927

Please sign in to comment.