Реализовала поиск на странице всех заведений.

Добавила стили
parent 677a4873
...@@ -24,6 +24,7 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes; ...@@ -24,6 +24,7 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid; import javax.validation.Valid;
import java.security.Principal; import java.security.Principal;
import java.util.stream.Collectors;
@Controller @Controller
@RequiredArgsConstructor @RequiredArgsConstructor
...@@ -52,6 +53,16 @@ public class PlaceController { ...@@ -52,6 +53,16 @@ public class PlaceController {
return "place/places"; return "place/places";
} }
@RequestMapping(path = "/", params = "search", method = RequestMethod.GET)
public String all(@RequestParam String search, Pageable pageable, HttpServletRequest uriBuilder,
Model model){
Page<Place> places = service.filter(pageable, search);
model.addAttribute("places", places);
String uri = uriBuilder.getRequestURI();
PageableView.constructPageable(places, propertiesService.getDefaultPageSize(), model, uri);
return "place/places";
}
@PreAuthorize("hasAuthority('USER') || hasAuthority('ADMIN')") @PreAuthorize("hasAuthority('USER') || hasAuthority('ADMIN')")
@GetMapping("/add") @GetMapping("/add")
public String add(){ public String add(){
...@@ -121,9 +132,9 @@ public class PlaceController { ...@@ -121,9 +132,9 @@ public class PlaceController {
} }
@PreAuthorize("hasAuthority('ADMIN')") @PreAuthorize("hasAuthority('ADMIN')")
@PostMapping("/{id}/review/{reviewId}") @PostMapping("/{id}/photo/{photoId}")
public String deletePhoto(@PathVariable Integer id, @PathVariable Integer reviewId){ public String deletePhoto(@PathVariable Integer id, @PathVariable Integer photoId){
placePhotoService.delete(reviewId); placePhotoService.delete(photoId);
return "redirect:/" + id; return "redirect:/" + id;
} }
......
...@@ -10,4 +10,5 @@ import org.springframework.stereotype.Repository; ...@@ -10,4 +10,5 @@ import org.springframework.stereotype.Repository;
public interface PlaceRepository extends JpaRepository<Place, Integer> { public interface PlaceRepository extends JpaRepository<Place, Integer> {
boolean existsByName(String name); boolean existsByName(String name);
Page<Place> findAll(Pageable pageable); Page<Place> findAll(Pageable pageable);
Page<Place> findAllByNameContains(Pageable pageable, String name);
} }
...@@ -23,11 +23,14 @@ public class PlaceService { ...@@ -23,11 +23,14 @@ public class PlaceService {
return new ResourceNotFoundException("Завдеение", id); return new ResourceNotFoundException("Завдеение", id);
}); });
} }
public Page<Place> all(Pageable pageable){ public Page<Place> all(Pageable pageable){
return repository.findAll(pageable); return repository.findAll(pageable);
} }
public Page<Place> filter(Pageable pageable, String name){
return repository.findAllByNameContains(pageable, name);
}
public boolean isUnique(String name){ public boolean isUnique(String name){
return repository.existsByName(name); return repository.existsByName(name);
} }
......
...@@ -2,3 +2,4 @@ spring.datasource.url= jdbc:mysql://localhost:3306/final_exam ...@@ -2,3 +2,4 @@ spring.datasource.url= jdbc:mysql://localhost:3306/final_exam
spring.datasource.username=root spring.datasource.username=root
spring.datasource.password=12345 spring.datasource.password=12345
server.port=8002 server.port=8002
spring.data.web.pageable.default-page-size=3
...@@ -63,6 +63,8 @@ label{ ...@@ -63,6 +63,8 @@ label{
display: flex; display: flex;
flex-direction: column; flex-direction: column;
max-width: 300px; max-width: 300px;
border: 1px grey solid;
margin: 7px;
} }
.item img{ .item img{
width: 70%; width: 70%;
......
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
<@main.renderWith title="Заведения"> <@main.renderWith title="Заведения">
<h1>Заведения</h1> <h1>Заведения</h1>
<form action="/">
<input type="text" name="search" placeholder="поиск">
<button type="submit">Искать</button>
</form>
<div class="items"> <div class="items">
<#if places?? && places?size != 0> <#if places?? && places?size != 0>
<#list places as c> <#list places as c>
...@@ -10,15 +14,17 @@ ...@@ -10,15 +14,17 @@
<a href="/${c.id}"> <a href="/${c.id}">
<img src="/file/${c.placePhoto.filePath}" alt="Изображение"> <img src="/file/${c.placePhoto.filePath}" alt="Изображение">
<p class="info">Наименование ${c.name}</p> <p class="info">Наименование ${c.name}</p>
</a>
<p class="info">Создал ${c.user.login}</p> <p class="info">Создал ${c.user.login}</p>
<p class="info">Рейтинг ${c.rating}</p> <p class="info">Рейтинг ${c.rating}</p>
<p class="info">Отзывов ${c.reviews?size}</p> <p class="info">Отзывов ${c.reviews?size}</p>
<p class="info">Фото ${c.photos?size}</p> <p class="info">Фото ${c.photos?size}</p>
</a>
</div> </div>
</#if> </#if>
</#list> </#list>
<#include "../pagination.ftlh"> </div>
<#include "../pagination.ftlh">
<div>
<#else> <#else>
<p>Заведений нет</p> <p>Заведений нет</p>
<a href="/add">Добавить заведение</a> <a href="/add">Добавить заведение</a>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment