From 965b4f93fb651e91f618e3e2ca390d2160a127e6 Mon Sep 17 00:00:00 2001 From: hen715 Date: Wed, 6 Nov 2024 16:41:13 +0900 Subject: [PATCH] =?UTF-8?q?[Feat]=20=ED=9A=83=EB=B6=88=EC=9D=B4=20?= =?UTF-8?q?=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EC=83=9D=EC=84=B1=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../inuportal/controller/FireController.java | 6 ++--- .../inuportal/domain/Fire.java | 3 ++- .../inuportal/repository/FireRepository.java | 4 +++ .../inuportal/service/FireService.java | 26 +++++++------------ 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/main/java/kr/inuappcenterportal/inuportal/controller/FireController.java b/src/main/java/kr/inuappcenterportal/inuportal/controller/FireController.java index f8e4100..f078c38 100644 --- a/src/main/java/kr/inuappcenterportal/inuportal/controller/FireController.java +++ b/src/main/java/kr/inuappcenterportal/inuportal/controller/FireController.java @@ -39,17 +39,17 @@ public class FireController { @PostMapping("/predict") public ResponseEntity> drawFireAiImage(@Valid@RequestBody FireDto fireDto, @AuthenticationPrincipal Member member) throws JsonProcessingException { log.info("횃불이 그림 그리기 호출 파라미터 :{}",fireDto.getPrompt()); - return ResponseEntity.status(HttpStatus.OK).body(ResponseDto.of(fireService.drawImage(member.getId(),fireDto.getPrompt()),"횃불이 ai 그림 요청이 큐에 성공적으로 추가됨.")); + return ResponseEntity.status(HttpStatus.OK).body(ResponseDto.of(fireService.drawImage(member,fireDto.getPrompt()),"횃불이 ai 그림 요청이 큐에 성공적으로 추가됨.")); } - @Operation(summary = "횃불이 ai 이미지 정보들 가져오기",description = "url 파라미터에 페이지 번호를 보내주세요. 보내지 않을 시 첫 페이지가 보내집니다. 한 페이지의 크기는 10입니다.") + @Operation(summary = "횃불이 ai 이미지 정보들 가져오기",description = "url 파라미터에 페이지 번호를 보내주세요. 보내지 않을 시 첫 페이지가 보내집니다. 한 페이지의 크기는 4입니다.") @ApiResponses({ @ApiResponse(responseCode = "200",description = "횃불이 ai 이미지 정보들 가져오기 성공",content = @Content(schema = @Schema(implementation = ResponseDto.class))), }) @GetMapping("") public ResponseEntity>> getFireRating(@AuthenticationPrincipal Member member,@RequestParam(required = false,defaultValue = "0") int page){ - return ResponseEntity.ok(ResponseDto.of(fireService.getFireImageList(page),"횃불이 ai 이미지 정보들 가져오기 성공")); + return ResponseEntity.ok(ResponseDto.of(fireService.getFireImageList(member,page),"횃불이 ai 이미지 정보들 가져오기 성공")); } diff --git a/src/main/java/kr/inuappcenterportal/inuportal/domain/Fire.java b/src/main/java/kr/inuappcenterportal/inuportal/domain/Fire.java index bf41d63..7924e3f 100644 --- a/src/main/java/kr/inuappcenterportal/inuportal/domain/Fire.java +++ b/src/main/java/kr/inuappcenterportal/inuportal/domain/Fire.java @@ -28,9 +28,10 @@ public class Fire extends BaseTimeEntity{ @Builder - public Fire(String prompt, String requestId) { + public Fire(String prompt, String requestId, Member member) { this.prompt = prompt; this.requestId = requestId; + this.member = member; } } diff --git a/src/main/java/kr/inuappcenterportal/inuportal/repository/FireRepository.java b/src/main/java/kr/inuappcenterportal/inuportal/repository/FireRepository.java index fd54e16..283a11a 100644 --- a/src/main/java/kr/inuappcenterportal/inuportal/repository/FireRepository.java +++ b/src/main/java/kr/inuappcenterportal/inuportal/repository/FireRepository.java @@ -1,7 +1,11 @@ package kr.inuappcenterportal.inuportal.repository; import kr.inuappcenterportal.inuportal.domain.Fire; +import kr.inuappcenterportal.inuportal.domain.Member; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; public interface FireRepository extends JpaRepository { + Page findByMemberOrderByIdDesc(Member member, Pageable pageable); } diff --git a/src/main/java/kr/inuappcenterportal/inuportal/service/FireService.java b/src/main/java/kr/inuappcenterportal/inuportal/service/FireService.java index 639761f..4f76bb2 100644 --- a/src/main/java/kr/inuappcenterportal/inuportal/service/FireService.java +++ b/src/main/java/kr/inuappcenterportal/inuportal/service/FireService.java @@ -22,10 +22,8 @@ import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; -import java.util.Map; + @Service @RequiredArgsConstructor @@ -35,20 +33,12 @@ public class FireService { private final FireRepository fireRepository; @Value("${aiUrl}") - private String initAiUrl; - - private static String url; - - @PostConstruct - public void initAiUrl(){ - url = initAiUrl; - log.info("ai 이미지 요청 url init url : {}",url); - } + private String url; @Transactional - public FireResponseDto drawImage(Long id, String prompt) throws JsonProcessingException { + public FireResponseDto drawImage(Member member, String prompt) throws JsonProcessingException { HashMap body = new HashMap<>(); - body.put("u_id",id); + body.put("u_id",member.getId()); body.put("prompt",prompt); ObjectMapper objectMapper = new ObjectMapper(); String requestBody = objectMapper.writeValueAsString(body); @@ -61,15 +51,17 @@ public FireResponseDto drawImage(Long id, String prompt) throws JsonProcessingEx .onStatus(HttpStatusCode::is4xxClientError, clientResponse -> Mono.just(new MyException(MyErrorCode.BAD_REQUEST_FIRE_AI))) .bodyToMono(FireResponseDto.class) .block(); + Fire fire = Fire.builder().requestId(fireResponseDto.getRequest_id()).prompt(prompt).member(member).build(); + fireRepository.save(fire); return fireResponseDto; } @Transactional(readOnly = true) - public Page getFireImageList(int page){ - Pageable pageable = PageRequest.of(page,10); - return fireRepository.findAll(pageable); + public Page getFireImageList(Member member, int page){ + Pageable pageable = PageRequest.of(page>0?--page:page,4); + return fireRepository.findByMemberOrderByIdDesc(member,pageable); }