#19 -- Fix filtering

This commit is contained in:
Anton Romanov 2021-04-09 14:57:52 +04:00
parent 3cdc712a2c
commit 1770de91c8
5 changed files with 58 additions and 63 deletions

View File

@ -8,7 +8,6 @@ package ru.ulstu.extractor.controller;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@ -34,21 +33,23 @@ public class GitFilteringController {
this.filteringService = filteringService;
}
@RequestMapping(value = FILTER_COMMITS, method = RequestMethod.GET)
@RequestMapping(value = FILTER_COMMITS, method = {RequestMethod.GET, RequestMethod.POST})
public String listCommits(
Model model,
@ModelAttribute FilterForm filterForm,
@RequestParam("page") Optional<Integer> page,
@RequestParam("size") Optional<Integer> size,
@RequestParam String repositoryUrl,
@RequestParam String branchName) {
@RequestParam Optional<Integer> page,
@RequestParam Optional<Integer> size,
@RequestParam Optional<String> repositoryUrl,
@RequestParam Optional<String> branchName,
@RequestParam Optional<String> author) {
int currentPage = page.orElse(1);
int pageSize = size.orElse(DEFAULT_PAGE_SIZE);
String author = "Anton Romanov";
Page<Commit> commitsPage = filteringService.getCommits(repositoryUrl,
branchName,
author,
String notEmptyRepositoryUrl = repositoryUrl.orElseThrow(() -> new RuntimeException("Url repository not present"));
String notEmptyBranchName = branchName.orElseThrow(() -> new RuntimeException("Branch name not present"));
Page<Commit> commitsPage = filteringService.getCommits(notEmptyRepositoryUrl,
notEmptyBranchName,
author.orElse(null),
new OffsetablePageRequest(currentPage - 1, pageSize));
int totalPages = commitsPage.getTotalPages();
if (totalPages > 0) {
@ -57,31 +58,16 @@ public class GitFilteringController {
.collect(Collectors.toList());
model.addAttribute("pageNumbers", pageNumbers);
}
FilterForm filterForm = new FilterForm();
filterForm.setCommitsPage(commitsPage);
filterForm.setBranch(branchName);
filterForm.setUrl(repositoryUrl);
filterForm.setBranchName(notEmptyBranchName);
filterForm.setRepositoryUrl(notEmptyRepositoryUrl);
filterForm.setAuthor(author.orElse(null));
model.addAttribute("filterForm", filterForm);
model.addAttribute("authors", filteringService.getRepositoryAuthors(repositoryUrl, branchName));
model.addAttribute("authors", filteringService.getRepositoryAuthors(
notEmptyRepositoryUrl,
notEmptyBranchName
));
return FILTER_COMMITS;
}
// @RequestMapping(value = FILTER_COMMITS, method = RequestMethod.POST)
// public ModelAndView listFilterCommits(
// Model model,
// @ModelAttribute FilterForm filterForm) {
// Page<Commit> commitsPage = filteringService.getCommits(filterForm.getUrl(),
// filterForm.getBranch(),
// new OffsetablePageRequest(0, DEFAULT_PAGE_SIZE));
// int totalPages = commitsPage.getTotalPages();
// if (totalPages > 0) {
// List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
// .boxed()
// .collect(Collectors.toList());
// model.addAttribute("pageNumbers", pageNumbers);
// }
// filterForm.setCommitsPage(commitsPage);
// model.addAttribute("filterForm", filterForm);
// model.addAttribute("authors", filteringService.getRepositoryAuthors(filterForm.getUrl(), filterForm.getBranch()));
// return new ModelAndView()FILTER_COMMITS + "?repositoryUrl="+filterForm.getUrl() + "&branchName=" +filterForm.getBranch() ;
// }
}

View File

@ -10,8 +10,8 @@ import ru.ulstu.extractor.model.Commit;
public class FilterForm {
private String filter;
private String url;
private String branch;
private String repositoryUrl;
private String branchName;
private String author;
private Page<Commit> commitsPage;
@ -27,7 +27,7 @@ public class FilterForm {
}
public FilterForm(String url) {
this.url = url;
this.repositoryUrl = url;
}
public String getFilter() {
@ -38,22 +38,6 @@ public class FilterForm {
this.filter = filter;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
public Page<Commit> getCommitsPage() {
return commitsPage;
}
@ -62,10 +46,30 @@ public class FilterForm {
this.commitsPage = commitsPage;
}
public String getRepositoryUrl() {
return repositoryUrl;
}
public void setRepositoryUrl(String repositoryUrl) {
this.repositoryUrl = repositoryUrl;
}
public String getBranchName() {
return branchName;
}
public void setBranchName(String branchName) {
this.branchName = branchName;
}
@Override
public String toString() {
return "FilterForm{" +
"subject='" + filter +
"filter='" + filter + '\'' +
", repositoryUrl='" + repositoryUrl + '\'' +
", branchName='" + branchName + '\'' +
", author='" + author + '\'' +
", commitsPage=" + commitsPage +
'}';
}
}

View File

@ -19,7 +19,7 @@ import ru.ulstu.extractor.model.Repository;
import java.util.List;
public interface CommitRepository extends JpaRepository<Commit, Integer> {
@Query("SELECT c FROM Commit c, Repository r, Branch b, Author a WHERE c.branch = b AND r = b.repository AND a = c.author AND r = :repository AND b.name = :branchName AND a.name = :author")
@Query("SELECT c FROM Commit c, Repository r, Branch b, Author a WHERE c.branch = b AND r = b.repository AND a = c.author AND r = :repository AND b.name = :branchName AND (:author IS NULL OR :author = '' OR a.name = :author)")
Page<Commit> findByRepositoryAndBranch(Pageable pageable, @Param("repository") Repository repository, @Param("branchName") String branchName, @Param("author") String author);
@Query("SELECT new ru.ulstu.extractor.model.CommitAuthorStatistic(c.author.name, COUNT(DISTINCT c.hash)) FROM Commit c GROUP by c.author.name")

View File

@ -40,7 +40,7 @@ public class FilteringService {
public Page<Commit> getCommits(@NotNull String repositoryUrl,
@NotNull String branchName,
@NotNull String author,
String author,
Pageable pageable) {
return commitRepository.findByRepositoryAndBranch(
pageable,

View File

@ -26,7 +26,7 @@
border-radius: 2px;
}
</style>
<form action="#" th:action="${@route.FILTER_COMMITS}" th:object="${filterForm}" method="post">
<form action="#" th:action="${@route.FILTER_COMMITS}" th:object="${filterForm}" method="get">
<div class="row">
<div class="col-md-1 col-sm-12">
Автор
@ -39,6 +39,11 @@
th:utext="${author}">
</option>
</select>
<script th:inline="javascript">
$('select[name=selValue]').val([[*{author}]]);
$('#select-author').selectpicker('refresh');
</script>
</div>
<div class="col-md-1 col-sm-12">
Дата:
@ -73,8 +78,8 @@
</div>
</div>
<input type="hidden" th:field="*{url}">
<input type="hidden" th:field="*{branch}">
<input type="hidden" th:field="*{repositoryUrl}">
<input type="hidden" th:field="*{branchName}">
<table class="table table-striped">
<thead class="thead-dark">
<tr>
@ -91,14 +96,14 @@
</tr>
</tbody>
</table>
<p>
</p>
</form>
Страницы:
<div th:if="${filterForm.commitsPage.totalPages > 0}" class="pagination"
th:each="pageNumber : ${pageNumbers}">
<a th:href="@{/filterCommits(size=${filterForm.commitsPage.size}, page=${pageNumber}, repositoryUrl=${filterForm.url}, branchName=${filterForm.branch})}"
<a th:href="@{/filterCommits(size=${filterForm.commitsPage.size}, page=${pageNumber},
repositoryUrl=${filterForm.repositoryUrl},
branchName=${filterForm.branchName},
author=${filterForm.author})}"
th:text=${pageNumber}
th:class="${pageNumber == filterForm.commitsPage.number} ? active"></a>
</div>