Skip to content

Commit

Permalink
Feat: #196 2인 사진생성요청 API 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeJae-H committed Nov 17, 2024
1 parent 594373c commit 5b94bee
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.gt.genti.picturegeneraterequest.command;

import com.gt.genti.picture.PictureRatio;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class AdvancedPGREQSaveCommand extends PGREQSaveCommand {

List<String> otherFacePictureKeyList;

@Builder(builderMethodName = "advancedBuilder")
public AdvancedPGREQSaveCommand(String prompt,
List<String> facePictureKeyList,
List<String> otherFacePictureKeyList,
PictureRatio pictureRatio) {
this.prompt = prompt;
this.facePictureKeyList = facePictureKeyList;
this.otherFacePictureKeyList = otherFacePictureKeyList;
this.pictureRatio = pictureRatio;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import com.gt.genti.picture.PictureRatio;
import com.gt.genti.picturegeneraterequest.model.ShotCoverage;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class PGREQSaveCommand {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import com.gt.genti.picturegeneraterequest.dto.request.AdvancedPGREQSaveRequestDto;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down Expand Up @@ -75,6 +76,15 @@ public ResponseEntity<ApiResult<Boolean>> createPictureGenerateRequest(
return GentiResponse.success(true);
}

@PostMapping("/advanced")
public ResponseEntity<ApiResult<Boolean>> createPictureGenerateRequestTwo(
@AuthUser Long userId,
@RequestBody @Valid AdvancedPGREQSaveRequestDto advancedPGREQSaveRequestDto) {
pictureGenerateRequestUseCase.createAdvancedPGREQ(userId,
advancedPGREQSaveRequestDto.toCommand());
return GentiResponse.success(true);
}

@Deprecated
@Logging(item = LogItem.PGREQ, action = LogAction.UPDATE, requester = LogRequester.USER)
@PutMapping("/{pictureGenerateRequestId}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.gt.genti.picturegeneraterequest.dto.request;

import com.gt.genti.picture.dto.request.CommonPictureKeyUpdateRequestDto;
import com.gt.genti.picturegeneraterequest.command.AdvancedPGREQSaveCommand;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Schema(name = "[PGREQ][User] 2인 사진생성요청 생성 dto")
public class AdvancedPGREQSaveRequestDto extends PGREQSaveRequestDto {

@NotNull
@Size(max = 3, min = 1, message = "사용자의 얼굴 사진 개수는 최소 1개, 최대 3개입니다.")
@Schema(description = "추가 인원의 얼굴 사진 리스트")
List<@NotNull CommonPictureKeyUpdateRequestDto> otherFacePictureList;

@Override
public AdvancedPGREQSaveCommand toCommand() {
return AdvancedPGREQSaveCommand.advancedBuilder()
.prompt(this.prompt)
.pictureRatio(this.pictureRatio)
.facePictureKeyList(this.facePictureList.stream().map(CommonPictureKeyUpdateRequestDto::getKey).toList())
.otherFacePictureKeyList(this.otherFacePictureList.stream().map(CommonPictureKeyUpdateRequestDto::getKey).toList())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import static com.gt.genti.constants.RedisConstants.LOCK_TTL_SECONDS;
import static com.gt.genti.picturegeneraterequest.service.mapper.PictureGenerateRequestStatusForUser.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import com.gt.genti.picture.userverification.model.PictureUserVerification;
import com.gt.genti.picturegeneraterequest.command.AdvancedPGREQSaveCommand;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
Expand Down Expand Up @@ -216,6 +218,42 @@ public PictureGenerateRequest createPGREQ(Long userId, PGREQSaveCommand pgreqSav
}
}

@Override
public PictureGenerateRequest createAdvancedPGREQ(Long userId, AdvancedPGREQSaveCommand advancedPGREQSaveCommand) {
User foundUser = findUserById(userId);
throwIfNewPictureGenerateRequestNotAvailable(foundUser);

String lockKey = "LOCK:" + userId + ":" + "createPGREQ";
if (!acquireLock(lockKey)) {
throw ExpectedException.withLogging(ResponseCode.PictureGenerateRequestAlreadyProcessed);
}
try {
List<PictureUserFace> uploadedFacePictureList = pictureService.updateIfNotExistsPictureUserFace(advancedPGREQSaveCommand.getFacePictureKeyList(), foundUser);
List<PictureUserFace> uploadedOtherFacePictureList = pictureService.updateIfNotExistsPictureUserFace(advancedPGREQSaveCommand.getOtherFacePictureKeyList(), foundUser);
List<PictureUserFace> mergedList = new ArrayList<>();
mergedList.addAll(uploadedFacePictureList);
mergedList.addAll(uploadedOtherFacePictureList);

String promptAdvanced = openAIService.getAdvancedPrompt(new PromptAdvancementRequestCommand(advancedPGREQSaveCommand.getPrompt()));
log.info(promptAdvanced);

PictureGenerateRequest createdPGREQ = PictureGenerateRequest.builder()
.requester(foundUser)
.promptAdvanced(promptAdvanced)
.prompt(advancedPGREQSaveCommand.getPrompt())
.pictureRatio(advancedPGREQSaveCommand.getPictureRatio())
.userFacePictureList(mergedList)
.build();

PictureGenerateRequest savedPGREQ = pictureGenerateRequestPort.save(createdPGREQ);
requestMatchService.matchNewRequest(savedPGREQ);

return savedPGREQ;
} finally {
releaseLock(lockKey);
}
}

private PicturePose processPicturePose(PGREQSaveCommand pgreqSaveCommand, User foundUser) {
String posePictureKey = pgreqSaveCommand.getPosePictureKey();
if (Objects.isNull(posePictureKey)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import com.gt.genti.picturegeneraterequest.command.AdvancedPGREQSaveCommand;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

Expand All @@ -23,6 +24,9 @@ public interface PictureGenerateRequestUseCase {
PictureGenerateRequest createPGREQ(Long userId,
PGREQSaveCommand pgreqSaveCommand);

PictureGenerateRequest createAdvancedPGREQ(Long userId,
AdvancedPGREQSaveCommand advancedPGREQSaveCommand);

void modifyPGREQ(Long userId,
Long pictureGenerateRequestId, PGREQSaveRequestDto pgreqSaveRequestDto);

Expand Down

0 comments on commit 5b94bee

Please sign in to comment.