lab5 refactoring

This commit is contained in:
parap 2023-05-15 19:08:42 +04:00
parent c793d802ef
commit 69466dca35
5 changed files with 37 additions and 16 deletions

Binary file not shown.

View File

@ -5,8 +5,4 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @SpringBootApplication
public class LabsApplication { public class LabsApplication {
public static void main(String[] args) {
SpringApplication.run(LabsApplication.class, args);
}
} }

View File

@ -32,17 +32,13 @@ public class FilmController {
@PostMapping("") @PostMapping("")
public FilmDTO createFilm(@RequestBody @Valid FilmDTO film) { public FilmDTO createFilm(@RequestBody @Valid FilmDTO film) {
Film result = filmService.addFilm(film.getName()); return new FilmDTO(filmService.addFilm(film.getName(), film.getGenre(), film.getFullNames()));
filmService.updateActors(result.getId(), film.getFullNames());
return new FilmDTO(filmService.updateGenres(result.getId(), film.getGenre()));
} }
@PatchMapping("/{id}") @PatchMapping("/{id}")
public FilmDTO updateFilm(@PathVariable Long id, public FilmDTO updateFilm(@PathVariable Long id,
@RequestBody @Valid FilmDTO film) { @RequestBody @Valid FilmDTO film) {
Film result = filmService.updateFilm(id, film.getName()); return new FilmDTO(filmService.updateFilm(id, film.getName(), film.getGenre(), film.getFullNames()));
filmService.updateActors(result.getId(), film.getFullNames());
return new FilmDTO(filmService.updateGenres(result.getId(), film.getGenre()));
} }
@PatchMapping("/add_genre/{id}") @PatchMapping("/add_genre/{id}")

View File

@ -49,16 +49,12 @@ public class FilmMvcController {
model.addAttribute("errors", bindingResult.getAllErrors()); model.addAttribute("errors", bindingResult.getAllErrors());
return "films-catalog"; return "films-catalog";
} }
Long filmId = id;
if (id == null || id <= 0) { if (id == null || id <= 0) {
Film result = filmService.addFilm(filmDTO.getName()); filmService.addFilm(filmDTO.getName(), filmDTO.getGenre(), filmDTO.getFullNames());
filmId = result.getId();
} else { } 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"; return "redirect:/film";
} }

View File

@ -36,6 +36,19 @@ public class FilmsService {
return repo.save(film); return repo.save(film);
} }
@Transactional
public Film addFilm(String name, List<String> genres, List<String> 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); return repo.save(currentFilm);
} }
@Transactional
public Film updateFilm(Long id, String name, List<String> genres, List<String> fullNames) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Film name is null or empty");
}
final Optional<Film> 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 @Transactional
public Film deleteFilm(Long id) { public Film deleteFilm(Long id) {
final Optional<Film> currentFilm = repo.findById(id); final Optional<Film> currentFilm = repo.findById(id);