ЛР5 есть связь фильмы-жанры и отчет по жанрам
This commit is contained in:
parent
03a0ed7f5d
commit
22ae31f5c6
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
@ -6,6 +6,7 @@ import org.springframework.validation.BindingResult;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import ru.ulstu.is.lab1.DataBase.service.FilmService;
|
import ru.ulstu.is.lab1.DataBase.service.FilmService;
|
||||||
import ru.ulstu.is.lab1.DataBase.service.GenreService;
|
import ru.ulstu.is.lab1.DataBase.service.GenreService;
|
||||||
|
import ru.ulstu.is.lab1.DataBase.model.Genre;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -32,6 +33,23 @@ public class FilmMvcController {
|
|||||||
return "film";
|
return "film";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/genres/{id}")
|
||||||
|
public String getGenresFromFilm(@PathVariable Long id, Model model) {
|
||||||
|
List<Genre> genres = filmService.getGenresFromFilm(id);
|
||||||
|
model.addAttribute("genres", genres);
|
||||||
|
return "view-genres";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("getGenres/{id}")
|
||||||
|
public String getGenres(@PathVariable Long id, Model model) {
|
||||||
|
model.addAttribute("genres",
|
||||||
|
genreService.findAllGenres().stream()
|
||||||
|
.map(GenreDTO::new)
|
||||||
|
.toList());
|
||||||
|
model.addAttribute("filmId", id);
|
||||||
|
return "add-genre";
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping(value = {"/edit", "/edit/{id}"})
|
@GetMapping(value = {"/edit", "/edit/{id}"})
|
||||||
public String editFilm(@PathVariable(required = false) Long id,
|
public String editFilm(@PathVariable(required = false) Long id,
|
||||||
Model model) {
|
Model model) {
|
||||||
@ -74,6 +92,6 @@ public class FilmMvcController {
|
|||||||
.map(Long::parseLong)
|
.map(Long::parseLong)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
filmService.addGenres(id, genreIdsAsLong);
|
filmService.addGenres(id, genreIdsAsLong);
|
||||||
return "redirect:/film";
|
return "redirect:../genres/{id}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,10 @@ import org.springframework.ui.Model;
|
|||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import ru.ulstu.is.lab1.DataBase.service.GenreService;
|
import ru.ulstu.is.lab1.DataBase.service.GenreService;
|
||||||
|
import ru.ulstu.is.lab1.DataBase.service.FilmService;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("/genre")
|
@RequestMapping("/genre")
|
||||||
@ -59,4 +61,13 @@ public class GenreMvcController {
|
|||||||
genreService.deleteGenre(id);
|
genreService.deleteGenre(id);
|
||||||
return "redirect:/genre";
|
return "redirect:/genre";
|
||||||
}
|
}
|
||||||
|
@GetMapping("/report/{id}")
|
||||||
|
public String findWorkersOnWorkplace(@PathVariable(required = false) Long id, Model model){
|
||||||
|
model.addAttribute("cathegory", "Фильмы по жанру: " + genreService.findGenre(id).getName());
|
||||||
|
model.addAttribute("films", genreService.findFilmOnGenre(id).stream()
|
||||||
|
.map(FilmDTO::new)
|
||||||
|
.toList());
|
||||||
|
model.addAttribute("page", "genre");
|
||||||
|
return "report";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,4 +81,15 @@ public class FilmService {
|
|||||||
}
|
}
|
||||||
return filmRepository.save(film);
|
return filmRepository.save(film);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<Genre> getGenresFromFilm(Long filmId){
|
||||||
|
Optional<Film> filmOptional = filmRepository.findById(filmId);
|
||||||
|
if (filmOptional.isPresent()) {
|
||||||
|
Film film = filmOptional.get();
|
||||||
|
return film.getGenres();
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Film not found with id: " + filmId);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
33
src/main/resources/templates/add-genre.html
Normal file
33
src/main/resources/templates/add-genre.html
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en"
|
||||||
|
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||||
|
layout:decorate="~{default}" xmlns:th="http://www.w3.org/1999/xhtml">
|
||||||
|
<head>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div th:text="${errors}" class="margin-bottom alert-danger"></div>
|
||||||
|
<div layout:fragment="content">
|
||||||
|
<form action="#" th:action="@{/film/add_genres/{id}(id=${filmId})}" method="post">
|
||||||
|
<div class="form-group">
|
||||||
|
<h2 class="text-danger mt-3">Выберите жанры:</h2>
|
||||||
|
<ul class="list-group mt-3">
|
||||||
|
<li class="list-group-item" th:each="genre, iterator: ${genres}">
|
||||||
|
<div class="form-check">
|
||||||
|
<input class="form-check-input" type="checkbox" name="genreId" th:value="${genre.id}">
|
||||||
|
<label class="form-check-label" th:text="${genre.name}"></label>
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<div class="mt-3">
|
||||||
|
<button type="submit" class="btn btn-success button-fixed">
|
||||||
|
<span>Добавить</span>
|
||||||
|
</button>
|
||||||
|
<a class="btn btn-secondary button-fixed" th:href="@{/film}">
|
||||||
|
Назад
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
13
src/main/resources/templates/error.html
Normal file
13
src/main/resources/templates/error.html
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en"
|
||||||
|
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||||
|
layout:decorate="~{default}" xmlns:th="http://www.w3.org/1999/xhtml">
|
||||||
|
<head>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div layout:fragment="content">
|
||||||
|
<div><span th:text="${error}"></span></div>
|
||||||
|
<a href="/">На главную</a>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -34,6 +34,12 @@
|
|||||||
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${film.id}').click()|">
|
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${film.id}').click()|">
|
||||||
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
|
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
|
||||||
</button>
|
</button>
|
||||||
|
<a class="btn btn-success button-fixed button-sm"
|
||||||
|
th:href="@{/film/genres/{id}(id=${film.id})}">
|
||||||
|
<i class="fa-solid fa-eye"></i>Посмотреть жанры</a>
|
||||||
|
<a class="btn btn-success button-fixed button-sm"
|
||||||
|
th:href="@{/film/getGenres/{id}(id=${film.id})}">
|
||||||
|
<i class="fa-solid fa-plus"></i>Добавить жанры</a>
|
||||||
</div>
|
</div>
|
||||||
<form th:action="@{/film/delete/{id}(id=${film.id})}" method="post">
|
<form th:action="@{/film/delete/{id}(id=${film.id})}" method="post">
|
||||||
<button th:id="'remove-' + ${film.id}" type="submit" style="display: none">
|
<button th:id="'remove-' + ${film.id}" type="submit" style="display: none">
|
||||||
|
@ -34,6 +34,10 @@
|
|||||||
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${genre.id}').click()|">
|
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${genre.id}').click()|">
|
||||||
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
|
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
|
||||||
</button>
|
</button>
|
||||||
|
<a class="btn btn-success button-fixed button-sm"
|
||||||
|
th:href="@{/genre/report/{id}(id=${genre.id})}">
|
||||||
|
<i class="fa fa-book" aria-hidden="true"></i> Отчет
|
||||||
|
</a>
|
||||||
</div>
|
</div>
|
||||||
<form th:action="@{/genre/delete/{id}(id=${genre.id})}" method="post">
|
<form th:action="@{/genre/delete/{id}(id=${genre.id})}" method="post">
|
||||||
<button th:id="'remove-' + ${genre.id}" type="submit" style="display: none">
|
<button th:id="'remove-' + ${genre.id}" type="submit" style="display: none">
|
||||||
|
32
src/main/resources/templates/report.html
Normal file
32
src/main/resources/templates/report.html
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en"
|
||||||
|
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||||
|
layout:decorate="~{default}" xmlns:th="http://www.w3.org/1999/xhtml">
|
||||||
|
<head>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h5 th:text="${cathegory}" class="d-flex justify-content-center mb-3"></h5>
|
||||||
|
<div th:text="${errors}" class="margin-bottom alert-danger"></div>
|
||||||
|
<div layout:fragment="content">
|
||||||
|
<div th:if="${id != null}" class="table-responsive">
|
||||||
|
<table class="table text-light">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th scope="col">#</th>
|
||||||
|
<th scope="col">Название</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr th:each="film, iterator: ${films}">
|
||||||
|
<th scope="row" th:text="${iterator.index} + 1"></th>
|
||||||
|
<td th:text="${film.name}"></td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<a class="btn btn-secondary" th:href="@{/} + ${page}">
|
||||||
|
Назад
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
28
src/main/resources/templates/view-genres.html
Normal file
28
src/main/resources/templates/view-genres.html
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en"
|
||||||
|
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||||
|
layout:decorate="~{default}" xmlns:th="http://www.w3.org/1999/xhtml">
|
||||||
|
<head>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div layout:fragment="content">
|
||||||
|
<div class="table-responsive">
|
||||||
|
<table class="table text-light">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th scope="col">Название</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr th:each="genre, iterator: ${genres}">
|
||||||
|
<td th:text="${genre.name}"></td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<a class="btn btn-secondary button-fixed" th:href="@{/film}">
|
||||||
|
Назад
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user