Skip to content

Commit

Permalink
feat: 캐스트 스크립트 검색 API 추가
Browse files Browse the repository at this point in the history
- GET /cast/{id}/script로 해당 캐스트 스크립트 가져옴
- 캐스트 재생은 GET /cast/{id}에서 GET /cast/{id}/audio로 변경 (일관성)
- 이에 따른 SentenceService, SentenceRepository 등 변경
  • Loading branch information
ja7811 committed Aug 9, 2024
1 parent 401e186 commit 4333195
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,19 @@ public ApiResponse<Object> saveCast(@PathVariable("castId") Long castId,
}

/* Cast 재생 API */
@GetMapping("/{castId}")
@GetMapping("/{castId}/audio")
@Operation(summary = "캐스트 재생 API")
public ResponseEntity<UrlResource> streamCast(@PathVariable("castId") Long castId,
@RequestHeader HttpHeaders headers){
return castService.streamCast(castId, headers);
}

/* Cast 스크립트 가져오는 API */
@GetMapping("/{castId}/scripts")
@Operation(summary = "캐스트 스크립트 가져오기 API")
public ApiResponse<Object> findCastScripts(@PathVariable("castId") Long castId){
return castService.getSentenceList(castId);
}

/* Cast 수정 API */

Expand Down
29 changes: 17 additions & 12 deletions src/main/java/com/umc/owncast/domain/cast/service/CastService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;

Expand All @@ -33,7 +32,7 @@ public class CastService {
private final TTSService ttsService;
private final StreamService streamService;
private final FileService fileService;
private final SentenceService sentenceService; // 가칭
private final SentenceService sentenceService;

private final CastRepository castRepository;
private final SentenceRepository sentenceRepository;
Expand All @@ -48,6 +47,7 @@ public class CastService {
public ApiResponse<Object> createCast(KeywordCastCreationDTO castRequest){
String script = scriptService.createScript(castRequest);
TTSResultDTO ttsResult = ttsService.createSpeech(script, castRequest);
// TODO 아랫부분 리팩토링 (다른 createCast()와 중복됨)
Cast cast = Cast.builder()
.voice(castRequest.getVoice())
.audioLength(castRequest.getAudioTime()) // TODO mp3 파일 길이 가져오기
Expand All @@ -68,7 +68,7 @@ public ApiResponse<Object> createCast(KeywordCastCreationDTO castRequest){
castRepository.save(cast);
sentenceRepository.saveAll(sentences);

SimpleCastResponseDTO response = new SimpleCastResponseDTO(cast);
CastScriptDTO response = new CastScriptDTO(cast);
return ApiResponse.of(SuccessCode._OK, response);
}

Expand Down Expand Up @@ -103,7 +103,7 @@ public ApiResponse<Object> createCast(ScriptCastCreationDTO castRequest){
castRepository.save(cast);
sentenceRepository.saveAll(sentences);

SimpleCastResponseDTO response = new SimpleCastResponseDTO(cast);
CastScriptDTO response = new CastScriptDTO(cast);
return ApiResponse.of(SuccessCode._OK, response);
}

Expand All @@ -128,17 +128,14 @@ public ApiResponse<Object> saveCast(Long castId, CastSaveDTO saveRequest){
}

public ApiResponse<Object> updateCast(Long castId, CastUpdateDTO updateRequest) {
Cast target = castRepository.findById(castId).orElseThrow(() -> new NoSuchElementException("캐스트가 존재하지 않습니다"));
// TODO 이미지
// - 원래 이미지 삭제
// - 현재 이미지명 난수화 -> 필요?
// - 현재 이미지 저장 O
Cast cast = castRepository.findById(castId).orElseThrow(() -> new NoSuchElementException("캐스트가 존재하지 않습니다"));
String imagePath = fileService.uploadFile(updateRequest.getCastImage());
// TODO 원래 이미지 삭제 (FileService.removeFile()?)
updateRequest.setImagePath(imagePath);
target.update(updateRequest);
castRepository.save(target);
cast.update(updateRequest);
castRepository.save(cast);

return ApiResponse.of(SuccessCode._OK, target);
return ApiResponse.of(SuccessCode._OK, cast);
}

public ResponseEntity<UrlResource> streamCast(Long castId, HttpHeaders headers) {
Expand All @@ -152,4 +149,12 @@ public ResponseEntity<UrlResource> streamCast(Long castId, HttpHeaders headers)
throw new RuntimeException("캐스트 스트리밍에 실패하였습니다.");
}
}

public ApiResponse<Object> getSentenceList(Long castId) {
List<Sentence> sentences = sentenceService.findCastSentence(castId);
List<SentenceResponseDTO> response = sentences.stream()
.map(SentenceResponseDTO::new)
.toList();
return ApiResponse.of(SuccessCode._OK, response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
import com.umc.owncast.domain.sentence.entity.Sentence;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface SentenceRepository extends JpaRepository<Sentence,Long> {
List<Sentence> findAllByCastIdOrderByTimePointAsc(Long castId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public interface SentenceService {

/** 매개변수를 묶어 List<Sentence>로 반환 */
public List<Sentence> mapToSentence(String original, String korean, TTSResultDTO ttsResultDTO, Cast cast);

public List<Sentence> findCastSentence(Long castId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ public List<Sentence> mapToSentence(String original, String korean, TTSResultDTO
}
return sentences;
}

@Override
public List<Sentence> findCastSentence(Long castId) {
return sentenceRepository.findAllByCastIdOrderByTimePointAsc(castId);
}
}

0 comments on commit 4333195

Please sign in to comment.