diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/PullRequestsServiceImpl.java b/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/PullRequestsServiceImpl.java index 93b92c2..e5ba8f1 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/PullRequestsServiceImpl.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/PullRequestsServiceImpl.java @@ -3,7 +3,9 @@ package org.sadtech.bot.bitbucketbot.service.impl; import lombok.NonNull; import org.sadtech.basic.context.page.Pagination; import org.sadtech.basic.context.page.Sheet; +import org.sadtech.basic.context.service.simple.FilterService; import org.sadtech.basic.core.service.AbstractBusinessLogicService; +import org.sadtech.basic.filter.criteria.CriteriaFilter; import org.sadtech.basic.filter.criteria.CriteriaQuery; import org.sadtech.bot.bitbucketbot.domain.IdAndStatusPr; import org.sadtech.bot.bitbucketbot.domain.PullRequestStatus; @@ -17,6 +19,7 @@ import org.sadtech.bot.bitbucketbot.repository.PullRequestsRepository; import org.sadtech.bot.bitbucketbot.service.ChangeService; import org.sadtech.bot.bitbucketbot.service.PullRequestsService; import org.sadtech.bot.bitbucketbot.utils.ChangeGenerator; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import java.util.List; @@ -28,11 +31,17 @@ public class PullRequestsServiceImpl extends AbstractBusinessLogicService filterService; - protected PullRequestsServiceImpl(PullRequestsRepository pullRequestsRepository, ChangeService changeService) { + protected PullRequestsServiceImpl( + PullRequestsRepository pullRequestsRepository, + ChangeService changeService, + @Qualifier("pullRequestFilterService") FilterService pullRequestsFilterService + ) { super(pullRequestsRepository); this.changeService = changeService; this.pullRequestsRepository = pullRequestsRepository; + this.filterService = pullRequestsFilterService; } @Override @@ -67,14 +76,6 @@ public class PullRequestsServiceImpl extends AbstractBusinessLogicService new UpdateException("ПР с таким id не существует")); - } - @NonNull @Override public List getAllByReviewerAndStatuses(String login, ReviewerStatus reviewerStatus, Set statuses) { @@ -92,27 +93,33 @@ public class PullRequestsServiceImpl extends AbstractBusinessLogicService getAllByFilter(@NonNull PullRequestFilter filter, Pagination pagination) { - return null; + public Sheet getAll(@NonNull PullRequestFilter filter, Pagination pagination) { + return filterService.getAll(filter, pagination); } @Override - public Sheet getALlByFilterQuery(@NonNull PullRequestFilter filter, Pagination pagination) { - return null; + public List getAll(@NonNull PullRequestFilter filter) { + return filterService.getAll(filter); } @Override - public Optional getByFilterQuery(@NonNull PullRequestFilter filterQuery) { - return Optional.empty(); + public Optional getFirst(@NonNull PullRequestFilter filter) { + return filterService.getFirst(filter); } @Override - public boolean existsByFilterQuery(@NonNull PullRequestFilter filter) { - return pullRequestsRepository.existsByFilterQuery( - CriteriaQuery.create() - .matchPhrase(PullRequest_.BITBUCKET_ID, filter.getBitbucketId()) - .matchPhrase(PullRequest_.REPOSITORY_ID, filter.getBitbucketRepositoryId()) - ); + public boolean exists(@NonNull PullRequestFilter filter) { + return filterService.exists(filter); + } + + private PullRequest findAndFillId(@NonNull PullRequest pullRequest) { + return pullRequestsRepository.findFirst( + CriteriaFilter.create().and( + CriteriaQuery.create() + .matchPhrase(PullRequest_.BITBUCKET_ID, pullRequest.getBitbucketId()) + .matchPhrase(PullRequest_.REPOSITORY_ID, pullRequest.getRepositoryId()) + ) + ).orElseThrow(() -> new UpdateException("ПР с таким id не существует")); } } diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/filter/PullRequestFilterService.java b/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/filter/PullRequestFilterService.java new file mode 100644 index 0000000..90f0fcf --- /dev/null +++ b/src/main/java/org/sadtech/bot/bitbucketbot/service/impl/filter/PullRequestFilterService.java @@ -0,0 +1,60 @@ +package org.sadtech.bot.bitbucketbot.service.impl.filter; + +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import org.sadtech.basic.context.page.Pagination; +import org.sadtech.basic.context.page.Sheet; +import org.sadtech.basic.context.service.simple.FilterService; +import org.sadtech.basic.filter.Filter; +import org.sadtech.basic.filter.FilterQuery; +import org.sadtech.basic.filter.criteria.CriteriaFilter; +import org.sadtech.basic.filter.criteria.CriteriaQuery; +import org.sadtech.bot.bitbucketbot.domain.entity.PullRequest; +import org.sadtech.bot.bitbucketbot.domain.entity.PullRequest_; +import org.sadtech.bot.bitbucketbot.domain.filter.PullRequestFilter; +import org.sadtech.bot.bitbucketbot.repository.PullRequestsRepository; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +@Service +@RequiredArgsConstructor +public class PullRequestFilterService implements FilterService { + + private final PullRequestsRepository pullRequestsRepository; + + @Override + public Sheet getAll(@NonNull PullRequestFilter filter, Pagination pagination) { + return pullRequestsRepository.findAll(createFilter(filter), pagination); + } + + @Override + public List getAll(@NonNull PullRequestFilter filter) { + return pullRequestsRepository.findAll(createFilter(filter)); + } + + @Override + public Optional getFirst(@NonNull PullRequestFilter filter) { + return pullRequestsRepository.findFirst(createFilter(filter)); + } + + @Override + public boolean exists(@NonNull PullRequestFilter filter) { + return pullRequestsRepository.exists(createFilter(filter)); + } + + private FilterQuery convertFilter(@NonNull PullRequestFilter filter) { + return CriteriaQuery.create() + .matchPhrase(PullRequest_.BITBUCKET_ID, filter.getBitbucketId()) + .matchPhrase(PullRequest_.REPOSITORY_ID, filter.getBitbucketRepositoryId()); + } + + private Filter createFilter(PullRequestFilter filter) { + return CriteriaFilter.create() + .and( + convertFilter(filter) + ); + } + +} diff --git a/src/main/java/org/sadtech/bot/bitbucketbot/service/parser/PullRequestParser.java b/src/main/java/org/sadtech/bot/bitbucketbot/service/parser/PullRequestParser.java index aab7825..24341c4 100644 --- a/src/main/java/org/sadtech/bot/bitbucketbot/service/parser/PullRequestParser.java +++ b/src/main/java/org/sadtech/bot/bitbucketbot/service/parser/PullRequestParser.java @@ -55,7 +55,7 @@ public class PullRequestParser { log.info("Закрыты: " + Arrays.toString(closeId.toArray())); log.info("Не найдены: " + Arrays.toString(newNotExistsId.toArray())); if (!newNotExistsId.isEmpty()) { - pullRequestsService.deleteAll(newNotExistsId); + pullRequestsService.deleteAllById(newNotExistsId); } } @@ -86,9 +86,9 @@ public class PullRequestParser { private List getExistsPr(@NonNull List pullRequestJsons) { return pullRequestJsons.stream() - .filter(json -> pullRequestsService.existsByFilterQuery(bitbucketIdAndPullRequestId(json))) - .map(pullRequestJson -> conversionService.convert(pullRequestJson, PullRequest.class)) - .peek(pullRequest -> pullRequestsService.getIdByBitbucketIdAndReposId(pullRequest.getBitbucketId(), pullRequest.getRepositoryId()).ifPresent(pullRequest::setId)) + .map(json -> pullRequestsService.getByFilterQuery(bitbucketIdAndPullRequestId(json))) + .filter(Optional::isPresent) + .map(Optional::get) .collect(Collectors.toList()); }