lab5 refactoring
This commit is contained in:
parent
c793d802ef
commit
69466dca35
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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}")
|
||||
|
@ -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";
|
||||
}
|
||||
|
||||
|
@ -36,6 +36,19 @@ public class FilmsService {
|
||||
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);
|
||||
}
|
||||
|
||||
@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
|
||||
public Film deleteFilm(Long id) {
|
||||
final Optional<Film> currentFilm = repo.findById(id);
|
||||
|
Loading…
Reference in New Issue
Block a user