Skip to content

Commit

Permalink
Merge pull request #270 from twenty-three-23/dev
Browse files Browse the repository at this point in the history
TT-334 버전 1.0.0 main으로 merge
  • Loading branch information
snacktime81 authored Jul 29, 2024
2 parents 90dffa9 + 6fa4ff1 commit d988d79
Show file tree
Hide file tree
Showing 17 changed files with 226 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static LocalTime calculateExpectedTime(String text) {
int wordsCount = words.length;

float expectedTimeToSecond = wordsCount * DEFAULT_TIME_PER_WORD_SECOND;

expectedTimeToSecond /= 3f;
LocalTime expectedTime = transferSeoondToLocalTime(expectedTimeToSecond);

return expectedTime;
Expand All @@ -24,7 +24,8 @@ public static LocalTime transferSeoondToLocalTime(float time) {
int hour = second / 3600;
int minute = (second % 3600) / 60;
int secondSet = (second % 3600) % 60;
int milliSecond = (int) ((time - second) * 1_000_000_000);
int milliSecond = ((int)((time-second)*100)) * 10_000_000;

return LocalTime.of(hour, minute, secondSet, milliSecond);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import com.twentythree.peech.script.service.ScriptService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
Expand All @@ -20,7 +18,7 @@ public class ScriptController implements SwaggerScriptInterface{

private final ScriptSentenceFacade scriptSentenceFacade;
private final ScriptService scriptService;
private final SaveModifyScriptService redisScriptSentenceFacade;
private final SaveModifyScriptService saveModifyScriptService;

@Operation(summary = "새로운 대본 생성",
description = "특정 주제를 themeId로 path에 넣고, 스크립트를 문단들로 나누어 RequestBody에 입력하면 새로운 버전, 대본, 문장 생성 및 저장.")
Expand Down Expand Up @@ -77,9 +75,8 @@ public ModifyScriptResponseDTO modifyScript(@PathVariable Long themeId, @PathVar

@Override
@PutMapping("api/v1/themes/{themeId}/scripts/{scriptId}")
public ResponseEntity<String> saveModifyScript(@PathVariable Long themeId,@PathVariable Long scriptId,@LoginUserId UserIdDTO userId) {
redisScriptSentenceFacade.saveModifyScript(themeId, scriptId, userId.userId());
return new ResponseEntity<>("대본 저장에 성공하였습니다", HttpStatus.OK);
public SaveScriptAndSentencesResponseDTO saveModifyScript(@PathVariable Long themeId,@PathVariable Long scriptId,@LoginUserId UserIdDTO userId) {
return saveModifyScriptService.saveModifyScript(themeId, scriptId, userId.userId());
}

@Operation(summary = "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ ExpectedTimeResponseDTO getScriptAndParagraphsExpectedTime(@PathVariable("themeI
@ApiResponse(responseCode = "200", description = "success", content = {@Content(schema = @Schema(implementation = ModifyScriptResponseDTO.class), mediaType = "application/json")})
ModifyScriptResponseDTO modifyScript(@PathVariable Long themeId, @PathVariable Long scriptId, @RequestBody ModifiedScriptRequestDTO request, @LoginUserId UserIdDTO userId);

@ApiResponse(responseCode = "200", description = "success", content = {@Content(schema = @Schema(implementation = ParagraphsResponseDTO.class), mediaType = "application/json")})
ResponseEntity<String> saveModifyScript(@PathVariable Long themeId, @PathVariable Long scriptId, @Parameter(hidden = true) @LoginUserId UserIdDTO userId);
@ApiResponse(responseCode = "200", description = "success", content = {@Content(schema = @Schema(implementation = SaveScriptAndSentencesResponseDTO.class), mediaType = "application/json")})
SaveScriptAndSentencesResponseDTO saveModifyScript(@PathVariable Long themeId, @PathVariable Long scriptId, @Parameter(hidden = true) @LoginUserId UserIdDTO userId);

@ApiResponse(responseCode = "200", description = "success", content = {@Content(schema = @Schema(implementation = ParagraphsResponseDTO.class), mediaType = "application/json")})
@GetMapping("api/v1/themes/{themeId}/scripts/{scriptId}/paragraphs")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.twentythree.peech.script.domain;

public enum InputAndSttType {
INPUT, STT
INPUT, STT, MODIFY
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,26 @@ private ScriptEntity(VersionEntity version, String scriptContent, LocalTime time
if (DType == InputAndSttType.INPUT) {
this.totalExpectTime = time;
this.DType = InputAndSttType.INPUT;
} else if (DType == InputAndSttType.STT) {
} else if (DType == InputAndSttType.MODIFY) {
this.DType = InputAndSttType.MODIFY;
this.totalRealTime = time;
this.DType = InputAndSttType.STT;
} else {
}
else {
throw new IllegalArgumentException("InputAndSttType이 올바르지 않게 입력 되었다.");
}
}

private ScriptEntity(VersionEntity version, String scriptContent, LocalTime totalRealTime, LocalTime totalExpectTime, InputAndSttType dType) {
if (dType != InputAndSttType.STT) {
throw new IllegalArgumentException("팩토리 함수를 잘못사용했습니다.");
}
this.version = version;
this.scriptContent = scriptContent;
this.totalRealTime = totalRealTime;
this.totalExpectTime = totalExpectTime;
this.DType = dType;
}


public static ScriptEntity ofCreateInputScript(VersionEntity version,
String scriptContent,
Expand All @@ -75,10 +87,22 @@ public static ScriptEntity ofCreateInputScript(VersionEntity version,
public static ScriptEntity ofCreateSTTScript(VersionEntity version,
String scriptContent,
LocalTime totalRealTime,
LocalTime totalExpectTime,
InputAndSttType DType) {
if (DType != InputAndSttType.STT) {
throw new IllegalArgumentException("팩토리 함수를 잘못사용했습니다.");
}
return new ScriptEntity(version, scriptContent, totalRealTime, totalExpectTime, DType);
}

// 추후 리팩토링 예정
public static ScriptEntity ofCreateModifyScript(VersionEntity version,
String scriptContent,
LocalTime totalRealTime,
InputAndSttType DType) {
if (DType != InputAndSttType.MODIFY) {
throw new IllegalArgumentException("팩토리 함수를 잘못사용했습니다.");
}
return new ScriptEntity(version, scriptContent, totalRealTime, DType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ private SentenceEntity(ScriptEntity scriptEntity, Long paragraphId, String sente
this.sentenceExpectTime = time;
} else if(scriptEntity.getDType() == InputAndSttType.STT){
this.sentenceRealTime = time;
} else if (scriptEntity.getDType() == InputAndSttType.MODIFY) {
this.sentenceRealTime = time;
} else {
throw new IllegalArgumentException("InputAndSttType이 올바르지 않게 입력 되었다.");
}
Expand All @@ -77,6 +79,16 @@ public static SentenceEntity ofCreateInputSentence(ScriptEntity scriptEntity,
}
return new SentenceEntity(scriptEntity, paragraphId, sentenceContent, sentenceOrder, sentenceExpectTime);
}

// 추후 리팩토링 예정
public static SentenceEntity ofCreateModifySentence(ScriptEntity scriptEntity,
Long paragraphId, String sentenceContent,
Long sentenceOrder, LocalTime sentenceRealTime){
if(scriptEntity.getDType() != InputAndSttType.MODIFY) {
throw new IllegalArgumentException("팩토리얼 함수를 잘못 사용했습니다.");
}
return new SentenceEntity(scriptEntity, paragraphId, sentenceContent, sentenceOrder, sentenceRealTime);
}

public static SentenceEntity ofCreateInputAndSTTSentence(ScriptEntity scriptEntity,
Long paragraphId, String sentenceContent, Long sentenceOrder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@
import com.twentythree.peech.script.cache.CacheService;
import com.twentythree.peech.script.domain.*;
import com.twentythree.peech.script.dto.RedisSentenceDTO;
import com.twentythree.peech.script.dto.response.SaveScriptAndSentencesResponseDTO;
import com.twentythree.peech.script.repository.ScriptRepository;
import com.twentythree.peech.script.repository.SentenceRepository;
import com.twentythree.peech.script.repository.ThemeRepository;
import com.twentythree.peech.script.repository.VersionRepository;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;


@RequiredArgsConstructor
@Service
public class SaveModifyScriptService {
Expand All @@ -24,28 +29,34 @@ public class SaveModifyScriptService {
private final ThemeRepository themeRepository;
private final SentenceRepository sentenceRepository;
private final VersionRepository versionRepository;
private final Logger log= LoggerFactory.getLogger(this.getClass().getName());

@Transactional
public void saveModifyScript(Long themeId, Long scriptId, Long userId) {
public SaveScriptAndSentencesResponseDTO saveModifyScript(Long themeId, Long scriptId, Long userId) {

// 문장 리스트 가져오기
List<String> sentenceIdList = cacheService.findAllByUserKey("user"+userId);
log.info("sentenceIdList: {}", sentenceIdList);


// 문장 정보들 저장
List<RedisSentenceDTO> redisSentenceInformationList = new ArrayList<>();
log.info("sentenceIdList: {}", sentenceIdList);

for(String sentenceId : sentenceIdList) {
// 문장 정보 가져오기
RedisSentenceDTO redisSentence = cacheService.findByKey(sentenceId);
redisSentenceInformationList.add(redisSentence);
}

ScriptEntity sttScriptEntity = scriptRepository.findById(scriptId).orElseThrow(() -> new IllegalArgumentException("해당 스크립트가 존재하지 않습니다."));
ScriptEntity sttScriptEntity = scriptRepository.findById(scriptId)
.orElseThrow(() -> new IllegalArgumentException("해당 스크립트가 존재하지 않습니다."));

Long majorVersion = sttScriptEntity.getVersion().getMajorVersion();
Long minorVersion = sttScriptEntity.getVersion().getMinorVersion();

ThemeEntity themeEntity = themeRepository.findById(themeId).orElseThrow(() -> new IllegalArgumentException("해당 테마가 존재하지 않습니다."));
ThemeEntity themeEntity = themeRepository.findById(themeId)
.orElseThrow(() -> new IllegalArgumentException("해당 테마가 존재하지 않습니다."));

// 최신 minorVersion 생성
VersionEntity versionEntity = VersionEntity
Expand All @@ -55,27 +66,32 @@ public void saveModifyScript(Long themeId, Long scriptId, Long userId) {
// 스크립트 저장
// 스크립트 저장 전에 List<RedisSentenceDTO> 에서 SentenceContent를 합쳐서 전체 내용을 만들어야함
String fullScript = addFullScript(redisSentenceInformationList);

LocalTime totalScriptTime = addTotalScriptTime(redisSentenceInformationList);

ScriptEntity scriptEntity = ScriptEntity
.ofCreateInputScript(versionEntity, fullScript, totalScriptTime, InputAndSttType.INPUT);
.ofCreateModifyScript(versionEntity, fullScript, totalScriptTime, InputAndSttType.MODIFY);

scriptRepository.save(scriptEntity);
ScriptEntity saveScript = scriptRepository.save(scriptEntity);

// 문장 저장
redisSentenceInformationList.forEach(redisSentenceDTO -> {

SentenceEntity sentenceEntity = SentenceEntity.ofCreateInputSentence(scriptEntity,
SentenceEntity sentenceEntity = SentenceEntity.ofCreateModifySentence(scriptEntity,
redisSentenceDTO.getParagraphId(), redisSentenceDTO.getSentenceContent(),
redisSentenceDTO.getSentenceOrder(), redisSentenceDTO.getTime());

sentenceRepository.save(sentenceEntity);
});

return new SaveScriptAndSentencesResponseDTO(saveScript.getScriptId());
}
// 문장 합치기
private String addFullScript(List<RedisSentenceDTO> redisSentenceDTOList) {

String[] sentenceContentList = redisSentenceDTOList.stream()
.sorted(Comparator.comparingLong(RedisSentenceDTO::getParagraphId)
.thenComparing(RedisSentenceDTO::getSentenceOrder))
.map(RedisSentenceDTO::getSentenceContent).toArray(String[]::new);

return String.join(" ", sentenceContentList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.twentythree.peech.script.domain.ScriptEntity;
import com.twentythree.peech.script.domain.SentenceEntity;
import com.twentythree.peech.script.dto.RedisSentenceDTO;
import com.twentythree.peech.script.dto.SaveScriptDTO;
import com.twentythree.peech.script.dto.paragraphIdToExpectedTime;
import com.twentythree.peech.script.dto.response.ExpectedTimeResponseDTO;
Expand All @@ -13,6 +14,7 @@

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -53,13 +55,16 @@ public MinorDetailScriptDTO getMinorScriptAndSentence(Long themeId, Long majorVe
for(Map.Entry<Long, List<SentenceEntity>> sentenceEntry : groupedSentences.entrySet()) {

List<SentenceEntity> sentenceList = sentenceEntry.getValue();
List<String> sentenceContentList = sentenceEntry.getValue().stream().map(SentenceEntity::getSentenceContent).toList();
List<String> sentenceContentList = sentenceEntry.getValue().stream().sorted(Comparator.comparingLong(SentenceEntity::getSentenceOrder))
.map(SentenceEntity::getSentenceContent).toList();

// 문단에 해당하는 시간 합산
LocalTime realTimePerParagraph = sentenceList.stream()
.map(SentenceEntity::getSentenceRealTime)
.reduce(LocalTime.of(0,0,0),
((localTime, localTime2) -> localTime.plusHours(localTime2.getHour()).plusMinutes(localTime2.getMinute()).plusSeconds(localTime2.getSecond())));
.reduce(LocalTime.of(0,0,0, 0),
((localTime, localTime2) -> localTime.plusHours(localTime2.getHour())
.plusMinutes(localTime2.getMinute()).plusSeconds(localTime2.getSecond())
.plusNanos(localTime2.getNano())));

ParagraphDetail paragraphDetail = new ParagraphDetail(realTimePerParagraph, sentenceContentList);
paragraphDetails.add(paragraphDetail);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public SaveScriptDTO saveInputScript(Long themeId, String[] paragraphs) {
}

@Transactional
public Mono<SaveSTTScriptVO> saveSTTScriptVO(Long themeId, Long scriptId, ClovaResponseDto clovaResponseDto) {
public Mono<SaveSTTScriptVO> saveSTTScriptVO(Long themeId, Long scriptId, ClovaResponseDto clovaResponseDto, LocalTime totalExpectedTime) {

ScriptEntity scriptEntity = scriptRepository.findById(scriptId).orElseThrow(() -> new IllegalArgumentException("scriptId가 잘못 되었습니다."));

Expand All @@ -82,17 +82,17 @@ public Mono<SaveSTTScriptVO> saveSTTScriptVO(Long themeId, Long scriptId, ClovaR
VersionEntity versionEntity = VersionEntity.ofCreateSTTScriptVersionAfterInput(majorVersion, latestMinorVersion, ThemeEntity);


return Mono.just(saveSTTScriptEntity(themeId, clovaResponseDto, versionEntity));
return Mono.just(saveSTTScriptEntity(themeId, clovaResponseDto, versionEntity, totalExpectedTime));
}

@Transactional
// Version과 SCRIPT Entity 저장 로직은 공통이므로 묶어서 처리
public SaveSTTScriptVO saveSTTScriptEntity(Long themeId, ClovaResponseDto clovaResponseDto, VersionEntity versionEntity) {
public SaveSTTScriptVO saveSTTScriptEntity(Long themeId, ClovaResponseDto clovaResponseDto, VersionEntity versionEntity, LocalTime totalExpectedTime) {


ThemeEntity ThemeEntity = themeRepository.findById(themeId).orElseThrow(() -> new IllegalArgumentException("패키지 아이디가 잘못되었습니다."));

ScriptEntity sttScriptEntity = ScriptEntity.ofCreateSTTScript(versionEntity, clovaResponseDto.getFullText(), clovaResponseDto.getTotalRealTime(), InputAndSttType.STT);
ScriptEntity sttScriptEntity = ScriptEntity.ofCreateSTTScript(versionEntity, clovaResponseDto.getFullText(), clovaResponseDto.getTotalRealTime(), totalExpectedTime,InputAndSttType.STT);

versionRepository.save(versionEntity);
scriptRepository.save(sttScriptEntity);
Expand All @@ -101,7 +101,7 @@ public SaveSTTScriptVO saveSTTScriptEntity(Long themeId, ClovaResponseDto clovaR
}

@Transactional
public Mono<SaveSTTScriptVO> saveSTTScriptVO(Long themeId, ClovaResponseDto clovaResponseDto) {
public Mono<SaveSTTScriptVO> saveSTTScriptVO(Long themeId, ClovaResponseDto clovaResponseDto, LocalTime totalExpectedTime) {

String script = clovaResponseDto.getFullText();

Expand All @@ -114,7 +114,7 @@ public Mono<SaveSTTScriptVO> saveSTTScriptVO(Long themeId, ClovaResponseDto clov
// 대본 입력이 없는 경우에는 해당 스크립트를 Input script 취급
VersionEntity versionEntity = VersionEntity.ofCreateInputScriptVersion(latestMajorVersion, themeId, ThemeEntity);

return Mono.just(saveSTTScriptEntity(themeId, clovaResponseDto, versionEntity));
return Mono.just(saveSTTScriptEntity(themeId, clovaResponseDto, versionEntity, totalExpectedTime));
}

public LocalTime getInputExpectedScriptTime(Long scriptId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ public List<SentenceEntity> saveSTTSentences(ScriptEntity scriptEntity, List<Add
return sentenceEntityList;
}

// 전체 예상시간을 가져오는 로직
public LocalTime getTotalExpectedTime (List<AddSentenceInformationVO> sentenceList) {
LocalTime totalExpectedTime = LocalTime.of(0, 0, 0);
for(AddSentenceInformationVO sentence : sentenceList){
LocalTime expectedTime = ScriptUtils.calculateExpectedTime(sentence.sentenceContent());
totalExpectedTime = totalExpectedTime.plusHours(expectedTime.getHour())
.plusMinutes(expectedTime.getMinute())
.plusSeconds(expectedTime.getSecond())
.plusNanos(expectedTime.getNano());
}
return totalExpectedTime;
}

public List<paragraphIdToExpectedTime> getParagraphExpectedTime(Long scriptId) {

List<paragraphIdToExpectedTime> results = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class STTParagraphDTO {

private Long paragraphId;
private Long paragraphOrder;
private String measurementResult;
private LocalTime time;
private LocalTime startTime;
private LocalTime endTime;
Expand Down
Loading

0 comments on commit d988d79

Please sign in to comment.