diff --git a/src/main/java/notai/stt/query/SttQueryRepository.java b/src/main/java/notai/stt/query/SttQueryRepository.java index 2fd0681..18b5d77 100644 --- a/src/main/java/notai/stt/query/SttQueryRepository.java +++ b/src/main/java/notai/stt/query/SttQueryRepository.java @@ -6,4 +6,5 @@ public interface SttQueryRepository { List findAllByDocumentIdAndPageNumber(Long documentId, Integer pageNumber); + List findAllByDocumentId(Long documentId); } diff --git a/src/main/java/notai/stt/query/SttQueryRepositoryImpl.java b/src/main/java/notai/stt/query/SttQueryRepositoryImpl.java index 5bee074..250e12f 100644 --- a/src/main/java/notai/stt/query/SttQueryRepositoryImpl.java +++ b/src/main/java/notai/stt/query/SttQueryRepositoryImpl.java @@ -20,4 +20,13 @@ public List findAllByDocumentIdAndPageNumber(Long documentId, Integer pageN .and(stt.pageNumber.eq(pageNumber))) .fetch(); } + + @Override + public List findAllByDocumentId(Long documentId) { + return queryFactory + .selectFrom(stt) + .join(stt.recording).fetchJoin() + .where(stt.recording.document.id.eq(documentId)) + .fetch(); + } } diff --git a/src/main/java/notai/sttTask/application/SttTaskQueryService.java b/src/main/java/notai/sttTask/application/SttTaskQueryService.java new file mode 100644 index 0000000..133ccb5 --- /dev/null +++ b/src/main/java/notai/sttTask/application/SttTaskQueryService.java @@ -0,0 +1,53 @@ +package notai.sttTask.application; + +import lombok.RequiredArgsConstructor; +import notai.document.domain.Document; +import notai.document.domain.DocumentRepository; +import notai.llm.domain.TaskStatus; +import static notai.llm.domain.TaskStatus.*; +import notai.member.domain.Member; +import notai.member.domain.MemberRepository; +import notai.stt.domain.Stt; +import notai.stt.domain.SttRepository; +import notai.sttTask.application.result.SttTaskOverallStatusResult; +import notai.sttTask.domain.SttTask; +import notai.sttTask.domain.SttTaskRepository; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Collections; +import java.util.List; + +@Transactional(readOnly = true) +@RequiredArgsConstructor +@Service +public class SttTaskQueryService { + + private final DocumentRepository documentRepository; + private final MemberRepository memberRepository; + private final SttTaskRepository sttTaskRepository; + private final SttRepository sttRepository; + + public SttTaskOverallStatusResult fetchOverallStatus(Long memberId, Long documentId) { + Document foundDocument = documentRepository.getById(documentId); + Member member = memberRepository.getById(memberId); + foundDocument.validateOwner(member); + + List sttResults = sttRepository.findAllByDocumentId(documentId); + + if (sttResults.isEmpty()) { + return SttTaskOverallStatusResult.of(documentId, NOT_REQUESTED, 0, 0); + } + List taskStatuses = + sttTaskRepository.findAllBySttIn(sttResults).stream().map(SttTask::getStatus).toList(); + + + int totalPages = taskStatuses.size(); + int completedPages = Collections.frequency(taskStatuses, COMPLETED); + + if (totalPages == completedPages) { + return SttTaskOverallStatusResult.of(documentId, COMPLETED, totalPages, completedPages); + } + return SttTaskOverallStatusResult.of(documentId, IN_PROGRESS, totalPages, completedPages); + } +} diff --git a/src/main/java/notai/sttTask/application/result/SttTaskOverallStatusResult.java b/src/main/java/notai/sttTask/application/result/SttTaskOverallStatusResult.java new file mode 100644 index 0000000..fe1ed73 --- /dev/null +++ b/src/main/java/notai/sttTask/application/result/SttTaskOverallStatusResult.java @@ -0,0 +1,19 @@ +package notai.sttTask.application.result; + +import notai.llm.domain.TaskStatus; + +public record SttTaskOverallStatusResult( + Long documentId, + TaskStatus overallStatus, + int totalPages, + int completedPages +) { + public static SttTaskOverallStatusResult of( + Long documentId, + TaskStatus taskStatus, + int totalPages, + int completedPages + ) { + return new SttTaskOverallStatusResult(documentId, taskStatus, totalPages, completedPages); + } +} diff --git a/src/main/java/notai/sttTask/domain/SttTaskRepository.java b/src/main/java/notai/sttTask/domain/SttTaskRepository.java index 08956f1..e2d6682 100644 --- a/src/main/java/notai/sttTask/domain/SttTaskRepository.java +++ b/src/main/java/notai/sttTask/domain/SttTaskRepository.java @@ -2,8 +2,10 @@ import static notai.common.exception.ErrorMessages.AI_SERVER_ERROR; import notai.common.exception.type.NotFoundException; +import notai.stt.domain.Stt; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; import java.util.UUID; public interface SttTaskRepository extends JpaRepository { @@ -11,4 +13,6 @@ public interface SttTaskRepository extends JpaRepository { default SttTask getById(UUID id) { return findById(id).orElseThrow(() -> new NotFoundException(AI_SERVER_ERROR)); } + + List findAllBySttIn(List stts); } diff --git a/src/main/java/notai/sttTask/presentation/SttTaskController.java b/src/main/java/notai/sttTask/presentation/SttTaskController.java new file mode 100644 index 0000000..3208d51 --- /dev/null +++ b/src/main/java/notai/sttTask/presentation/SttTaskController.java @@ -0,0 +1,26 @@ +package notai.sttTask.presentation; + +import lombok.RequiredArgsConstructor; +import notai.auth.Auth; +import notai.sttTask.application.SttTaskQueryService; +import notai.sttTask.application.result.SttTaskOverallStatusResult; +import notai.sttTask.presentation.response.SttTaskOverallStatusResponse; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +@RequiredArgsConstructor +@RestController +public class SttTaskController { + + private final SttTaskQueryService sttTaskQueryService; + + @GetMapping("/status/{documentId}") + public ResponseEntity fetchOverallStatus( + @Auth Long memberId, @PathVariable("documentId") Long documentId + ) { + SttTaskOverallStatusResult result = sttTaskQueryService.fetchOverallStatus(memberId, documentId); + return ResponseEntity.ok(SttTaskOverallStatusResponse.from(result)); + } +} diff --git a/src/main/java/notai/sttTask/presentation/response/SttTaskOverallStatusResponse.java b/src/main/java/notai/sttTask/presentation/response/SttTaskOverallStatusResponse.java new file mode 100644 index 0000000..61d10f0 --- /dev/null +++ b/src/main/java/notai/sttTask/presentation/response/SttTaskOverallStatusResponse.java @@ -0,0 +1,20 @@ +package notai.sttTask.presentation.response; + +import notai.llm.domain.TaskStatus; +import notai.sttTask.application.result.SttTaskOverallStatusResult; + +public record SttTaskOverallStatusResponse( + Long documentId, + TaskStatus overallStatus, + Integer totalPages, + Integer completedPages +) { + public static SttTaskOverallStatusResponse from(SttTaskOverallStatusResult result) { + return new SttTaskOverallStatusResponse( + result.documentId(), + result.overallStatus(), + result.totalPages(), + result.completedPages() + ); + } +}