diff --git a/Frontend/vue-project/src/components/CatalogCollections.vue b/Frontend/vue-project/src/components/CatalogCollections.vue new file mode 100644 index 0000000..8957f25 --- /dev/null +++ b/Frontend/vue-project/src/components/CatalogCollections.vue @@ -0,0 +1,106 @@ + + + \ No newline at end of file diff --git a/Frontend/vue-project/src/components/Catalogs.vue b/Frontend/vue-project/src/components/Catalogs.vue index b95dcad..427bb95 100644 --- a/Frontend/vue-project/src/components/Catalogs.vue +++ b/Frontend/vue-project/src/components/Catalogs.vue @@ -3,9 +3,9 @@ data() { return { catalogs: [ - { name: 'collections', label: 'Сборники' }, + { name: 'genres', label: 'Жанры' }, { name: 'films', label: 'Фильмы' }, - { name: 'genres', label: 'Жанры' } + { name: 'collections', label: 'Коллекции' } ] } } diff --git a/Frontend/vue-project/src/main.js b/Frontend/vue-project/src/main.js index ccfdb17..f01baa4 100644 --- a/Frontend/vue-project/src/main.js +++ b/Frontend/vue-project/src/main.js @@ -10,7 +10,7 @@ import Table from './components/Table.vue' import Catalogs from './components/Catalogs.vue' import CatalogGenres from './components/CatalogGenres.vue' import CatalogFilms from './components/CatalogFilms.vue' - +import CatalogCollections from './components/CatalogCollections.vue' const routes = [ { path: '/', redirect: '/index' }, @@ -22,7 +22,8 @@ const routes = [ { path: '/table', component: Table}, { path: '/catalogs', component: Catalogs}, { path: '/catalogs/genres', component: CatalogGenres}, - { path: '/catalogs/films', component: CatalogFilms} + { path: '/catalogs/films', component: CatalogFilms}, + { path: '/catalogs/collections', component: CatalogCollections} ] const router = createRouter({ diff --git a/Frontend/vue-project/src/models/Collection.js b/Frontend/vue-project/src/models/Collection.js index dbef5df..69163d5 100644 --- a/Frontend/vue-project/src/models/Collection.js +++ b/Frontend/vue-project/src/models/Collection.js @@ -21,6 +21,6 @@ export default class Collection { } get filmIds() { - return this.filmIds; + return this._filmIds; } } \ No newline at end of file diff --git a/data.mv.db b/data.mv.db index f110373..4bd2a56 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/CollectionController.java b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/CollectionController.java index 8c49f10..1841c54 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/CollectionController.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/CollectionController.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.Collection; +import org.springframework.web.bind.annotation.*; import ru.ulstu.is.lab1.DataBase.service.CollectionService; -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 CollectionController { private final CollectionService collectionService; - @Autowired public CollectionController(CollectionService collectionService) { this.collectionService = collectionService; } @@ -37,41 +26,13 @@ public class CollectionController { } @PostMapping - public CollectionDTO createCollection(@RequestParam("name") String name, @RequestParam("FilmsId") String FilmsId) throws IOException { - List ids = new ArrayList<>(); - String num = ""; - if(FilmsId.length() == 0) - FilmsId = ""; - for (Character sm: - FilmsId.toCharArray()) { - if(sm.equals('-')) - { - ids.add(Long.parseLong(num)); - num=""; - } - else - num+=sm; - } - return new CollectionDTO(collectionService.addCollection(name, ids)); + public CollectionDTO createCollection(@RequestBody @Valid CollectionDTO collectionDTO) { + return new CollectionDTO(collectionService.addCollection(collectionDTO.getName())); } - @PatchMapping("/{id}") - public CollectionDTO updateCollection(@PathVariable Long id, @RequestParam("name") String name, @RequestParam("FilmsId") String FilmsId) { - List ids = new ArrayList<>(); - String num = ""; - if(FilmsId.length() == 0) - FilmsId = ""; - for (Character sm: - FilmsId.toCharArray()) { - if(sm.equals('-')) - { - ids.add(Long.parseLong(num)); - num=""; - } - else - num+=sm; - } - return new CollectionDTO(collectionService.updateCollection(id, name, ids)); + @PutMapping("/{id}") + public CollectionDTO updateCollection(@PathVariable Long id, @RequestBody @Valid CollectionDTO collectionDTO) { + return new CollectionDTO(collectionService.updateCollection(id, collectionDTO.getName())); } @DeleteMapping("/{id}") @@ -82,4 +43,14 @@ public class CollectionController { public void deleteAllCollections(){ collectionService.deleteAllCollections(); } + + @PostMapping("/add_film/{id}") + public CollectionDTO addFilm(@PathVariable Long id, @RequestParam Long film_id) { + return new CollectionDTO(collectionService.addFilm(id, film_id)); + } + + @DeleteMapping("/del_film/{id}") + public CollectionDTO delFilm(@PathVariable Long id, @RequestParam Long film_id) { + return new CollectionDTO(collectionService.deleteFilm(id, film_id)); + } } diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/CollectionDTO.java b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/CollectionDTO.java index 3688251..61a25cf 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/CollectionDTO.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/CollectionDTO.java @@ -1,22 +1,22 @@ package ru.ulstu.is.lab1.DataBase.controller; import ru.ulstu.is.lab1.DataBase.model.Collection; +import ru.ulstu.is.lab1.DataBase.model.Film; import java.util.List; public class CollectionDTO { private Long id; private String name; - private List filmDTOList; + private List filmIds; + public CollectionDTO() { + } public CollectionDTO(Collection collection){ this.id = collection.getId(); this.name = collection.getName(); - this.filmDTOList = collection.getFilms() == null ? null : collection.getFilms() - .stream() - .map(FilmDTO::new) - .toList(); - } - public CollectionDTO() { + if(collection.getFilms() != null) { + this.filmIds = collection.getFilms().stream().map(Film::getId).toList(); + } } public Long getId(){ return id; @@ -24,7 +24,7 @@ public class CollectionDTO { public String getName(){ return name; } - public List getFilmDTOList(){ - return filmDTOList; + public List getFilmIds(){ + return filmIds; } } diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/service/CollectionService.java b/src/main/java/ru/ulstu/is/lab1/DataBase/service/CollectionService.java index 14bac03..d22d4cd 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/service/CollectionService.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/service/CollectionService.java @@ -1,13 +1,15 @@ package ru.ulstu.is.lab1.DataBase.service; +import org.springframework.util.StringUtils; import ru.ulstu.is.lab1.DataBase.model.Film; import ru.ulstu.is.lab1.DataBase.model.Collection; import ru.ulstu.is.lab1.DataBase.Repository.ICollectionRepository; +import ru.ulstu.is.lab1.DataBase.model.Genre; import ru.ulstu.is.lab1.util.validation.ValidatorUtil; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.io.IOException; +import javax.persistence.EntityNotFoundException; import java.util.List; import java.util.Optional; @@ -23,17 +25,12 @@ public class CollectionService { this.filmService = filmService; } @Transactional - public Collection addCollection(String name, List filmsId) throws IOException { - final Collection collection = new Collection(name); - if(filmsId.size() > 0 ) - { - for (Long id: filmsId) { - Film film = filmService.findFilm(id); - collection.addFilm(film); - } + public Collection addCollection(String name) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Collection name is null or empty"); } + final Collection collection = new Collection(name); validatorUtil.validate(collection); - return collectionRepository.save(collection); } @@ -49,19 +46,12 @@ public class CollectionService { } @Transactional - public Collection updateCollection(Long id, String name, List filmsId) { + public Collection updateCollection(Long id, String name) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Collection name is null or empty"); + } final Collection currentCollection = findCollection(id); currentCollection.setName(name); - if(filmsId.size()>0) - { - currentCollection.getFilms().clear(); - for (Long filmId: filmsId) { - Film film = filmService.findFilm(filmId); - currentCollection.addFilm(film); - } - } - else - currentCollection.getFilms().clear(); validatorUtil.validate(currentCollection); return collectionRepository.save(currentCollection); } @@ -77,4 +67,34 @@ public class CollectionService { public void deleteAllCollections() { collectionRepository.deleteAll(); } + + @Transactional + public Collection addFilm(Long collectionId, Long filmId) { + Collection collection = findCollection(collectionId); + if (collection == null) { + throw new EntityNotFoundException(String.format("Collection with id [%s] is not found", collectionId)); + } + final Film film = filmService.findFilm(filmId); + if (film == null) { + throw new EntityNotFoundException(String.format("Film with id [%s] is not found", filmId)); + } + + collection.addFilm(film); + return collectionRepository.save(collection); + } + + @Transactional + public Collection deleteFilm(Long collectionId, Long filmId) { + Collection collection = findCollection(collectionId); + if (collection == null) { + throw new EntityNotFoundException(String.format("Collection with id [%s] is not found", collectionId)); + } + final Film film = filmService.findFilm(filmId); + if (film == null) { + throw new EntityNotFoundException(String.format("Film with id [%s] is not found", filmId)); + } + + collection.removeFilm(film); + return collectionRepository.save(collection); + } } 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 b912be8..e19280b 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 @@ -9,7 +9,6 @@ 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;