From 1880d0dc53dc83d8d4a24233b35f457bc50b903f Mon Sep 17 00:00:00 2001 From: "ityurner02@mail.ru" Date: Mon, 27 Mar 2023 19:36:00 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=A03=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=82=D0=B5=D1=81=D1=82=D0=BE?= =?UTF-8?q?=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data.mv.db | Bin 36864 -> 36864 bytes .../controller/CollectionController.java | 6 ++ .../DataBase/controller/FilmController.java | 5 ++ .../is/lab1/DataBase/model/Collection.java | 31 ++++++- .../ru/ulstu/is/lab1/DataBase/model/Film.java | 31 ++++++- .../ulstu/is/lab1/DataBase/model/Genre.java | 13 +++ .../DataBase/service/CollectionService.java | 11 +++ .../is/lab1/DataBase/service/FilmService.java | 11 +++ .../ru/ulstu/is/lab1/JpaCollectionTests.java | 80 ++++++++++++++++++ .../java/ru/ulstu/is/lab1/JpaFilmTests.java | 78 +++++++++++++++++ .../java/ru/ulstu/is/lab1/JpaGenreTests.java | 61 +++++++++++++ .../ulstu/is/lab1/Lab1ApplicationTests.java | 24 ------ 12 files changed, 323 insertions(+), 28 deletions(-) create mode 100644 src/test/java/ru/ulstu/is/lab1/JpaCollectionTests.java create mode 100644 src/test/java/ru/ulstu/is/lab1/JpaFilmTests.java create mode 100644 src/test/java/ru/ulstu/is/lab1/JpaGenreTests.java delete mode 100644 src/test/java/ru/ulstu/is/lab1/Lab1ApplicationTests.java diff --git a/data.mv.db b/data.mv.db index 88ed3b628deb68813c042432fabe53332e8a9758..8b5e1f9870fe94703b4d54b37478d97eb117ca02 100644 GIT binary patch delta 461 zcmbV|K}!Nb7>0MuY%@fNAR(ielqp7Q(S5e+(Hc?>5>=MjG3kH7HeC^$jWHQZDBFF zKhj`xnHM|jBn5UgScsVDAUQHEC$%IwBelrNGTG3~A|=rr=v=?a4GJuq|NApK003U4 BWzzrv 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 6ae01df..b778f6c 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 @@ -10,6 +10,7 @@ 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 ru.ulstu.is.lab1.DataBase.model.Film; import ru.ulstu.is.lab1.DataBase.service.CollectionService; import java.util.List; @@ -49,6 +50,11 @@ public class CollectionController { return collectionService.addFilm(id, film_id); } + @DeleteMapping("/remove_film/{id}") + public Collection removeFilm(@PathVariable Long id, @RequestParam Long film_id) { + return collectionService.removeFilm(id, film_id); + } + @DeleteMapping("/{id}") public Collection deleteCollection(@PathVariable Long id) { return collectionService.deleteCollection(id); 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 84d26d7..0eb063e 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 @@ -52,6 +52,11 @@ public class FilmController { return filmService.addGenre(id, genre_id); } + @DeleteMapping("/remove_genre/{id}") + public Film removeGenre(@PathVariable Long id, @RequestParam Long genre_id) { + return filmService.removeGenre(id, genre_id); + } + @DeleteMapping("/{id}") public Film deleteFilm(@PathVariable Long id) { return filmService.deleteFilm(id); diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/model/Collection.java b/src/main/java/ru/ulstu/is/lab1/DataBase/model/Collection.java index cd3f7d4..576dabf 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/model/Collection.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/model/Collection.java @@ -2,7 +2,9 @@ package ru.ulstu.is.lab1.DataBase.model; import javax.persistence.*; +import java.util.ArrayList; import java.util.List; +import java.util.Objects; @Entity public class Collection { @@ -36,8 +38,33 @@ public class Collection { this.name = name; } - public void addFilm(Film f) { - films.add(f); + public void addFilm(Film film) { + if (films == null){ + films = new ArrayList<>(); + } + this.films.add(film); + } + + public void removeFilm(Film film){ + if(films != null){ + films.remove(film); + } + } + + public List getFilms() { + return films; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Collection collection = (Collection) o; + return Objects.equals(id, collection.id); + } + @Override + public int hashCode() { + return Objects.hash(id); } @Override diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/model/Film.java b/src/main/java/ru/ulstu/is/lab1/DataBase/model/Film.java index 6f6cf3f..20c2a1a 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/model/Film.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/model/Film.java @@ -3,6 +3,8 @@ package ru.ulstu.is.lab1.DataBase.model; import javax.persistence.*; import java.util.List; +import java.util.ArrayList; +import java.util.Objects; @Entity public class Film { @@ -36,8 +38,33 @@ public class Film { this.name = name; } - public void addGenre(Genre g) { - genres.add(g); + public void addGenre(Genre genre) { + if (genres == null){ + genres = new ArrayList<>(); + } + this.genres.add(genre); + } + + public void removeGenre(Genre genre){ + if(genres != null){ + genres.remove(genre); + } + } + + public List getGenres() { + return genres; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Film film = (Film) o; + return Objects.equals(id, film.id); + } + @Override + public int hashCode() { + return Objects.hash(id); } @Override 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 31a495f..71af0ed 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 @@ -3,6 +3,7 @@ package ru.ulstu.is.lab1.DataBase.model; import javax.persistence.*; import java.util.List; +import java.util.Objects; @Entity public class Genre { @@ -32,6 +33,18 @@ public class Genre { this.name = name; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Genre genre = (Genre) o; + return Objects.equals(id, genre.id); + } + @Override + public int hashCode() { + return Objects.hash(id); + } + @Override public String toString() { return "Genre{" + 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 c8dc645..a87e418 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 @@ -43,6 +43,17 @@ public class CollectionService { return em.merge(collection); } + @Transactional + public Collection removeFilm(Long collectionId, Long filmId){ + final Collection collection = em.find(Collection.class, collectionId); + final Film film = em.find(Film.class, filmId); + if(filmId <= 0 || film == null){ + throw new IllegalArgumentException("Film with id [%s] is not found"); + } + collection.removeFilm(film); + return em.merge(collection); + } + @Transactional(readOnly = true) public Collection findCollection(Long id) { final Collection collection = em.find(Collection.class, id); 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 ee4eb5a..a91c3cd 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 @@ -42,6 +42,17 @@ public class FilmService { return em.merge(film); } + @Transactional + public Film removeGenre(Long filmId, Long genreId){ + final Film film = em.find(Film.class, filmId); + final Genre genre = em.find(Genre.class, genreId); + if(genreId <= 0 || genre == null){ + throw new IllegalArgumentException("Genre with id [%s] is not found"); + } + film.removeGenre(genre); + return em.merge(film); + } + @Transactional(readOnly = true) public Film findFilm(Long id) { final Film film = em.find(Film.class, id); diff --git a/src/test/java/ru/ulstu/is/lab1/JpaCollectionTests.java b/src/test/java/ru/ulstu/is/lab1/JpaCollectionTests.java new file mode 100644 index 0000000..0f5dfef --- /dev/null +++ b/src/test/java/ru/ulstu/is/lab1/JpaCollectionTests.java @@ -0,0 +1,80 @@ +package ru.ulstu.is.lab1; + +import ru.ulstu.is.lab1.DataBase.model.Film; +import ru.ulstu.is.lab1.DataBase.model.Genre; +import ru.ulstu.is.lab1.DataBase.service.FilmService; +import ru.ulstu.is.lab1.DataBase.model.Collection; +import ru.ulstu.is.lab1.DataBase.service.CollectionService; +import javax.persistence.EntityNotFoundException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import ru.ulstu.is.lab1.DataBase.service.GenreService; + +import java.util.List; + +@SpringBootTest +public class JpaCollectionTests { + private static final Logger log = LoggerFactory.getLogger(JpaFilmTests.class); + @Autowired + private CollectionService collectionService; + @Autowired + private FilmService filmService; + @Test + void testCollectionsCreate(){ + collectionService.deleteAllCollections(); + filmService.deleteAllFilms(); + final Film film1 = filmService.addFilm("Джокер"); + final Film film2 = filmService.addFilm("Учитель на замену"); + final Collection collection1 = collectionService.addCollection("Грустное"); + collection1.addFilm(film1); + collection1.addFilm(film2); + final Collection collection2 = collectionService.addCollection("Об антигерое"); + collection2.addFilm(film1); + log.info("testCreateCollection " + collection1.toString()); + log.info("testCreateCollection " + collection2.toString()); + Assertions.assertNotNull(collection1.getId()); + Assertions.assertNotNull(collection2.getId()); + } + @Test + void testCollectionRead() { + collectionService.deleteAllCollections(); + filmService.deleteAllFilms(); + final Film film = filmService.addFilm("Астрал"); + final Collection collection = collectionService.addCollection("Пугающее"); + collection.addFilm(film); + log.info("testCollectionRead[0]: " + collection.toString()); + final Collection findCollection = collectionService.findCollection(collection.getId()); + log.info("testCollectionRead[1]: " + findCollection.toString()); + Assertions.assertEquals(collection, findCollection); + } + @Test + void testAddFilms(){ + collectionService.deleteAllCollections(); + filmService.deleteAllFilms(); + final Film film1 = filmService.addFilm("Начало"); + final Film film2 = filmService.addFilm("Пункт назначения"); + final Film film3 = filmService.addFilm("День сурка"); + final Collection collection = collectionService.addCollection("Избранное"); + collection.addFilm(film1); + collection.addFilm(film2); + collection.addFilm(film3); + log.info("testAddFilms[0]: " + collection.toString()); + Assertions.assertEquals(collection.getFilms().size(), 3); + } + @Test + void testCollectionReadNotFound() { + collectionService.deleteAllCollections(); + Assertions.assertThrows(EntityNotFoundException.class, () -> collectionService.findCollection(-1L)); + } + @Test + void testCollectionsReadAllEmpty() { + collectionService.deleteAllCollections(); + final List collections = collectionService.findAllCollections(); + log.info("testCollectionReadAllEmpty: " + collections.toString()); + Assertions.assertEquals(collections.size(), 0); + } +} diff --git a/src/test/java/ru/ulstu/is/lab1/JpaFilmTests.java b/src/test/java/ru/ulstu/is/lab1/JpaFilmTests.java new file mode 100644 index 0000000..028d7a0 --- /dev/null +++ b/src/test/java/ru/ulstu/is/lab1/JpaFilmTests.java @@ -0,0 +1,78 @@ +package ru.ulstu.is.lab1; + +import ru.ulstu.is.lab1.DataBase.model.Genre; +import ru.ulstu.is.lab1.DataBase.service.GenreService; +import ru.ulstu.is.lab1.DataBase.model.Film; +import ru.ulstu.is.lab1.DataBase.service.FilmService; +import javax.persistence.EntityNotFoundException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.List; + +@SpringBootTest +public class JpaFilmTests { + private static final Logger log = LoggerFactory.getLogger(JpaFilmTests.class); + @Autowired + private FilmService filmService; + @Autowired + private GenreService genreService; + @Test + void testFilmsCreate(){ + filmService.deleteAllFilms(); + genreService.deleteAllGenres(); + final Genre genre1 = genreService.addGenre("Фантастика"); + final Genre genre2 = genreService.addGenre("Комедия"); + final Film film1 = filmService.addFilm("Сапожник"); + film1.addGenre(genre1); + film1.addGenre(genre2); + final Film film2 = filmService.addFilm("Маска"); + film2.addGenre(genre1); + log.info("testCreateFilm " + film1.toString()); + log.info("testCreateFilm " + film2.toString()); + Assertions.assertNotNull(film1.getId()); + Assertions.assertNotNull(film2.getId()); + } + @Test + void testFilmRead() { + filmService.deleteAllFilms(); + genreService.deleteAllGenres(); + final Genre genre = genreService.addGenre("Ужасы"); + final Film film = filmService.addFilm("Оно"); + film.addGenre(genre); + log.info("testFilmRead[0]: " + film.toString()); + final Film findFilm = filmService.findFilm(film.getId()); + log.info("testFilmRead[1]: " + findFilm.toString()); + Assertions.assertEquals(film, findFilm); + } + @Test + void testAddGenres(){ + filmService.deleteAllFilms(); + genreService.deleteAllGenres(); + final Genre genre1 = genreService.addGenre("Драма"); + final Genre genre2 = genreService.addGenre("Триллер"); + final Genre genre3 = genreService.addGenre("Боевик"); + final Film film = filmService.addFilm("Брат"); + film.addGenre(genre1); + film.addGenre(genre2); + film.addGenre(genre3); + log.info("testAddGenres[0]: " + film.toString()); + Assertions.assertEquals(film.getGenres().size(), 3); + } + @Test + void testFilmReadNotFound() { + filmService.deleteAllFilms(); + Assertions.assertThrows(EntityNotFoundException.class, () -> filmService.findFilm(-1L)); + } + @Test + void testFilmsReadAllEmpty() { + filmService.deleteAllFilms(); + final List films = filmService.findAllFilms(); + log.info("testFilmReadAllEmpty: " + films.toString()); + Assertions.assertEquals(films.size(), 0); + } +} diff --git a/src/test/java/ru/ulstu/is/lab1/JpaGenreTests.java b/src/test/java/ru/ulstu/is/lab1/JpaGenreTests.java new file mode 100644 index 0000000..92d31e5 --- /dev/null +++ b/src/test/java/ru/ulstu/is/lab1/JpaGenreTests.java @@ -0,0 +1,61 @@ +package ru.ulstu.is.lab1; + +import ru.ulstu.is.lab1.DataBase.model.Genre; +import ru.ulstu.is.lab1.DataBase.service.GenreService; +import javax.persistence.EntityNotFoundException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.List; + +@SpringBootTest +class JpaGenreTests { + private static final Logger log = LoggerFactory.getLogger(JpaGenreTests.class); + @Autowired + private GenreService genreService; + @Test + void testGenresCreate(){ + genreService.deleteAllGenres(); + final Genre genre1 = genreService.addGenre("Фантастика"); + final Genre genre2 = genreService.addGenre("Комедия"); + log.info("testCreateGenre " + genre1.toString()); + log.info("testCreateGenre " + genre2.toString()); + Assertions.assertNotNull(genre1.getId()); + Assertions.assertNotNull(genre2.getId()); + } + @Test + void testGenreRead() { + genreService.deleteAllGenres(); + final Genre genre = genreService.addGenre("Ужасы"); + log.info("testGenreRead[0]: " + genre.toString()); + final Genre findGenre = genreService.findGenre(genre.getId()); + log.info("testGenreRead[1]: " + findGenre.toString()); + Assertions.assertEquals(genre, findGenre); + } + @Test + void testGenreReadNotFound() { + genreService.deleteAllGenres(); + Assertions.assertThrows(EntityNotFoundException.class, () -> genreService.findGenre(-1L)); + } + @Test + void testGenreReadAll() { + genreService.deleteAllGenres(); + genreService.addGenre("Драма"); + genreService.addGenre("Триллер"); + final List genres = genreService.findAllGenres(); + log.info("testGenreReadAll: " + genres.toString()); + Assertions.assertEquals(genres.size(), 2); + genreService.deleteAllGenres(); + } + @Test + void testGenreReadAllEmpty() { + genreService.deleteAllGenres(); + final List genres = genreService.findAllGenres(); + log.info("testGenreReadAllEmpty: " + genres.toString()); + Assertions.assertEquals(genres.size(), 0); + } +} diff --git a/src/test/java/ru/ulstu/is/lab1/Lab1ApplicationTests.java b/src/test/java/ru/ulstu/is/lab1/Lab1ApplicationTests.java deleted file mode 100644 index 39bfe9e..0000000 --- a/src/test/java/ru/ulstu/is/lab1/Lab1ApplicationTests.java +++ /dev/null @@ -1,24 +0,0 @@ -package ru.ulstu.is.lab1; - -import ru.ulstu.is.lab1.DataBase.model.Film; -import ru.ulstu.is.lab1.DataBase.model.Collection; -import ru.ulstu.is.lab1.DataBase.model.Genre; -import ru.ulstu.is.lab1.DataBase.service.FilmService; -import ru.ulstu.is.lab1.DataBase.service.CollectionService; -import ru.ulstu.is.lab1.DataBase.service.GenreService; -import javax.persistence.EntityNotFoundException; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; - -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -@SpringBootTest -class Lab1ApplicationTests { - -}