ЛР5 надо что-то решать с удалением связей

This commit is contained in:
ityurner02@mail.ru 2023-05-23 19:52:23 +04:00
parent 22ae31f5c6
commit 4246c98078
7 changed files with 98 additions and 13 deletions

Binary file not shown.

View File

@ -6,9 +6,10 @@ import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.lab1.DataBase.service.CollectionService;
import ru.ulstu.is.lab1.DataBase.service.FilmService;
import ru.ulstu.is.lab1.DataBase.service.GenreService;
import ru.ulstu.is.lab1.DataBase.model.Film;
import javax.validation.Valid;
import java.util.List;
@Controller
@RequestMapping("/collection")
@ -31,6 +32,23 @@ public class CollectionMvcController {
return "collection";
}
@GetMapping("/films/{id}")
public String getFilmsFromCollection(@PathVariable Long id, Model model) {
List<Film> films = collectionService.getFilmsFromCollection(id);
model.addAttribute("films", films);
return "view-films";
}
@GetMapping("getFilms/{id}")
public String getFilms(@PathVariable Long id, Model model) {
model.addAttribute("films",
filmService.findAllFilms().stream()
.map(FilmDTO::new)
.toList());
model.addAttribute("collectionId", id);
return "add-film";
}
@GetMapping(value = {"/edit", "/edit/{id}"})
public String editCollection(@PathVariable(required = false) Long id,
Model model) {
@ -66,17 +84,11 @@ public class CollectionMvcController {
return "redirect:/collection";
}
@PostMapping(value = "/add_film/{id}/{filmId}")
@PostMapping(value = "/add_film/{id}")
public String addFilm(@PathVariable(value = "id") Long id,
@PathVariable(value = "filmId") Long filmId) {
collectionService.addFilm(id, filmId);
return "redirect:/collection";
}
@PostMapping(value = "/del_film/{id}/{filmId}")
public String deleteFilm(@PathVariable(value = "id") Long id,
@PathVariable(value = "filmId") Long filmId) {
collectionService.deleteFilm(id, filmId);
return "redirect:/collection";
@RequestParam("filmId") String filmId) {
Long filmIdAsLong = Long.parseLong(filmId);
collectionService.addFilm(id, filmIdAsLong);
return "redirect:../films/{id}";
}
}

View File

@ -62,7 +62,7 @@ public class GenreMvcController {
return "redirect:/genre";
}
@GetMapping("/report/{id}")
public String findWorkersOnWorkplace(@PathVariable(required = false) Long id, Model model){
public String findFilmsOnGenre(@PathVariable(required = false) Long id, Model model){
model.addAttribute("cathegory", "Фильмы по жанру: " + genreService.findGenre(id).getName());
model.addAttribute("films", genreService.findFilmOnGenre(id).stream()
.map(FilmDTO::new)

View File

@ -97,4 +97,15 @@ public class CollectionService {
collection.removeFilm(film);
return collectionRepository.save(collection);
}
@Transactional
public List<Film> getFilmsFromCollection(Long collectionId){
Optional<Collection> collectionOptional = collectionRepository.findById(collectionId);
if (collectionOptional.isPresent()) {
Collection collection = collectionOptional.get();
return collection.getFilms();
} else {
throw new IllegalArgumentException("Collection not found with id: " + collectionId);
}
}
}

View 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 th:text="${errors}" class="margin-bottom alert-danger"></div>
<div layout:fragment="content">
<form action="#" th:action="@{/collection/add_film/{id}(id=${collectionId})}" method="post">
<div class="form-group">
<h2 class="text-danger mt-3">Выберите фильм:</h2>
<select class="form-select" name="filmId" id="films" required>
<option th:each="film, iterator: ${films}" th:text="${film.name}" th:value="${film.id}">
</select>
</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="@{/collection}">
Назад
</a>
</div>
</form>
</div>
</body>
</html>

View File

@ -34,6 +34,12 @@
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${collection.id}').click()|">
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
</button>
<a class="btn btn-success button-fixed button-sm"
th:href="@{/collection/films/{id}(id=${collection.id})}">
<i class="fa-solid fa-eye"></i>Посмотреть фильмы</a>
<a class="btn btn-success button-fixed button-sm"
th:href="@{/collection/getFilms/{id}(id=${collection.id})}">
<i class="fa-solid fa-plus"></i>Добавить фильмы</a>
</div>
<form th:action="@{/collection/delete/{id}(id=${collection.id})}" method="post">
<button th:id="'remove-' + ${collection.id}" type="submit" style="display: none">

View 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="film, iterator: ${films}">
<td th:text="${film.name}"></td>
</tr>
</tbody>
</table>
<a class="btn btn-secondary button-fixed" th:href="@{/collection}">
Назад
</a>
</div>
</div>
</body>
</html>