diff --git a/Frontend/vue-project/src/components/CatalogFilms.vue b/Frontend/vue-project/src/components/CatalogFilms.vue new file mode 100644 index 0000000..6dae233 --- /dev/null +++ b/Frontend/vue-project/src/components/CatalogFilms.vue @@ -0,0 +1,102 @@ + + + + + + + + + + Название фильма + + + + + + Добавить жанр + + Выберите жанр + + {{ genre.name }} + + + + Добавить + + \ No newline at end of file diff --git a/Frontend/vue-project/src/components/DataTable.vue b/Frontend/vue-project/src/components/DataTable.vue index 036f145..5d6b33c 100644 --- a/Frontend/vue-project/src/components/DataTable.vue +++ b/Frontend/vue-project/src/components/DataTable.vue @@ -65,6 +65,6 @@ user-select: none; } *{ - color: white; + color: red; } \ No newline at end of file diff --git a/Frontend/vue-project/src/components/Modal.vue b/Frontend/vue-project/src/components/Modal.vue index f12c004..4dd2b91 100644 --- a/Frontend/vue-project/src/components/Modal.vue +++ b/Frontend/vue-project/src/components/Modal.vue @@ -57,7 +57,10 @@ \ No newline at end of file diff --git a/Frontend/vue-project/src/main.js b/Frontend/vue-project/src/main.js index 75e6397..ccfdb17 100644 --- a/Frontend/vue-project/src/main.js +++ b/Frontend/vue-project/src/main.js @@ -9,6 +9,7 @@ import Form from './components/Form.vue' import Table from './components/Table.vue' import Catalogs from './components/Catalogs.vue' import CatalogGenres from './components/CatalogGenres.vue' +import CatalogFilms from './components/CatalogFilms.vue' const routes = [ @@ -20,7 +21,8 @@ const routes = [ { path: '/form', component: Form}, { path: '/table', component: Table}, { path: '/catalogs', component: Catalogs}, - { path: '/catalogs/genres', component: CatalogGenres} + { path: '/catalogs/genres', component: CatalogGenres}, + { path: '/catalogs/films', component: CatalogFilms} ] const router = createRouter({ diff --git a/data.mv.db b/data.mv.db index 8c0e32d..a977f73 100644 Binary files a/data.mv.db and b/data.mv.db differ diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/FilmController.java b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/FilmController.java index f4c84d8..d1937b3 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/FilmController.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/FilmController.java @@ -1,19 +1,9 @@ package ru.ulstu.is.lab1.DataBase.controller; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PatchMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; -import ru.ulstu.is.lab1.DataBase.model.Film; +import org.springframework.web.bind.annotation.*; import ru.ulstu.is.lab1.DataBase.service.FilmService; -import java.io.IOException; -import java.util.ArrayList; +import javax.validation.Valid; import java.util.List; @RestController @@ -21,7 +11,6 @@ import java.util.List; public class FilmController { private final FilmService filmService; - @Autowired public FilmController(FilmService filmService) { this.filmService = filmService; } @@ -37,41 +26,13 @@ public class FilmController { } @PostMapping - public FilmDTO createFilm(@RequestParam("name") String name, @RequestParam("GenresId") String GenresId) throws IOException { - List ids = new ArrayList<>(); - String num = ""; - if(GenresId.length() == 0) - GenresId = ""; - for (Character sm: - GenresId.toCharArray()) { - if(sm.equals('-')) - { - ids.add(Long.parseLong(num)); - num=""; - } - else - num+=sm; - } - return new FilmDTO(filmService.addFilm(name, ids)); + public FilmDTO createFilm(@RequestBody @Valid FilmDTO filmDTO) { + return new FilmDTO(filmService.addFilm(filmDTO.getName())); } - @PatchMapping("/{id}") - public FilmDTO updateFilm(@PathVariable Long id, @RequestParam("name") String name, @RequestParam("GenresId") String GenresId) { - List ids = new ArrayList<>(); - String num = ""; - if(GenresId.length() == 0) - GenresId = ""; - for (Character sm: - GenresId.toCharArray()) { - if(sm.equals('-')) - { - ids.add(Long.parseLong(num)); - num=""; - } - else - num+=sm; - } - return new FilmDTO(filmService.updateFilm(id, name, ids)); + @PutMapping("/{id}") + public FilmDTO updateFilm(@PathVariable Long id, @RequestBody @Valid FilmDTO filmDTO) { + return new FilmDTO(filmService.updateFilm(id, filmDTO.getName())); } @DeleteMapping("/{id}") @@ -83,4 +44,14 @@ public class FilmController { public void deleteAllFilms(){ filmService.deleteAllFilms(); } + + @PostMapping("/add_genre/{id}") + public FilmDTO addGenre(@PathVariable Long id, @RequestParam Long genre_id) { + return new FilmDTO(filmService.addGenre(id, genre_id)); + } + + @DeleteMapping("/del_genre/{id}") + public FilmDTO delGenre(@PathVariable Long id, @RequestParam Long genre_id) { + return new FilmDTO(filmService.deleteGenre(id, genre_id)); + } } diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/FilmDTO.java b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/FilmDTO.java index 95ec3c4..2b18abd 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/FilmDTO.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/FilmDTO.java @@ -1,23 +1,22 @@ package ru.ulstu.is.lab1.DataBase.controller; import ru.ulstu.is.lab1.DataBase.model.Film; +import ru.ulstu.is.lab1.DataBase.model.Genre; import java.util.List; public class FilmDTO { private Long id; private String name; - private List genreDTOList; - private List collectionDTOList; + private List genreIds; + public FilmDTO() { + } public FilmDTO(Film film){ this.id = film.getId(); this.name = film.getName(); - this.genreDTOList = film.getGenres() == null ? null : film.getGenres() - .stream() - .map(GenreDTO::new) - .toList(); - } - public FilmDTO() { + if(film.getGenres() != null) { + this.genreIds = film.getGenres().stream().map(Genre::getId).toList(); + } } public Long getId(){ return id; @@ -25,7 +24,7 @@ public class FilmDTO { public String getName(){ return name; } - public List getGenreDTOList(){ - return genreDTOList; + public List getGenreIds(){ + return genreIds; } } diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/GenreDTO.java b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/GenreDTO.java index b49a765..ff9302b 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/GenreDTO.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/GenreDTO.java @@ -17,5 +17,4 @@ public class GenreDTO { public String getName(){ return name; } - public void setName(String name) { this.name = name; } } diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/model/Genre.java b/src/main/java/ru/ulstu/is/lab1/DataBase/model/Genre.java index cd9319b..9680cde 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/model/Genre.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/model/Genre.java @@ -34,12 +34,10 @@ public class Genre { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (!(o instanceof Genre)) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; Genre genre = (Genre) o; - return Objects.equals(id, genre.id) && Objects.equals(this.name, genre.name); + return Objects.equals(id, genre.id); } @Override public int hashCode() { diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/service/FilmService.java b/src/main/java/ru/ulstu/is/lab1/DataBase/service/FilmService.java index fd063d4..f4f9866 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/service/FilmService.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/service/FilmService.java @@ -6,7 +6,9 @@ import ru.ulstu.is.lab1.DataBase.Repository.IFilmRepository; import ru.ulstu.is.lab1.util.validation.ValidatorUtil; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; +import javax.persistence.EntityNotFoundException; import java.io.IOException; import java.util.List; import java.util.Optional; @@ -24,17 +26,12 @@ public class FilmService { } @Transactional - public Film addFilm(String name, List genresId) throws IOException { - final Film film = new Film(name); - if(genresId.size() > 0 ) - { - for (Long id: genresId) { - Genre genre = genreService.findGenre(id); - film.addGenre(genre); - } + public Film addFilm(String name) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Film name is null or empty"); } + final Film film = new Film(name); validatorUtil.validate(film); - return filmRepository.save(film); } @@ -50,19 +47,12 @@ public class FilmService { } @Transactional - public Film updateFilm(Long id, String name, List genresId) { + public Film updateFilm(Long id, String name) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Film name is null or empty"); + } final Film currentFilm = findFilm(id); currentFilm.setName(name); - if(genresId.size()>0) - { - currentFilm.getGenres().clear(); - for (Long genreId: genresId) { - Genre genre = genreService.findGenre(genreId); - currentFilm.addGenre(genre); - } - } - else - currentFilm.getGenres().clear(); validatorUtil.validate(currentFilm); return filmRepository.save(currentFilm); } @@ -78,4 +68,40 @@ public class FilmService { public void deleteAllFilms() { filmRepository.deleteAll(); } + + @Transactional + public Film addGenre(Long filmId, Long genreId) { + final Optional filmOpt = filmRepository.findById(filmId); + + if (filmOpt.isEmpty()) { + throw new EntityNotFoundException(String.format("Film with id [%s] is not found", filmId)); + } + Film film = filmOpt.get(); + + final Genre genre = genreService.findGenre(genreId); + if (genre == null) { + throw new EntityNotFoundException(String.format("Genre with id [%s] is not found", genreId)); + } + + film.addGenre(genre); + return filmRepository.save(film); + } + + @Transactional + public Film deleteGenre(Long filmId, Long genreId) { + final Optional filmOpt = filmRepository.findById(filmId); + + if (filmOpt.isEmpty()) { + throw new EntityNotFoundException(String.format("Film with id [%s] is not found", filmId)); + } + Film film = filmOpt.get(); + + 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); + } }