Skip to content

Commit

Permalink
Merge pull request #186 from Genti2024/feat/pgreq-by-redesign
Browse files Browse the repository at this point in the history
Feat/pgreq by redesign
  • Loading branch information
LeeJae-H authored Oct 27, 2024
2 parents ebf471b + 19a7483 commit 7f682db
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public class PGREQSaveRequestDto {
@Schema(description = "얼굴 사진 리스트")
List<@NotNull CommonPictureKeyUpdateRequestDto> facePictureList;

@NotNull
@Schema(description = "카메라 앵글")
CameraAngle cameraAngle;
@NotNull

@Schema(description = "프레임")
ShotCoverage shotCoverage;

@NotNull
@Schema(description = "사진 비율")
PictureRatio pictureRatio;
Expand All @@ -59,7 +59,7 @@ public PGREQSaveRequestDto(String prompt, CommonPictureKeyUpdateRequestDto poseP
public PGREQSaveCommand toCommand() {
return PGREQSaveCommand.builder()
.prompt(this.prompt)
.posePictureKey(this.posePicture.getKey())
.posePictureKey(this.posePicture != null ? this.posePicture.getKey() : null)
.cameraAngle(this.cameraAngle)
.shotCoverage(this.shotCoverage)
.pictureRatio(this.pictureRatio)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,31 +187,13 @@ public PictureGenerateRequest createPGREQ(Long userId, PGREQSaveCommand pgreqSav
throwIfNewPictureGenerateRequestNotAvailable(foundUser);

String lockKey = "LOCK:" + userId + ":" + "createPGREQ";
Boolean lockGranted = redisTemplate.opsForValue().setIfAbsent(lockKey, "locked", LOCK_TTL_SECONDS, TimeUnit.SECONDS);
if (Boolean.FALSE.equals(lockGranted)) {
if (!acquireLock(lockKey)) {
throw ExpectedException.withLogging(ResponseCode.PictureGenerateRequestAlreadyProcessed);
}
try {
String posePictureKey = "";
PicturePose foundPicturePose = null;
if (!Objects.isNull(pgreqSaveCommand.getPosePictureKey())) {
posePictureKey = pgreqSaveCommand.getPosePictureKey();
String finalPosePictureKey = posePictureKey;
foundPicturePose = pictureService.findByKeyPicturePose(posePictureKey).orElseGet(() -> {
log.info("""
%s 유저가 요청에 포함한 포즈참고사진 key [%s] 기존 사진을 찾을 수 없어 신규 저장""".formatted(foundUser.getEmail(),
finalPosePictureKey));
return pictureService.updatePicture(
CreatePicturePoseCommand.builder().key(finalPosePictureKey).uploader(foundUser).build());
});
}

List<String> facePictureUrl = pgreqSaveCommand.getFacePictureKeyList();
List<PictureUserFace> uploadedFacePictureList = pictureService.updateIfNotExistsPictureUserFace(
facePictureUrl, foundUser);

String promptAdvanced = openAIService.getAdvancedPrompt(
new PromptAdvancementRequestCommand(pgreqSaveCommand.getPrompt()));
PicturePose picturePose = processPicturePose(pgreqSaveCommand, foundUser);
List<PictureUserFace> uploadedFacePictureList = pictureService.updateIfNotExistsPictureUserFace(pgreqSaveCommand.getFacePictureKeyList(), foundUser);
String promptAdvanced = openAIService.getAdvancedPrompt(new PromptAdvancementRequestCommand(pgreqSaveCommand.getPrompt()));
log.info(promptAdvanced);

PictureGenerateRequest createdPGREQ = PictureGenerateRequest.builder()
Expand All @@ -221,7 +203,7 @@ public PictureGenerateRequest createPGREQ(Long userId, PGREQSaveCommand pgreqSav
.shotCoverage(pgreqSaveCommand.getShotCoverage())
.cameraAngle(pgreqSaveCommand.getCameraAngle())
.pictureRatio(pgreqSaveCommand.getPictureRatio())
.picturePose(foundPicturePose)
.picturePose(picturePose)
.userFacePictureList(uploadedFacePictureList)
.build();

Expand All @@ -230,10 +212,32 @@ public PictureGenerateRequest createPGREQ(Long userId, PGREQSaveCommand pgreqSav

return savedPGREQ;
} finally {
Boolean keyDeleted = redisTemplate.delete(lockKey);
if (Boolean.FALSE.equals(keyDeleted)) {
log.error("Redis에 key {} 값이 존재하지 않습니다.", lockKey);
}
releaseLock(lockKey);
}
}

private PicturePose processPicturePose(PGREQSaveCommand pgreqSaveCommand, User foundUser) {
String posePictureKey = pgreqSaveCommand.getPosePictureKey();
if (Objects.isNull(posePictureKey)) {
return null;
}
return pictureService.findByKeyPicturePose(posePictureKey).orElseGet(() -> {
log.info("""
%s 유저가 요청에 포함한 포즈참고사진 key [%s] 기존 사진을 찾을 수 없어 신규 저장""".formatted(foundUser.getEmail(),
posePictureKey));
return pictureService.updatePicture(
CreatePicturePoseCommand.builder().key(posePictureKey).uploader(foundUser).build());
});
}

private Boolean acquireLock(String lockKey) {
return redisTemplate.opsForValue().setIfAbsent(lockKey, "locked", LOCK_TTL_SECONDS, TimeUnit.SECONDS);
}

private void releaseLock(String lockKey) {
Boolean keyDeleted = redisTemplate.delete(lockKey);
if (Boolean.FALSE.equals(keyDeleted)) {
log.error("Redis에 key {} 값이 존재하지 않습니다.", lockKey);
}
}

Expand Down
3 changes: 2 additions & 1 deletion genti-domain/src/main/java/com/gt/genti/common/EnumUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ private static <E extends Enum<E> & ConvertableEnum> E toEnum(Class<E> enumType,
if (value == null) {
E enumNullValue = enumType.getEnumConstants()[0].getNullValue();
if (enumNullValue == null) {
throw ExpectedException.withLogging(ResponseCode.NotNullableEnum, enumType);
// throw ExpectedException.withLogging(ResponseCode.NotNullableEnum, enumType);
return null;
} else {
return enumNullValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public String convertToDatabaseColumn(T attribute) {
} catch (NullPointerException e) {
T enumNullValue = enumClassType.getEnumConstants()[0].getNullValue();
if (enumNullValue == null) {
throw ExpectedException.withLogging(ResponseCode.NotNullableEnum, enumClassType.getName());
// throw ExpectedException.withLogging(ResponseCode.NotNullableEnum, enumClassType.getName());
return null;
} else {
return enumNullValue.getStringValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ public class PictureGenerateRequest extends BaseTimeEntity {
@JoinColumn(name = "picture_pose_id")
PicturePose picturePose;

@Column(name = "camera_angle", nullable = false)
@Column(name = "camera_angle")
@Convert(converter = CameraAngleConverter.class)
CameraAngle cameraAngle;

@Column(name = "shot_coverage", nullable = false)
@Column(name = "shot_coverage")
@Convert(converter = ShotCoverageConverter.class)
ShotCoverage shotCoverage;

Expand Down

0 comments on commit 7f682db

Please sign in to comment.