From ed8c6edfe2cffbfd665dce42b92053769263aa93 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Wed, 24 Mar 2021 15:22:53 +0400 Subject: [PATCH 1/2] #10 - add table --- build.gradle | 1 + .../extractor/mvc/GitExtractorController.java | 58 ++++++++++++++++--- .../extractor/service/CommitService.java | 40 +++++++++++++ src/main/resources/templates/filtering.html | 36 +++++++----- src/main/resources/templates/newRepo.html | 2 - 5 files changed, 111 insertions(+), 26 deletions(-) create mode 100644 src/main/java/ru/ulstu/extractor/service/CommitService.java diff --git a/build.gradle b/build.gradle index 4cd6c5e..0d2a506 100644 --- a/build.gradle +++ b/build.gradle @@ -48,6 +48,7 @@ dependencies { compile group: 'org.springframework.boot', name: 'spring-boot-starter-jetty' compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf' compile group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect' + compile group: 'org.springframework.data', name: 'spring-data-commons' compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-afterburner' compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5' compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc' diff --git a/src/main/java/ru/ulstu/extractor/mvc/GitExtractorController.java b/src/main/java/ru/ulstu/extractor/mvc/GitExtractorController.java index 676e7f2..77b4c1f 100644 --- a/src/main/java/ru/ulstu/extractor/mvc/GitExtractorController.java +++ b/src/main/java/ru/ulstu/extractor/mvc/GitExtractorController.java @@ -1,5 +1,8 @@ package ru.ulstu.extractor.mvc; +import org.eclipse.jgit.api.errors.GitAPIException; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -7,23 +10,34 @@ import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; import ru.ulstu.extractor.model.Branch; +import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.mvc.model.FilterForm; import ru.ulstu.extractor.mvc.model.RepoForm; +import ru.ulstu.extractor.service.CommitService; import ru.ulstu.extractor.service.FilteringService; import ru.ulstu.extractor.service.GitRepositoryService; +import java.io.IOException; import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.IntStream; @Controller public class GitExtractorController { private final FilteringService filteringService; private final GitRepositoryService gitRepositoryService; + private final CommitService commitService; public GitExtractorController(FilteringService filteringService, - GitRepositoryService gitRepositoryService) { + GitRepositoryService gitRepositoryService, + CommitService commitService) { this.filteringService = filteringService; this.gitRepositoryService = gitRepositoryService; + this.commitService = commitService; } @GetMapping("/newRepo") @@ -32,14 +46,16 @@ public class GitExtractorController { return "newRepo"; } - @GetMapping("/filtering") - public String filter(Model model) { - model.addAttribute("filterForm", new FilterForm()); - return "filtering"; - } +// @GetMapping("/filtering") +// public String filter(Model model) { +// model.addAttribute("filterForm", new FilterForm()); +// return "filtering"; +// } @PostMapping("/sendFilter") - public String sendFilter(@ModelAttribute FilterForm filterForm, Model model) { + public String sendFilter(@ModelAttribute FilterForm filterForm, Model model) throws GitAPIException, IOException { + List list = gitRepositoryService.getCommits(filterForm.getUrl()); + model.addAttribute("commits", list); if (filterForm.getFilter() == null || filterForm.getFilter().isEmpty()) { model.addAttribute("error", "'Строка' не должно быть пустым"); return "filtering"; @@ -61,12 +77,36 @@ public class GitExtractorController { } @RequestMapping(value = "/newRepo", method = RequestMethod.POST, params = "next") - public String setBranch(@ModelAttribute RepoForm repoForm, Model model) { + public String setBranch(@ModelAttribute RepoForm repoForm, Model model, RedirectAttributes redirectAttributes) { model.addAttribute("filterForm", new FilterForm(repoForm.getRepo())); if (repoForm.getBranch() == null) { return "newRepo"; } else { - return "filtering"; + redirectAttributes.addAttribute("url", repoForm.getRepo()); + redirectAttributes.addAttribute("branch", repoForm.getBranch()); + return "redirect:/filtering"; } } + + @RequestMapping(value = "/filtering", method = RequestMethod.GET) + public String listCommits( + Model model, + @ModelAttribute FilterForm filterForm, + @RequestParam("page") Optional page, + @RequestParam("size") Optional size, + @RequestParam String url, + @RequestParam String branch) throws GitAPIException, IOException { + int currentPage = page.orElse(1); + int pageSize = size.orElse(5); + Page commitPage = commitService.findPaginated(PageRequest.of(currentPage - 1, pageSize), url); + model.addAttribute("commitPage", commitPage); + int totalPages = commitPage.getTotalPages(); + if (totalPages > 0) { + List pageNumbers = IntStream.rangeClosed(1, totalPages) + .boxed() + .collect(Collectors.toList()); + model.addAttribute("pageNumbers", pageNumbers); + } + return "filtering"; + } } diff --git a/src/main/java/ru/ulstu/extractor/service/CommitService.java b/src/main/java/ru/ulstu/extractor/service/CommitService.java new file mode 100644 index 0000000..4f7b143 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/service/CommitService.java @@ -0,0 +1,40 @@ +package ru.ulstu.extractor.service; + +import org.eclipse.jgit.api.errors.GitAPIException; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; +import ru.ulstu.extractor.model.Commit; + +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +@Service +public class CommitService { + private final GitRepositoryService gitRepositoryService; + + public CommitService(GitRepositoryService gitRepositoryService) { + this.gitRepositoryService = gitRepositoryService; + } + + public Page findPaginated(Pageable pageable, String url) throws GitAPIException, IOException { + int pageSize = pageable.getPageSize(); + int currentPage = pageable.getPageNumber(); + int startItem = currentPage * pageSize; + List commits = gitRepositoryService.getCommits(url); + ; + + if (commits.size() < startItem) { + commits = Collections.emptyList(); + } else { + int toIndex = Math.min(startItem + pageSize, commits.size()); + commits = commits.subList(startItem, toIndex); + } + return new PageImpl<>(commits, PageRequest.of(currentPage, pageSize), commits.size()); + } +} + + diff --git a/src/main/resources/templates/filtering.html b/src/main/resources/templates/filtering.html index 0899636..88676d5 100644 --- a/src/main/resources/templates/filtering.html +++ b/src/main/resources/templates/filtering.html @@ -7,7 +7,6 @@
-

Фильтровать данные:

По автору @@ -30,23 +29,30 @@

- - - - - - - - - - - - + + + + + + + + + + + + + + + +
AuthorDateCommit

- - +
+ diff --git a/src/main/resources/templates/newRepo.html b/src/main/resources/templates/newRepo.html index 39d5457..5406ca6 100644 --- a/src/main/resources/templates/newRepo.html +++ b/src/main/resources/templates/newRepo.html @@ -7,7 +7,6 @@
-

Ваш git репозиторий:
@@ -28,7 +27,6 @@

-