Skip to content

Commit

Permalink
[Feat] 횃불이 이미지 생성 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hen715 committed Nov 6, 2024
1 parent b024b36 commit 965b4f9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ public class FireController {
@PostMapping("/predict")
public ResponseEntity<ResponseDto<FireResponseDto>> 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<ResponseDto<Page<Fire>>> 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 이미지 정보들 가져오기 성공"));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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<Fire,Long> {
Page<Fire> findByMemberOrderByIdDesc(Member member, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String,Object > 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);
Expand All @@ -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<Fire> getFireImageList(int page){
Pageable pageable = PageRequest.of(page,10);
return fireRepository.findAll(pageable);
public Page<Fire> getFireImageList(Member member, int page){
Pageable pageable = PageRequest.of(page>0?--page:page,4);
return fireRepository.findByMemberOrderByIdDesc(member,pageable);
}


Expand Down

0 comments on commit 965b4f9

Please sign in to comment.