Небольшой рефакторинг репозиториев для MR

This commit is contained in:
Struchkov Mark 2022-12-21 20:55:07 +03:00
parent 4877a45c67
commit 295da29cd6
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
4 changed files with 12 additions and 19 deletions

View File

@ -22,8 +22,6 @@ public interface MergeRequestRepository {
List<MergeRequest> findAllById(Set<Long> mergeRequestIds); List<MergeRequest> findAllById(Set<Long> mergeRequestIds);
void deleteByIds(Set<Long> mergeRequestIds);
List<MergeRequest> findAllByReviewerId(Long personId); List<MergeRequest> findAllByReviewerId(Long personId);
void deleteByStates(Set<MergeRequestState> states); void deleteByStates(Set<MergeRequestState> states);

View File

@ -10,6 +10,7 @@ import dev.struchkov.bot.gitlab.data.jpa.MergeRequestJpaRepository;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -23,41 +24,43 @@ public class MergeRequestRepositoryImpl implements MergeRequestRepository {
private final MergeRequestForDiscussionJpaRepository forDiscussionJpaRepository; private final MergeRequestForDiscussionJpaRepository forDiscussionJpaRepository;
@Override @Override
@Transactional(readOnly = true)
public Set<IdAndStatusPr> findAllIdByStateIn(@NonNull Set<MergeRequestState> statuses) { public Set<IdAndStatusPr> findAllIdByStateIn(@NonNull Set<MergeRequestState> statuses) {
return jpaRepository.findAllIdByStateIn(statuses); return jpaRepository.findAllIdByStateIn(statuses);
} }
@Override @Override
@Transactional
public MergeRequest save(MergeRequest mergeRequest) { public MergeRequest save(MergeRequest mergeRequest) {
return jpaRepository.save(mergeRequest); return jpaRepository.save(mergeRequest);
} }
@Override @Override
@Transactional(readOnly = true)
public Optional<MergeRequest> findById(Long mergeRequestId) { public Optional<MergeRequest> findById(Long mergeRequestId) {
return jpaRepository.findById(mergeRequestId); return jpaRepository.findById(mergeRequestId);
} }
@Override @Override
@Transactional(readOnly = true)
public List<MergeRequestForDiscussion> findAllForDiscussion() { public List<MergeRequestForDiscussion> findAllForDiscussion() {
return forDiscussionJpaRepository.findAll(); return forDiscussionJpaRepository.findAll();
} }
@Override @Override
@Transactional(readOnly = true)
public List<MergeRequest> findAllById(Set<Long> mergeRequestIds) { public List<MergeRequest> findAllById(Set<Long> mergeRequestIds) {
return jpaRepository.findAllById(mergeRequestIds); return jpaRepository.findAllById(mergeRequestIds);
} }
@Override @Override
public void deleteByIds(Set<Long> mergeRequestIds) { @Transactional(readOnly = true)
jpaRepository.deleteAllByIdIn(mergeRequestIds);
}
@Override
public List<MergeRequest> findAllByReviewerId(Long personId) { public List<MergeRequest> findAllByReviewerId(Long personId) {
return jpaRepository.findAllByReviewersIn(personId); return jpaRepository.findAllByReviewersIn(personId);
} }
@Override @Override
@Transactional
public void deleteByStates(Set<MergeRequestState> states) { public void deleteByStates(Set<MergeRequestState> states) {
jpaRepository.deleteAllByStateIn(states); jpaRepository.deleteAllByStateIn(states);
} }

View File

@ -7,9 +7,7 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation; import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.Set; import java.util.Set;
/** /**
@ -18,19 +16,9 @@ import java.util.Set;
public interface MergeRequestJpaRepository extends JpaRepositoryImplementation<MergeRequest, Long> { public interface MergeRequestJpaRepository extends JpaRepositoryImplementation<MergeRequest, Long> {
void deleteAllByIdIn(Collection<Long> id);
@Query("SELECT new dev.struchkov.bot.gitlab.context.domain.IdAndStatusPr(p.id, p.twoId, p.projectId, p.state) FROM MergeRequest p WHERE p.state IN :states") @Query("SELECT new dev.struchkov.bot.gitlab.context.domain.IdAndStatusPr(p.id, p.twoId, p.projectId, p.state) FROM MergeRequest p WHERE p.state IN :states")
Set<IdAndStatusPr> findAllIdByStateIn(@Param("states") Set<MergeRequestState> states); Set<IdAndStatusPr> findAllIdByStateIn(@Param("states") Set<MergeRequestState> states);
@Query("SELECT p.id FROM MergeRequest p")
Set<Long> findAllIds();
@Query("SELECT p.author.id FROM MergeRequest p WHERE p.id = :id")
Optional<String> findAuthorById(@Param("id") Long id);
List<MergeRequest> findAllByAssigneeId(Long userId);
@Query("SELECT mr FROM MergeRequest mr LEFT JOIN mr.reviewers r WHERE r.id = :reviewerId") @Query("SELECT mr FROM MergeRequest mr LEFT JOIN mr.reviewers r WHERE r.id = :reviewerId")
List<MergeRequest> findAllByReviewersIn(@Param("reviewerId") Long reviewerId); List<MergeRequest> findAllByReviewersIn(@Param("reviewerId") Long reviewerId);

View File

@ -56,6 +56,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>