Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…TTHON_TEAM_32_BE into develope
  • Loading branch information
Sirius506775 committed Mar 22, 2024
2 parents 39e49a8 + 76755a0 commit 1f1c3b9
Show file tree
Hide file tree
Showing 20 changed files with 392 additions and 17 deletions.
9 changes: 7 additions & 2 deletions src/main/java/site/balpyo/ai/controller/AIUserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.function.ServerRequest;
import site.balpyo.ai.dto.AIGenerateRequest;
import site.balpyo.ai.service.AIGenerateService;
import site.balpyo.common.dto.CommonResponse;
import site.balpyo.common.dto.ErrorEnum;
import site.balpyo.common.util.CommonUtils;

import javax.validation.Valid;

Expand All @@ -24,12 +26,15 @@ public class AIUserController {
public String BALPYO_API_KEY;

@PostMapping("/script")
public ResponseEntity<CommonResponse> generateScript(@Valid @RequestBody AIGenerateRequest aiGenerateRequest){
public ResponseEntity<CommonResponse> generateScript(@Valid @RequestBody AIGenerateRequest aiGenerateRequest,
@RequestHeader(value = "UID", required = false) String uid){

if(!BALPYO_API_KEY.equals(aiGenerateRequest.getBalpyoAPIKey()))return CommonResponse.error(ErrorEnum.BALPYO_API_KEY_ERROR);
//TODO :: 임시 api 시크릿 키 구현 (차후 로그인 연동시 삭제예정)
System.out.println(uid);
if(CommonUtils.isAnyParameterNullOrBlank(uid))return CommonResponse.error(ErrorEnum.BALPYO_UID_KEY_MISSING);

return aiGenerateService.generateScript(aiGenerateRequest);
return aiGenerateService.generateScript(aiGenerateRequest,uid);
}

}
12 changes: 12 additions & 0 deletions src/main/java/site/balpyo/ai/dto/AIGenerateResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package site.balpyo.ai.dto;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor

public class AIGenerateResponse {
private Object resultScript;
private String gptId;
}
22 changes: 15 additions & 7 deletions src/main/java/site/balpyo/ai/entity/AIGenerateLogEntity.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package site.balpyo.ai.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import site.balpyo.ai.dto.AIGenerateRequest;
import site.balpyo.guest.entity.GuestEntity;
import site.balpyo.script.entity.ScriptEntity;

import javax.persistence.*;
import java.time.LocalDateTime;

@Data
@Getter
@Setter
@Entity
@Table(name = "ai_generate_log")
@Builder
Expand All @@ -30,20 +30,28 @@ public class AIGenerateLogEntity {

private double secPerLetter;

@OneToOne(cascade = CascadeType.ALL)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "uid")
private GuestEntity guestEntity;

@OneToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@JoinColumn(name = "gpt_info_id", referencedColumnName = "gptInfoId")
private GPTInfoEntity gptInfoEntity;

@OneToOne(fetch = FetchType.LAZY, mappedBy = "aiGenerateLogEntity", cascade = CascadeType.ALL)
private ScriptEntity scriptEntity;

@CreationTimestamp
private LocalDateTime createdAt;

public AIGenerateLogEntity convertToEntity(AIGenerateRequest aiGenerateRequest, GPTInfoEntity gptInfoEntity){
public AIGenerateLogEntity convertToEntity(AIGenerateRequest aiGenerateRequest, GPTInfoEntity gptInfoEntity,GuestEntity guestEntity){
return AIGenerateLogEntity.builder()
.secTime(aiGenerateRequest.getSecTime())
.topic(aiGenerateRequest.getTopic())
.keywords(aiGenerateRequest.getKeywords())
.secPerLetter(0) // TODO :: 차후 0 값 변경
.gptInfoEntity(gptInfoEntity)
.guestEntity(guestEntity)
.build();
}

Expand Down
9 changes: 4 additions & 5 deletions src/main/java/site/balpyo/ai/entity/GPTInfoEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.springframework.stereotype.Service;
import site.balpyo.ai.dto.GPTResponse;

import javax.annotation.processing.Completion;
import javax.persistence.*;
import java.time.LocalDateTime;

@Data
@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name = "gpt_info_entity")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import site.balpyo.ai.entity.AIGenerateLogEntity;
import site.balpyo.ai.entity.GPTInfoEntity;

import java.util.Optional;

@Repository
public interface AIGenerateLogRepository extends JpaRepository<AIGenerateLogEntity, Long> {
Optional<AIGenerateLogEntity> findByGptInfoEntity(GPTInfoEntity gptInfoEntity);
}
29 changes: 26 additions & 3 deletions src/main/java/site/balpyo/ai/service/AIGenerateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import site.balpyo.ai.dto.AIGenerateRequest;
import site.balpyo.ai.dto.AIGenerateResponse;
import site.balpyo.ai.entity.AIGenerateLogEntity;
import site.balpyo.ai.entity.GPTInfoEntity;
import site.balpyo.ai.repository.AIGenerateLogRepository;
import site.balpyo.ai.repository.GPTInfoRepository;
import site.balpyo.common.dto.CommonResponse;
import site.balpyo.common.dto.ErrorEnum;
import site.balpyo.common.util.CommonUtils;
import site.balpyo.guest.entity.GuestEntity;
import site.balpyo.guest.repository.GuestRepository;

import javax.transaction.Transactional;
import java.lang.reflect.Array;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
@RequiredArgsConstructor
Expand All @@ -26,10 +33,13 @@ public class AIGenerateService {

private final AIGenerateLogRepository aiGenerateLogRepository;

private final GuestRepository guestRepository;


@Value("${secrets.GPT_API_KEY}")
public String GPT_API_KEY;
public ResponseEntity<CommonResponse> generateScript(AIGenerateRequest request){
@Transactional
public ResponseEntity<CommonResponse> generateScript(AIGenerateRequest request,String uid){

// TODO :: TEST인 경우 TEST값 반환 <- 개발 완료 후 삭제 예정
if(request.isTest()) return CommonResponse.success(new GPTTestObject().getGPTTestObject());
Expand All @@ -48,11 +58,24 @@ public ResponseEntity<CommonResponse> generateScript(AIGenerateRequest request){
Object resultBody = generatedScriptObject.getBody();
//5. GPT 응답에서 GPTInfoEntity 추출 및 jpa로 저장할 수 있도록 GPTInfoEntity로변환
GPTInfoEntity gptInfoData = new GPTInfoEntity().ResponseBodyToGPTInfoEntity(resultBody);


GuestEntity guestEntity = null;
Optional<GuestEntity> optionalGuestEntity= guestRepository.findById(uid);
if(optionalGuestEntity.isPresent()){
guestEntity = optionalGuestEntity.get();
}

//6. AI 사용기록에 gpt정보와 요청값들을 AIGenerateLogEntity형태로 변환
AIGenerateLogEntity aiGenerateLog = new AIGenerateLogEntity().convertToEntity(request , gptInfoData);
AIGenerateLogEntity aiGenerateLog = new AIGenerateLogEntity().convertToEntity(request , gptInfoData,guestEntity);



aiGenerateLogRepository.save(aiGenerateLog); //저장
String GPTId = aiGenerateLog.getGptInfoEntity().getGptInfoId();


return CommonResponse.success(resultScript);
return CommonResponse.success(new AIGenerateResponse(resultScript,GPTId));
}


Expand Down
1 change: 1 addition & 0 deletions src/main/java/site/balpyo/common/dto/ErrorEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum ErrorEnum {

//9000 - client 계열 에러
BALPYO_API_KEY_ERROR("9000", "BALPYO_API_KEY를 다시 확인해주세요."),
BALPYO_UID_KEY_MISSING("9001", "헤더의 UID를 다시 확인해주세요."),

// 5000 - 내부 서버 에러
INTERNAL_SERVER_ERROR("5000", "내부 서버 오류가 발생했습니다.");
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/site/balpyo/common/util/GuestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package site.balpyo.common.util;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import site.balpyo.guest.entity.GuestEntity;
import site.balpyo.guest.repository.GuestRepository;

import java.util.Optional;

@Component
@RequiredArgsConstructor
public class GuestUtils {

public static boolean verifyUID(String uid, GuestRepository guestRepository){
Optional<GuestEntity> guestEntity = guestRepository.findById(uid);
return guestEntity.isPresent();
}
}
29 changes: 29 additions & 0 deletions src/main/java/site/balpyo/guest/controller/GuestController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package site.balpyo.guest.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import site.balpyo.common.dto.CommonResponse;
import site.balpyo.common.dto.ErrorEnum;
import site.balpyo.common.util.CommonUtils;
import site.balpyo.guest.service.GuestService;

@RestController
@CrossOrigin
@RequiredArgsConstructor
@RequestMapping("/guest/manage")
public class GuestController {

private final GuestService guestService;
@PostMapping("/uid")
public ResponseEntity<CommonResponse> generateUniqueIdentifier(){
return guestService.generateUID();
}

@GetMapping("/uid")
public ResponseEntity<CommonResponse> verifyUID( @RequestHeader(value = "UID", required = false) String uid){
if(CommonUtils.isAnyParameterNullOrBlank(uid))return CommonResponse.error(ErrorEnum.BALPYO_UID_KEY_MISSING);
return guestService.verifyUID(uid);
}

}
13 changes: 13 additions & 0 deletions src/main/java/site/balpyo/guest/dto/UIDResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package site.balpyo.guest.dto;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class UIDResponse {

private String uid;


}
11 changes: 11 additions & 0 deletions src/main/java/site/balpyo/guest/dto/VerifyResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package site.balpyo.guest.dto;

import lombok.AllArgsConstructor;
import lombok.Data;

@AllArgsConstructor
@Data
public class VerifyResponse {
boolean isVerified;
String yourUID;
}
33 changes: 33 additions & 0 deletions src/main/java/site/balpyo/guest/entity/GuestEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package site.balpyo.guest.entity;

import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import site.balpyo.ai.entity.AIGenerateLogEntity;
import site.balpyo.script.entity.ScriptEntity;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.List;

@Getter
@Setter
@Entity
@Table(name = "guest")
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class GuestEntity {

@Id
private String uid;

@CreationTimestamp
private LocalDateTime createdAt;

@OneToMany(mappedBy = "guestEntity", cascade = CascadeType.ALL)
private List<AIGenerateLogEntity> aiGenerateLogEntities;

@OneToMany(mappedBy = "guestEntity", cascade = CascadeType.ALL)
private List<ScriptEntity> scriptEntities;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package site.balpyo.guest.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import site.balpyo.guest.entity.GuestEntity;

public interface GuestRepository extends JpaRepository<GuestEntity, String> {
}
37 changes: 37 additions & 0 deletions src/main/java/site/balpyo/guest/service/GuestService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package site.balpyo.guest.service;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import site.balpyo.common.dto.CommonResponse;
import site.balpyo.common.util.GuestUtils;
import site.balpyo.guest.dto.UIDResponse;
import site.balpyo.guest.dto.VerifyResponse;
import site.balpyo.guest.entity.GuestEntity;
import site.balpyo.guest.repository.GuestRepository;

import java.util.UUID;

@Service
@RequiredArgsConstructor
public class GuestService {

private final GuestRepository guestRepository;
public ResponseEntity<CommonResponse> generateUID(){
UUID uuid = UUID.randomUUID();
String uuidString = uuid.toString();

GuestEntity guestEntity = GuestEntity.builder()
.uid(uuidString)
.build();
guestRepository.save(guestEntity);

return CommonResponse.success(new UIDResponse(uuidString));
}

public ResponseEntity<CommonResponse> verifyUID(String uid) {
boolean isVerified = GuestUtils.verifyUID(uid,guestRepository);

return CommonResponse.success(new VerifyResponse(isVerified,uid));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package site.balpyo.script.controller;


import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import site.balpyo.common.dto.CommonResponse;
import site.balpyo.common.dto.ErrorEnum;
import site.balpyo.common.util.CommonUtils;
import site.balpyo.script.dto.ScriptRequest;
import site.balpyo.script.service.ScriptService;

@RestController
@CrossOrigin
@RequiredArgsConstructor
@RequestMapping("/every/manage")
public class EveryScriptController {

private final ScriptService scriptService;

@PostMapping("/script")
public ResponseEntity<CommonResponse> saveScript(@RequestBody ScriptRequest scriptRequest,
@RequestHeader(value = "UID", required = false) String uid){

if(CommonUtils.isAnyParameterNullOrBlank(uid))return CommonResponse.error(ErrorEnum.BALPYO_UID_KEY_MISSING);
return scriptService.saveScript(scriptRequest,uid);
}
}
Loading

0 comments on commit 1f1c3b9

Please sign in to comment.