Skip to content

Commit

Permalink
[Feat] 횃불이 평점 부여 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hen715 committed Nov 8, 2024
1 parent 39e5af5 commit 616962f
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ 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,fireDto.getPrompt()),"횃불이 ai 그림 요청이 큐에 성공적으로 추가됨."));
}

Expand All @@ -52,6 +51,17 @@ public ResponseEntity<ResponseDto<FirePageResponseDto>> getFireRating(@Authentic
return ResponseEntity.ok(ResponseDto.of(fireService.getFireImageList(member,page),"횃불이 ai 이미지 정보들 가져오기 성공"));
}

@Operation(summary = "횃불이 ai 그림 평점 부여",description = "헤더에 인증토큰을, 바디에 {req_id,rating,comment(필수아님)}을 json 형식으로 보내주세요.")
@ApiResponses({
@ApiResponse(responseCode = "200",description = "횃불이 그림 평점 부여 성공",content = @Content(schema = @Schema(implementation = ResponseDto.class))),
@ApiResponse(responseCode = "400",description = "횃불이 이미지 관련 요청에 문제가 있습니다.",content = @Content(schema = @Schema(implementation = ResponseDto.class)))
})
@PostMapping("/rating")
public ResponseEntity<ResponseDto<Long>> ratingImage(@AuthenticationPrincipal Member member, @Valid@RequestBody FireRatingDto fireRatingDto) throws JsonProcessingException {
fireService.rating(member,fireRatingDto);
return ResponseEntity.ok(ResponseDto.of(1L,"횃불이 그림 평점 부여 성공"));
}




Expand Down
11 changes: 11 additions & 0 deletions src/main/java/kr/inuappcenterportal/inuportal/domain/Fire.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,23 @@ public class Fire extends BaseTimeEntity{
@Column(name = "member_id")
private Long memberId;

@Column(name = "is_rated")
private Boolean isRated;

@Column(name = "good_count")
private Integer goodCount;

@Builder
public Fire(String prompt, String requestId, Long memberId) {
this.prompt = prompt;
this.requestId = requestId;
this.memberId = memberId;
this.isRated = false;
this.goodCount = 0;
}

public void rate(){
this.isRated = true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package kr.inuappcenterportal.inuportal.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Schema(description = "횃불이 ai 그림 평점 등록 요청Dto")
@NoArgsConstructor
@Getter
public class FireRatingDto {

private Long u_id;

@Schema(description = "그림의 요청번호",example = "a1b2c3d4-e5f6-7g8h-9i0j-k1l2m3n4o5p6")
@NotBlank
private String req_id;

@Schema(description = "평점(0~5)")
@Min(0)
@Max(5)
private Integer rating;

@Schema(description = "한 줄평",example = "손이 어색해요")
private String comment;

public void setU_id(Long u_id) {
this.u_id = u_id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public enum MyErrorCode {
EMAIL_NOT_AUTHORIZATION(HttpStatus.FORBIDDEN,"인증되지 않은 이메일입니다."),
NOT_LIKE_MY_POST(HttpStatus.BAD_REQUEST,"자신의 게시글에는 추천을 할 수 없습니다."),
NOT_LIKE_MY_REPLY(HttpStatus.BAD_REQUEST,"자신의 댓글에는 추천을 할 수 없습니다."),
BAD_REQUEST_FIRE_AI(HttpStatus.BAD_REQUEST,"횃불이 이미지가 생성 중 입니다."),
BAD_REQUEST_FIRE_AI(HttpStatus.BAD_REQUEST,"횃불이 이미지 관련 요청에 문제가 있습니다."),
AI_IMAGE_GENERATING(HttpStatus.BAD_REQUEST,"횃불이 이미지가 생성 중 입니다."),
STUDENT_LOGIN_ERROR(HttpStatus.UNAUTHORIZED,"학번 또는 비밀번호가 틀립니다."),
RATED_IMAGE(HttpStatus.BAD_REQUEST,"이미 평가된 이미지입니다."),
WEATHER_REQUEST_ERROR(HttpStatus.BAD_REQUEST,"날씨 요청에 문제가 있습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface FireRepository extends JpaRepository<Fire,Long> {
Page<Fire> findByMemberIdOrderByIdDesc(Long member_id, Pageable pageable);
Optional<Fire> findByRequestId(String requestId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import kr.inuappcenterportal.inuportal.domain.Member;
import kr.inuappcenterportal.inuportal.dto.FireListResponseDto;
import kr.inuappcenterportal.inuportal.dto.FirePageResponseDto;
import kr.inuappcenterportal.inuportal.dto.FireRatingDto;
import kr.inuappcenterportal.inuportal.dto.FireResponseDto;
import kr.inuappcenterportal.inuportal.exception.ex.MyErrorCode;
import kr.inuappcenterportal.inuportal.exception.ex.MyException;
Expand Down Expand Up @@ -36,8 +37,11 @@ public class FireService {
private final WebClient webClient;
private final FireRepository fireRepository;

@Value("${aiUrl}")
private String url;
@Value("${aiGenerateUrl}")
private String generateUrl;

@Value("${aiRatingUrl}")
private String ratingUrl;

@Transactional
public FireResponseDto drawImage(Member member, String prompt) throws JsonProcessingException {
Expand All @@ -48,11 +52,11 @@ public FireResponseDto drawImage(Member member, String prompt) throws JsonProces
String requestBody = objectMapper.writeValueAsString(body);
log.info("횃불이 ai 이미지 생성 요청 파라미터 :{}",prompt);
FireResponseDto fireResponseDto =webClient.post()
.uri(url)
.uri(generateUrl)
.headers(httpHeaders -> httpHeaders.set("Content-Type","application/json"))
.bodyValue(requestBody)
.retrieve()
.onStatus(HttpStatusCode::is4xxClientError, clientResponse -> Mono.just(new MyException(MyErrorCode.BAD_REQUEST_FIRE_AI)))
.onStatus(HttpStatusCode::is4xxClientError, clientResponse -> Mono.just(new MyException(MyErrorCode.AI_IMAGE_GENERATING)))
.bodyToMono(FireResponseDto.class)
.block();
Fire fire = Fire.builder().requestId(fireResponseDto.getRequest_id()).prompt(prompt).memberId(member.getId()).build();
Expand All @@ -71,6 +75,26 @@ public FirePageResponseDto getFireImageList(Member member, int page){
return FirePageResponseDto.of(fires.getTotalPages(), fires.getTotalElements(), fireListResponseDto);
}

@Transactional
public void rating(Member member, FireRatingDto fireRatingDto) throws JsonProcessingException {
Fire fire = fireRepository.findByRequestId(fireRatingDto.getReq_id()).orElseThrow(()->new MyException(MyErrorCode.IMAGE_NOT_FOUND));
if(fire.getIsRated()){
throw new MyException(MyErrorCode.RATED_IMAGE);
}
fire.rate();
fireRatingDto.setU_id(member.getId());
ObjectMapper objectMapper = new ObjectMapper();
String body = objectMapper.writeValueAsString(fireRatingDto);
webClient.post()
.uri(ratingUrl)
.headers(httpHeaders -> httpHeaders.set("Content-Type","application/json"))
.bodyValue(body)
.retrieve()
.onStatus(HttpStatusCode::is4xxClientError, clientResponse -> Mono.just(new MyException(MyErrorCode.BAD_REQUEST_FIRE_AI)))
.bodyToMono(String.class)
.block();
}



}

0 comments on commit 616962f

Please sign in to comment.