#10 - add table

This commit is contained in:
Anton Romanov 2021-03-24 15:22:53 +04:00
parent 4af08bc9b8
commit ed8c6edfe2
5 changed files with 111 additions and 26 deletions

View File

@ -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'

View File

@ -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<Commit> 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 {
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<Integer> page,
@RequestParam("size") Optional<Integer> size,
@RequestParam String url,
@RequestParam String branch) throws GitAPIException, IOException {
int currentPage = page.orElse(1);
int pageSize = size.orElse(5);
Page<Commit> commitPage = commitService.findPaginated(PageRequest.of(currentPage - 1, pageSize), url);
model.addAttribute("commitPage", commitPage);
int totalPages = commitPage.getTotalPages();
if (totalPages > 0) {
List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
.boxed()
.collect(Collectors.toList());
model.addAttribute("pageNumbers", pageNumbers);
}
return "filtering";
}
}
}

View File

@ -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<Commit> findPaginated(Pageable pageable, String url) throws GitAPIException, IOException {
int pageSize = pageable.getPageSize();
int currentPage = pageable.getPageNumber();
int startItem = currentPage * pageSize;
List<Commit> 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());
}
}

View File

@ -7,7 +7,6 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<div class="container" layout:fragment="content">
<body>
<form action="#" th:action="@{/sendFilter}" th:object="${filterForm}" method="post">
<p><b>Фильтровать данные:</b><Br></p>
По автору
@ -30,23 +29,30 @@
<input type="hidden" th:field="*{url}">
</p>
<p style="color:red" th:text="${error}"></p>
<!-- <table>-->
<!-- <tr>-->
<!-- <th>Author</th>-->
<!-- <th>Date</th>-->
<!-- <th>Commit</th>-->
<!-- </tr>-->
<!-- <tr th:each="commit : ${commits}">-->
<!-- <td th:text="${commit.author}">Onions</td>-->
<!-- <td th:text="${commit.date}">2.41</td>-->
<!-- <td th:text="${commit.message}">yes</td>-->
<!-- </tr>-->
<!-- </table>-->
<table border="1">
<thead>
<tr>
<th>Author</th>
<th>Date</th>
<th>Commit</th>
</tr>
</thead>
<tbody>
<tr th:each="commit: ${commits}">
<td th:text="${commit.author}"></td>
<td th:text="${commit.date}"></td>
<td th:text="${commit.message}"></td>
</tr>
</tbody>
</table>
<p>
<input type="submit" value="Отправить"/>
</p>
</form>
</body>
</div>
<div th:if="${commitPage.totalPages > 0}" class="pagination"
th:each="pageNumber : ${pageNumbers}">
<a th:href="@{/filtering.html(size=${commitPage.size}, page=${pageNumber})}"
th:class="${pageNumber==commitPage.number + 1} ? active"></a>
</div>
</html>

View File

@ -7,7 +7,6 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<div class="container" layout:fragment="content">
<body>
<form action="#" th:action="@{/newRepo}" th:object="${repoForm}" method="post">
<p style="color:red" th:text="${error}"></p>
<p><b>Ваш git репозиторий:</b><br>
@ -28,7 +27,6 @@
<input type="submit" name="next" value="Продолжить"/>
</p>
</form>
</body>
</div>
<script>
$('#select-branch').selectpicker('refresh');