сдано

This commit is contained in:
ker73rus 2023-11-17 13:56:52 +04:00
parent 40b56f7a59
commit 60e6468423
9 changed files with 138 additions and 4 deletions

Binary file not shown.

View File

@ -1,6 +1,16 @@
package com.example.demo.author.repository; package com.example.demo.author.repository;
import com.example.demo.author.model.Author; import com.example.demo.author.model.Author;
import com.example.demo.film.model.Film;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
public interface AuthorRepository extends JpaRepository<Author, Long> { public interface AuthorRepository extends JpaRepository<Author, Long> {
@Query("SELECT a.films FROM Author a WHERE a.id = :id")
List<Film> getFilmFromAuthor(Long id );
@Query("SELECT COUNT(g) FROM Author a join a.films f JOIN f.genres g WHERE a.id = :id")
Long getCountGenreOfFilmFromAuthor(Long id);
} }

View File

@ -2,12 +2,15 @@ package com.example.demo.author.service;
import com.example.demo.author.model.Author; import com.example.demo.author.model.Author;
import com.example.demo.author.repository.AuthorRepository; import com.example.demo.author.repository.AuthorRepository;
import com.example.demo.film.model.Film;
import com.example.demo.genre.model.Genre;
import com.example.demo.util.validation.ValidatorUtil; import com.example.demo.util.validation.ValidatorUtil;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -57,6 +60,14 @@ public class AuthorService {
return currentAuthor; return currentAuthor;
} }
public Long getCountGenreOfFilmFromAuthor(Long id) {
return authorRepository.getCountGenreOfFilmFromAuthor(id);
}
public List<Film> getFilmsFromAuthor(Long id){
return authorRepository.getFilmFromAuthor(id);
}
@Transactional @Transactional
public void deleteAllAuthors() { public void deleteAllAuthors() {

View File

@ -31,6 +31,9 @@ public class FilmMvcController {
public String requestURI(final HttpServletRequest request) { public String requestURI(final HttpServletRequest request) {
return request.getRequestURI(); return request.getRequestURI();
} }
@GetMapping @GetMapping
public String getFilms(@RequestParam(value = "authorId", required = false) Long authorId, public String getFilms(@RequestParam(value = "authorId", required = false) Long authorId,
@RequestParam(value = "genreId",required = false) Long genreId, @RequestParam(value = "genreId",required = false) Long genreId,
@ -66,10 +69,7 @@ public class FilmMvcController {
model.addAttribute("errors", bindingResult.getAllErrors()); model.addAttribute("errors", bindingResult.getAllErrors());
return "film-edit"; return "film-edit";
} }
List<Genre> genres = new ArrayList<>(); List<Genre> genres = genreService.findGenresByListId(film.getGenresId());
for (Long obj: film.getGenresId()) {
genres.add(genreService.findGenre(obj));
}
if (id == null || id <= 0) { if (id == null || id <= 0) {
filmService.addFilm(film.getName(), film.getDescription(), authorService.findAuthor(film.getAuthorId()),genres); filmService.addFilm(film.getName(), film.getDescription(), authorService.findAuthor(film.getAuthorId()),genres);
} }

View File

@ -50,6 +50,12 @@ public class Film {
public List<Genre> getGenres() { public List<Genre> getGenres() {
return genres; return genres;
} }
public String getNameGenres() {
StringBuilder result = new StringBuilder();
for(Genre g : genres)
result.append(g.getName()).append(", ");
return result.toString();
}
public void addGenre(Genre genre) { public void addGenre(Genre genre) {
if (genres == null){ if (genres == null){

View File

@ -7,6 +7,7 @@ import com.example.demo.util.validation.ValidatorUtil;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -32,6 +33,14 @@ public class GenreService {
return genre.orElseThrow(() -> new GenreNotFoundException(id)); return genre.orElseThrow(() -> new GenreNotFoundException(id));
} }
@Transactional
public List<Genre> findGenresByListId(List<Long> ll){
List<Genre> genres = new ArrayList<>();
for (Long l: ll){
genreRepository.findById(l).ifPresent(genres::add);
}
return genres;
}
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<Genre> findAllGenres() { public List<Genre> findAllGenres() {
return genreRepository.findAll(); return genreRepository.findAll();

View File

@ -1,10 +1,19 @@
package com.example.demo.util; package com.example.demo.util;
import com.example.demo.author.controller.AuthorDto;
import com.example.demo.author.service.AuthorService;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@Controller @Controller
public class StaticPagesMvcController { public class StaticPagesMvcController {
private final AuthorService authorService;
public StaticPagesMvcController(AuthorService authorService) {
this.authorService = authorService;
}
@RequestMapping("/") @RequestMapping("/")
public String indexPage(){ public String indexPage(){
return "index"; return "index";
@ -17,4 +26,20 @@ public class StaticPagesMvcController {
public String loginPage(){ public String loginPage(){
return "login"; return "login";
} }
// @RequestMapping("/request")
// public String requestPage(){
// return "request";
// }
@RequestMapping(value = {"/request", "/request/{id}"})
public String editProduct(@PathVariable(required = false) Long id,
@ModelAttribute AuthorDto componentDTO,
Model model) {
if (componentDTO != null && componentDTO.getId() != 0){
model.addAttribute("count", authorService.getCountGenreOfFilmFromAuthor(componentDTO.getId()));
model.addAttribute("filmList", authorService.getFilmsFromAuthor(componentDTO.getId()));
}
model.addAttribute("componentDto", new AuthorDto());
model.addAttribute("components", authorService.findAllAuthors());
return "request";
}
} }

View File

@ -59,6 +59,9 @@
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/forum">Форум</a> <a class="nav-link" href="/forum">Форум</a>
</li> </li>
<li class="nav-item">
<a class="nav-link" href="/request">Запрос</a>
</li>
</ul> </ul>
</div> </div>
<form class="w-100"> <form class="w-100">

View File

@ -0,0 +1,70 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:th="http://www.thymeleaf.org"
layout:decorate="~{default}">
<head>
</head>
<body>
<div layout:fragment="content">
<div class="flex-shrink-0" style="background-color: RGB(255, 255, 255)">
<div>
<form
action="#"
th:action="@{/request}"
th:object="${componentDto}"
method="post"
>
<div class="mb-3">
<label for="lang" class="form-label">Автор</label>
<select id="lang" class="form-select" th:field="${componentDto.id}">
<option
th:each="value: ${components}"
th:value="${value.getId()}"
th:selected="true"
th:text="${value.getSurname()}"
></option>
</select>
</div>
<div>
<div>
<button class="btn btn-success button-fixed" type="submit">
получить
</button>
</div>
</div>
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">ID</th>
<th scope="col">Название</th>
<th scope="col">Жанры</th>
</tr>
</thead>
<tbody>
<tr th:each="film, iterator: ${filmList}">
<th scope="row" th:text="${iterator.index} + 1"></th>
<td th:text="${film.id}"></td>
<td th:text="${film.name}" style="width: 60%"></td>
<td th:text="${film.getNameGenres()}" style="width: 60%"></td>
<td style="width: 10%">
</td>
</tr>
<tr>
<th scope="col"></th>
<th scope="col"></th>
<th scope="col" style="text-align: end">Итого: </th>
<th th:if="${count != null}" th:text="${count}"></th>
</tr>
</tbody>
</table>
</form>
<br/>
</div>
</div>
</div>
</body>
</html>