From 69466dca3508bcd94e7b01335481ca862594bfbb Mon Sep 17 00:00:00 2001 From: parap Date: Mon, 15 May 2023 19:08:42 +0400 Subject: [PATCH] lab5 refactoring --- data.mv.db | Bin 155648 -> 155648 bytes .../java/ru/ip/labs/labs/LabsApplication.java | 4 --- .../labs/films/controller/FilmController.java | 8 ++--- .../films/controller/FilmMvcController.java | 8 ++--- .../labs/labs/films/service/FilmsService.java | 33 ++++++++++++++++++ 5 files changed, 37 insertions(+), 16 deletions(-) diff --git a/data.mv.db b/data.mv.db index ed8c9f8ed6cfceb6e99540ca3b6f9951e5cb0a30..0b87f4ade90b6ed9852b3611dbfe975e2d2d378c 100644 GIT binary patch delta 15039 zcmeHOdu$xV8QugDNdk!-oHz-D za)(3IumB`Xm4Ya9-*0`!BwL3Y8s05WN=KQK`%1-TvnxH{8 zRb6%f7mGP)p6SePET@{N3U6&Yc**(T(9-L8CMXNM89oV0^rVGPA|E|T(Np|ZkLnEP zl=ao&v3rgn`j_a)j~bj!cce<0o=Q}$yPP*rj$|q5E{2 zFSkUGWSnFKG-LIEXkth-PQf&jSdEQ{rb>owa8B3$++rgpodEBFbR4H@?%mI##k9yf z&s@{s+$_`-ILq`pVijp>M|{|kVBn58TN0y=G+y8pxqYy2aIa;0yG6HjD=2sE+Zyc} zatHC=Fj$cl+WQS-aF5n!Wbeo`@`-Q-M9JB_;j)75?^bonZjB}`dJ|^^gBXt>$R<U#Ix8&R$h@@q)@kYDAf7mp&0pb)*Nydi5Pu7K|I zw8#=GAlsRzq7`s{`f^7&99Eq+Z8aQ_jCTmbvl=lVZI&%T>`c+{w}hng#0YGQVSgM9 zK{48$H?}zi)?ZkLGg)aN%ldmCN{kLAzjypj!zEvs35voHC6ggaB4ZFG_!Ta0$t(CB z$U>M9%QLEi-xY!%!IL2N>Q8O4`0_GZOIPP3lbnibCaZKsxXoBMQ45X=ZPw45D3$tma0$p zB(sS~pn?R&LXv7pYpKBml}M%nv6^}{3$Hv8iyPFZ_m}O5_aI`Ex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85^LzgfQ+Lq5~GvW}iu%hxAOFx{rkK6915vo6uUtmT){hgb3mP&m{d7Ak<6edb>8F8dy*aZQEqAm9?^-p4l6(^h2iB#J&Y# zYOd!O2YZ#G6Ys1H`?7Mq`z>htdpt&UJthm^f_5R&d|`S-7A4nVRB)IqD49gr9U#b= z!!Sku;o;wnJ|M7#4+-r5%8JsK%_vUqUB@3L&+Qqe{WtST_=e`M=gj0#&)KoTqiAsC EAF&mz5&!@I 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);