|
| 1 | +package io.ejangs.docsa.domain.branch.app; |
| 2 | + |
| 3 | +import io.ejangs.docsa.domain.branch.dao.mysql.BranchRepository; |
| 4 | +import io.ejangs.docsa.domain.branch.entity.Branch; |
| 5 | +import io.ejangs.docsa.domain.commit.entity.Commit; |
| 6 | +import io.ejangs.docsa.domain.edge.dto.graph.BranchGraphDto; |
| 7 | +import io.ejangs.docsa.domain.doc.entity.Doc; |
| 8 | +import io.ejangs.docsa.global.exception.CustomException; |
| 9 | +import io.ejangs.docsa.global.exception.errorcode.BranchErrorCode; |
| 10 | +import io.ejangs.docsa.global.util.RenewUpdatedAtHelper; |
| 11 | +import java.util.List; |
| 12 | +import lombok.RequiredArgsConstructor; |
| 13 | +import org.springframework.stereotype.Service; |
| 14 | +import org.springframework.transaction.annotation.Transactional; |
| 15 | + |
| 16 | +@Service |
| 17 | +@RequiredArgsConstructor |
| 18 | +public class BranchQueryService { |
| 19 | + |
| 20 | + private final BranchRepository branchRepository; |
| 21 | + |
| 22 | + @Transactional(readOnly = true) |
| 23 | + public Branch getById(Long id) { |
| 24 | + return branchRepository.findById(id).orElseThrow(() -> new CustomException(BranchErrorCode.BRANCH_NOT_FOUND)); |
| 25 | + } |
| 26 | + |
| 27 | + @Transactional(readOnly = true) |
| 28 | + public List<BranchGraphDto> getBranchGraphList(Long docId) { |
| 29 | + return branchRepository.findBranchGraphDtoList(docId); |
| 30 | + } |
| 31 | + |
| 32 | + public Branch save(Branch branch) { |
| 33 | + return branchRepository.save(branch); |
| 34 | + } |
| 35 | + |
| 36 | + public Branch createBranch(Doc doc, String branchName) { |
| 37 | + Branch branch = branchRepository.save(Branch.builder().name(branchName).doc(doc).build()); |
| 38 | + RenewUpdatedAtHelper.touch(branch); |
| 39 | + return branch; |
| 40 | + } |
| 41 | + |
| 42 | + public Branch createBranch(Doc doc, String branchName, Commit fromCommit) { |
| 43 | + Branch branch = Branch.builder().name(branchName).doc(doc).fromCommit(fromCommit).build(); |
| 44 | + return branchRepository.save(branch); |
| 45 | + } |
| 46 | + |
| 47 | + @Transactional(readOnly = true) |
| 48 | + public boolean existsSubBranchByFromCommitIds(List<Long> commitIds) { |
| 49 | + return branchRepository.existsByFromCommitIdIn(commitIds); |
| 50 | + } |
| 51 | + |
| 52 | + public void delete(Branch branch) { |
| 53 | + branchRepository.delete(branch); |
| 54 | + } |
| 55 | + |
| 56 | + //TODO: 검증하는 메소드는 별도의 ex.ValidationService로 분리(모든 도메인) |
| 57 | + public void checkBranchInDocOwnedByUser(Long documentId, Long branchId, Long userId) { |
| 58 | + boolean exists = |
| 59 | + branchRepository.existsByIdAndDocIdAndDocUserId(branchId, documentId, userId); |
| 60 | + if (!exists) { |
| 61 | + throw new CustomException(BranchErrorCode.BRANCH_NOT_FOUND); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public boolean checkFromOrRootCommitInBranch(Commit commit) { |
| 66 | + return branchRepository.existsByRootCommitIdOrFromCommitId(commit.getId()); |
| 67 | + } |
| 68 | + |
| 69 | + public void checkDuplicatedWithBranchName(Long docId, String branchName) { |
| 70 | + boolean isDuplicate = branchRepository.existsByDocIdAndName(docId, branchName); |
| 71 | + |
| 72 | + if (isDuplicate) { |
| 73 | + throw new CustomException(BranchErrorCode.BRANCH_NAME_DUPLICATED); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments