diff --git a/data.mv.db b/data.mv.db index ed8c9f8..0b87f4a 100644 Binary files a/data.mv.db and b/data.mv.db differ diff --git a/src/main/java/ru/ip/labs/labs/LabsApplication.java b/src/main/java/ru/ip/labs/labs/LabsApplication.java index 345aaae..b7e5532 100644 --- a/src/main/java/ru/ip/labs/labs/LabsApplication.java +++ b/src/main/java/ru/ip/labs/labs/LabsApplication.java @@ -5,8 +5,4 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class LabsApplication { - - public static void main(String[] args) { - SpringApplication.run(LabsApplication.class, args); - } } diff --git a/src/main/java/ru/ip/labs/labs/films/controller/FilmController.java b/src/main/java/ru/ip/labs/labs/films/controller/FilmController.java index e8e4d4a..d847926 100644 --- a/src/main/java/ru/ip/labs/labs/films/controller/FilmController.java +++ b/src/main/java/ru/ip/labs/labs/films/controller/FilmController.java @@ -32,17 +32,13 @@ public class FilmController { @PostMapping("") public FilmDTO createFilm(@RequestBody @Valid FilmDTO film) { - Film result = filmService.addFilm(film.getName()); - filmService.updateActors(result.getId(), film.getFullNames()); - return new FilmDTO(filmService.updateGenres(result.getId(), film.getGenre())); + return new FilmDTO(filmService.addFilm(film.getName(), film.getGenre(), film.getFullNames())); } @PatchMapping("/{id}") public FilmDTO updateFilm(@PathVariable Long id, @RequestBody @Valid FilmDTO film) { - Film result = filmService.updateFilm(id, film.getName()); - filmService.updateActors(result.getId(), film.getFullNames()); - return new FilmDTO(filmService.updateGenres(result.getId(), film.getGenre())); + return new FilmDTO(filmService.updateFilm(id, film.getName(), film.getGenre(), film.getFullNames())); } @PatchMapping("/add_genre/{id}") diff --git a/src/main/java/ru/ip/labs/labs/films/controller/FilmMvcController.java b/src/main/java/ru/ip/labs/labs/films/controller/FilmMvcController.java index f076496..cc65a83 100644 --- a/src/main/java/ru/ip/labs/labs/films/controller/FilmMvcController.java +++ b/src/main/java/ru/ip/labs/labs/films/controller/FilmMvcController.java @@ -49,16 +49,12 @@ public class FilmMvcController { model.addAttribute("errors", bindingResult.getAllErrors()); return "films-catalog"; } - Long filmId = id; if (id == null || id <= 0) { - Film result = filmService.addFilm(filmDTO.getName()); - filmId = result.getId(); + filmService.addFilm(filmDTO.getName(), filmDTO.getGenre(), filmDTO.getFullNames()); } else { - filmService.updateFilm(id, filmDTO.getName()); + filmService.updateFilm(id, filmDTO.getName(), filmDTO.getGenre(), filmDTO.getFullNames()); } - filmService.updateGenres(filmId, filmDTO.getGenre()); - filmService.updateActors(filmId, filmDTO.getFullNames()); return "redirect:/film"; } diff --git a/src/main/java/ru/ip/labs/labs/films/service/FilmsService.java b/src/main/java/ru/ip/labs/labs/films/service/FilmsService.java index ef3d9fe..60705e7 100644 --- a/src/main/java/ru/ip/labs/labs/films/service/FilmsService.java +++ b/src/main/java/ru/ip/labs/labs/films/service/FilmsService.java @@ -36,6 +36,19 @@ public class FilmsService { return repo.save(film); } + @Transactional + public Film addFilm(String name, List genres, List fullNames) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Student name is null or empty"); + } + + Film film = new Film(name); + Film result = repo.save(film); + + updateGenres(result.getId(), genres); + return updateActors(film.getId(), fullNames); + } + // фильмы по жанру // фильмы по актеру @@ -149,6 +162,26 @@ public class FilmsService { return repo.save(currentFilm); } + @Transactional + public Film updateFilm(Long id, String name, List genres, List fullNames) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Film name is null or empty"); + } + final Optional currentFilmOpt = repo.findById(id); + + if(currentFilmOpt.isEmpty()) { + return null; + } + + final Film currentFilm = currentFilmOpt.get(); + + currentFilm.setName(name); + repo.save(currentFilm); + + updateGenres(id, genres); + return updateActors(id, fullNames); + } + @Transactional public Film deleteFilm(Long id) { final Optional currentFilm = repo.findById(id);