ЛР5 чуть-чуть изменить логику чекбоксов

This commit is contained in:
ityurner02@mail.ru 2023-05-24 17:26:29 +04:00
parent 4246c98078
commit 7512de70c2
6 changed files with 43 additions and 0 deletions

Binary file not shown.

View File

@ -36,6 +36,7 @@ public class CollectionMvcController {
public String getFilmsFromCollection(@PathVariable Long id, Model model) { public String getFilmsFromCollection(@PathVariable Long id, Model model) {
List<Film> films = collectionService.getFilmsFromCollection(id); List<Film> films = collectionService.getFilmsFromCollection(id);
model.addAttribute("films", films); model.addAttribute("films", films);
model.addAttribute("collectionId", id);
return "view-films"; return "view-films";
} }
@ -91,4 +92,11 @@ public class CollectionMvcController {
collectionService.addFilm(id, filmIdAsLong); collectionService.addFilm(id, filmIdAsLong);
return "redirect:../films/{id}"; return "redirect:../films/{id}";
} }
@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/films/{id}";
}
} }

View File

@ -37,6 +37,7 @@ public class FilmMvcController {
public String getGenresFromFilm(@PathVariable Long id, Model model) { public String getGenresFromFilm(@PathVariable Long id, Model model) {
List<Genre> genres = filmService.getGenresFromFilm(id); List<Genre> genres = filmService.getGenresFromFilm(id);
model.addAttribute("genres", genres); model.addAttribute("genres", genres);
model.addAttribute("filmId", id);
return "view-genres"; return "view-genres";
} }
@ -94,4 +95,11 @@ public class FilmMvcController {
filmService.addGenres(id, genreIdsAsLong); filmService.addGenres(id, genreIdsAsLong);
return "redirect:../genres/{id}"; return "redirect:../genres/{id}";
} }
@PostMapping(value = "/del_genre/{id}/{genreId}")
public String deleteFilm(@PathVariable(value = "id") Long id,
@PathVariable(value = "genreId") Long genreId) {
filmService.deleteGenre(id, genreId);
return "redirect:/film/genres/{id}";
}
} }

View File

@ -82,6 +82,21 @@ public class FilmService {
return filmRepository.save(film); return filmRepository.save(film);
} }
@Transactional
public Film deleteGenre(Long filmId, Long genreId) {
Film film = findFilm(filmId);
if (film == null) {
throw new EntityNotFoundException(String.format("Film with id [%s] is not found", filmId));
}
final Genre genre = genreService.findGenre(genreId);
if (genre == null) {
throw new EntityNotFoundException(String.format("Genre with id [%s] is not found", genreId));
}
film.removeGenre(genre);
return filmRepository.save(film);
}
@Transactional @Transactional
public List<Genre> getGenresFromFilm(Long filmId){ public List<Genre> getGenresFromFilm(Long filmId){
Optional<Film> filmOptional = filmRepository.findById(filmId); Optional<Film> filmOptional = filmRepository.findById(filmId);

View File

@ -11,11 +11,17 @@
<thead> <thead>
<tr> <tr>
<th scope="col">Название</th> <th scope="col">Название</th>
<th></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr th:each="film, iterator: ${films}"> <tr th:each="film, iterator: ${films}">
<td th:text="${film.name}"></td> <td th:text="${film.name}"></td>
<td>
<form method="post" th:action="@{/collection/del_film/{id}/{filmId}(id=${id}, filmId=${film.id})}">
<button type="submit" class="btn btn-danger button-fixed button-sm">Удалить</button>
</form>
</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>

View File

@ -11,11 +11,17 @@
<thead> <thead>
<tr> <tr>
<th scope="col">Название</th> <th scope="col">Название</th>
<th></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr th:each="genre, iterator: ${genres}"> <tr th:each="genre, iterator: ${genres}">
<td th:text="${genre.name}"></td> <td th:text="${genre.name}"></td>
<td>
<form method="post" th:action="@{/film/del_genre/{id}/{genreId}(id=${id}, genreId=${genre.id})}">
<button type="submit" class="btn btn-danger button-fixed button-sm">Удалить</button>
</form>
</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>