Если выборка меньше 10 фильмов, то пагинатор не показывается
This commit is contained in:
@@ -8,5 +8,6 @@ import java.util.List;
|
||||
|
||||
public interface BaseRepository {
|
||||
List<CinemaDocument> findByTitle(String title, int page);
|
||||
long countByTitle(String title);
|
||||
void save(CinemaDocument cinemaDocument);
|
||||
}
|
||||
|
||||
@@ -26,13 +26,22 @@ public class MongoDBRepository implements BaseRepository {
|
||||
Query query = new Query();
|
||||
final String regex = String.format(".*%s.*", Pattern.quote(title));
|
||||
query.addCriteria(Criteria.where("title").regex(regex, "i"));
|
||||
final int limitPage = 10;
|
||||
final int limitPage = 10; //TODO лимиты надо вывести в общую константу
|
||||
query.skip(limitPage * (page-1));
|
||||
query.limit(limitPage);
|
||||
logger.debug("query: {}", query.toString());
|
||||
return mongoOperations.find(query, CinemaDocument.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long countByTitle(String title) {
|
||||
Query query = new Query();
|
||||
final String regex = String.format(".*%s.*", Pattern.quote(title));
|
||||
query.addCriteria(Criteria.where("title").regex(regex, "i"));
|
||||
logger.debug("query: {}", query.toString());
|
||||
return mongoOperations.count(query, CinemaDocument.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(CinemaDocument cinemaDocument) {
|
||||
mongoOperations.save(cinemaDocument);
|
||||
|
||||
@@ -49,10 +49,15 @@ public class WebAppController {
|
||||
if (searchText.trim().isEmpty()) {
|
||||
return "redirect:/";
|
||||
}
|
||||
List<CinemaDocument> cinemaDocuments = baseRepository.findByTitle(searchText.trim(), page);
|
||||
searchText = searchText.trim();
|
||||
long count = baseRepository.countByTitle(searchText);
|
||||
List<CinemaDocument> cinemaDocuments = baseRepository.findByTitle(searchText, page);
|
||||
model.addAttribute("cindocs", cinemaDocuments);
|
||||
model.addAttribute("searchText", searchText);
|
||||
model.addAttribute("page", page);
|
||||
final int limitPage = 10; //TODO лимиты надо вывести в общую константу
|
||||
if (count > 10) {
|
||||
model.addAttribute("page", page);
|
||||
}
|
||||
return "searchResult";
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
[#include "search.comp.ftl"]
|
||||
<hr>
|
||||
|
||||
<span class="searchResult">
|
||||
<div class="searchResult">
|
||||
[#if cindocs?has_content]
|
||||
[#list cindocs as cinema]
|
||||
<div class="panel panel-default">
|
||||
@@ -24,10 +24,12 @@
|
||||
</div>
|
||||
</div>
|
||||
[/#list]
|
||||
[#if page??]
|
||||
<ul class="pager">
|
||||
<li class="previous"><a href="?search=${searchText}&page=${page-1}"><span aria-hidden="true">←</span> Назад</a></li>
|
||||
<li class="next"><a href="?search=${searchText}&page=${page+1}">Вперед <span aria-hidden="true">→</span></a></li>
|
||||
</ul>
|
||||
[/#if]
|
||||
[#else]
|
||||
<div style="text-align: center; font-size: 1.5em">
|
||||
Ничего не найдено
|
||||
|
||||
Reference in New Issue
Block a user