diff --git a/src/main/java/ru/ip/labs/labs/films/controller/GenreController.java b/src/main/java/ru/ip/labs/labs/films/controller/GenreController.java index bb371f1..5f37391 100644 --- a/src/main/java/ru/ip/labs/labs/films/controller/GenreController.java +++ b/src/main/java/ru/ip/labs/labs/films/controller/GenreController.java @@ -25,7 +25,7 @@ public class GenreController { @GetMapping("/") public List getGenres() { - return genreService.findAllFilms(); + return genreService.findAllGenres(); } @PostMapping("/") diff --git a/src/main/java/ru/ip/labs/labs/films/models/Actor.java b/src/main/java/ru/ip/labs/labs/films/models/Actor.java index e28f066..5d4339c 100644 --- a/src/main/java/ru/ip/labs/labs/films/models/Actor.java +++ b/src/main/java/ru/ip/labs/labs/films/models/Actor.java @@ -1,5 +1,8 @@ package ru.ip.labs.labs.films.models; import javax.persistence.*; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.util.List; @Entity @@ -9,24 +12,36 @@ public class Actor { private Long id; private String name; private String surname; + @Lob + private byte[] photo; - @ManyToMany(fetch = FetchType.EAGER) - @JoinTable(name="actors_films", - joinColumns = @JoinColumn(name="actor_id"), - inverseJoinColumns = @JoinColumn(name="film_id") - ) + @ManyToMany(fetch = FetchType.EAGER, mappedBy = "actors") private List films; public Actor() {} - public Actor(String name) { + public Actor(String name, String surname) { this.name = name; + this.surname = surname; + } + + public byte[] getPhoto() { + return photo; + } + + public void setPhoto(String path) throws IOException { + File f = new File(path); + photo = Files.readAllBytes(f.toPath()); } public Long getId() { return id; } + public String getName() { + return name; + } + public String getSurname() { return surname; } @@ -39,8 +54,8 @@ public class Actor { this.name = name; } - public void addFilm(Film f) { - films.add(f); + public List getFilms() { + return films; } @Override diff --git a/src/main/java/ru/ip/labs/labs/films/models/Film.java b/src/main/java/ru/ip/labs/labs/films/models/Film.java index 2ddf4a9..1b8ce35 100644 --- a/src/main/java/ru/ip/labs/labs/films/models/Film.java +++ b/src/main/java/ru/ip/labs/labs/films/models/Film.java @@ -1,4 +1,7 @@ package ru.ip.labs.labs.films.models; +import org.hibernate.annotations.LazyCollection; +import org.hibernate.annotations.LazyCollectionOption; + import javax.persistence.*; import java.util.Iterator; import java.util.List; @@ -10,13 +13,21 @@ public class Film { private Long id; private String name; - @ManyToMany(fetch = FetchType.EAGER) + @ManyToMany + @LazyCollection(LazyCollectionOption.FALSE) @JoinTable(name="films_genres", joinColumns = @JoinColumn(name="film_id"), inverseJoinColumns = @JoinColumn(name="genre_id") ) private List genres; + @ManyToMany(fetch = FetchType.EAGER) + @JoinTable(name="actors_films", + joinColumns = @JoinColumn(name="film_id"), + inverseJoinColumns = @JoinColumn(name="actor_id") + ) + private List actors; + public Film() {} public Film(String name) { @@ -27,6 +38,10 @@ public class Film { return id; } + public String getName() { + return name; + } + public void setName(String name) { this.name = name; } @@ -35,6 +50,18 @@ public class Film { genres.add(g); } + public void addActor(Actor actor) { + actors.add(actor); + } + + public List getActors() { + return actors; + } + + public List getGenres() { + return genres; + } + @Override public String toString() { String res = "\nFilm{" + diff --git a/src/main/java/ru/ip/labs/labs/films/models/Genre.java b/src/main/java/ru/ip/labs/labs/films/models/Genre.java index 8e4dd75..6e8bb9b 100644 --- a/src/main/java/ru/ip/labs/labs/films/models/Genre.java +++ b/src/main/java/ru/ip/labs/labs/films/models/Genre.java @@ -9,9 +9,6 @@ public class Genre { private Long id; private String name; - @ManyToMany(fetch = FetchType.EAGER, mappedBy = "genres") - private List films; - public Genre() {} public Genre(String name) { this.name = name; diff --git a/src/main/java/ru/ip/labs/labs/films/service/ActorService.java b/src/main/java/ru/ip/labs/labs/films/service/ActorService.java index d420ded..7fe1739 100644 --- a/src/main/java/ru/ip/labs/labs/films/service/ActorService.java +++ b/src/main/java/ru/ip/labs/labs/films/service/ActorService.java @@ -9,6 +9,7 @@ import ru.ip.labs.labs.films.models.Film; import javax.persistence.EntityManager; import javax.persistence.EntityNotFoundException; import javax.persistence.PersistenceContext; +import java.io.IOException; import java.util.List; @Service @@ -23,26 +24,25 @@ public class ActorService { throw new IllegalArgumentException("Student name is null or empty"); } - Actor actor = new Actor(name); + Actor actor = new Actor(name, surname); em.persist(actor); return em.find(Actor.class, actor.getId()); } - @Transactional - public Actor addFilm(Long actorId, Long filmId) { - final Actor actor = em.find(Actor.class, actorId); - if (actor == null) { - throw new EntityNotFoundException(String.format("Actor with id [%s] is not found", actorId)); + public Actor addActor(String name, String surname, String path) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Student name is null or empty"); } - final Film film = em.find(Film.class, filmId); - if (film == null) { - throw new EntityNotFoundException(String.format("Film with id [%s] is not found", filmId)); + Actor actor = new Actor(name, surname); + try { + actor.setPhoto(path); + } catch(IOException err) { + System.out.println(err.getMessage()); } - - actor.addFilm(film); - return em.merge(actor); + em.persist(actor); + return em.find(Actor.class, actor.getId()); } @Transactional(readOnly = true) @@ -54,7 +54,6 @@ public class ActorService { return actor; } - @Transactional(readOnly = true) public List findAllActors() { return em.createQuery("select f from Actor f", Actor.class) @@ -62,12 +61,13 @@ public class ActorService { } @Transactional - public Actor updateActor(Long id, String name) { + public Actor updateActor(Long id, String name, String surname) { if (!StringUtils.hasText(name)) { throw new IllegalArgumentException("Actor name is null or empty"); } final Actor currentActor = findActor(id); - currentActor.setName(name); + if(name != null) currentActor.setName(name); + if(surname != null) currentActor.setSurname(surname); return em.merge(currentActor); } diff --git a/src/main/java/ru/ip/labs/labs/films/service/FilmsService.java b/src/main/java/ru/ip/labs/labs/films/service/FilmsService.java index 4b75781..9aea1bc 100644 --- a/src/main/java/ru/ip/labs/labs/films/service/FilmsService.java +++ b/src/main/java/ru/ip/labs/labs/films/service/FilmsService.java @@ -3,12 +3,15 @@ package ru.ip.labs.labs.films.service; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; +import ru.ip.labs.labs.films.models.Actor; import ru.ip.labs.labs.films.models.Film; import ru.ip.labs.labs.films.models.Genre; import javax.persistence.EntityManager; import javax.persistence.EntityNotFoundException; import javax.persistence.PersistenceContext; +import javax.persistence.Query; +import java.time.LocalDate; import java.util.List; @Service @@ -29,9 +32,13 @@ public class FilmsService { return em.find(Film.class, film.getId()); } + // фильмы по жанру + // фильмы по актеру + @Transactional public Film addGenre(Long filmId, Long genreId) { final Film film = em.find(Film.class, filmId); + if (film == null) { throw new EntityNotFoundException(String.format("Film with id [%s] is not found", filmId)); } @@ -45,6 +52,22 @@ public class FilmsService { return em.merge(film); } + @Transactional + public Film addActor(Long filmId, Long actorId) { + final Film film = em.find(Film.class, filmId); + if (film == null) { + throw new EntityNotFoundException(String.format("Film with id [%s] is not found", filmId)); + } + + final Actor actor = em.find(Actor.class, actorId); + if (actor == null) { + throw new EntityNotFoundException(String.format("Genre with id [%s] is not found", actorId)); + } + + film.addActor(actor); + return em.merge(film); + } + @Transactional(readOnly = true) public Film findFilm(Long id) { final Film film = em.find(Film.class, id); @@ -54,6 +77,28 @@ public class FilmsService { return film; } + @Transactional(readOnly = true) + public List findFilmByGenre(Long id, Long genreId) { + Query query = em.createQuery("select f from Film f " + + "join f.genres g " + + "where g.id = :genreId", Film.class) + .setParameter("genreId", genreId); + + List result = query.getResultList(); + return result; + } + + @Transactional(readOnly = true) + public List findFilmByActor(Long id, String name) { + Query query = em.createQuery("select f from Film f " + + "join f.actors a " + + "where a.name like :actorName", Film.class) + .setParameter("actorName", '%' + name + '%'); + + List result = query.getResultList(); + return result; + } + @Transactional(readOnly = true) public List findAllFilms() { diff --git a/src/main/java/ru/ip/labs/labs/films/service/GenreService.java b/src/main/java/ru/ip/labs/labs/films/service/GenreService.java index c520984..57b2dfc 100644 --- a/src/main/java/ru/ip/labs/labs/films/service/GenreService.java +++ b/src/main/java/ru/ip/labs/labs/films/service/GenreService.java @@ -38,9 +38,8 @@ public class GenreService { return genre; } - @Transactional(readOnly = true) - public List findAllFilms() { + public List findAllGenres() { return em.createQuery("select g from Genre g", Genre.class) .getResultList(); } diff --git a/src/test/java/ru/ip/labs/labs/JpaActorsTest.java b/src/test/java/ru/ip/labs/labs/JpaActorsTest.java index cb462ab..6fddb3a 100644 --- a/src/test/java/ru/ip/labs/labs/JpaActorsTest.java +++ b/src/test/java/ru/ip/labs/labs/JpaActorsTest.java @@ -1,4 +1,100 @@ package ru.ip.labs.labs; +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.ip.labs.labs.films.models.Actor; +import ru.ip.labs.labs.films.models.Film; +import ru.ip.labs.labs.films.models.Genre; +import ru.ip.labs.labs.films.service.ActorService; +import ru.ip.labs.labs.films.service.FilmsService; +import ru.ip.labs.labs.films.service.GenreService; + +import javax.persistence.EntityNotFoundException; +import java.util.List; + +@SpringBootTest public class JpaActorsTest { + private static final Logger log = LoggerFactory.getLogger(JpaFilmsTest.class); + @Autowired + FilmsService filmService; + + @Autowired + ActorService actorService; + + @Test + void testFilmCreate() { + actorService.deleteAllActors(); + final Actor actor = actorService.addActor("Victor", "Ivanov"); + log.info(actor.toString()); + Assertions.assertNotNull(actor.getId()); + } + + @Test + void testFilmReadNotFound() { + actorService.deleteAllActors(); + Assertions.assertThrows(EntityNotFoundException.class, () -> actorService.findActor(-1L)); + } + + @Test + void findFilm() { + actorService.deleteAllActors(); + final Actor actor = actorService.addActor("Victor", "Ivanov"); + log.info(actor.toString()); + final Actor findActor = actorService.findActor(actor.getId()); + log.info(findActor.toString()); + Assertions.assertEquals(actor.toString(), findActor.toString()); + } + + @Test + void testReadAll() { + actorService.deleteAllActors(); + final Actor actor = actorService.addActor("Victor", "Ivanov"); + log.info(actor.toString()); + final List actors = actorService.findAllActors(); + Assertions.assertEquals(actors.size(), 1); + } + + @Test + void updateActor() { + actorService.deleteAllActors(); + final Actor actor = actorService.addActor("Victor", "Ivanov"); + actorService.updateActor(actor.getId(), "Terminator", null); + final Actor findActor = actorService.findActor(actor.getId()); + log.info(findActor.toString()); + Assertions.assertEquals(findActor.getName() + " " + findActor.getSurname(), "Terminator Ivanov"); + } + + @Test + void deleteActor() { + actorService.deleteAllActors(); + final Actor actor = actorService.addActor("Victor", "Ivanov"); + log.info(actor.toString()); + actorService.deleteActor(actor.getId()); + Assertions.assertThrows(EntityNotFoundException.class, () -> actorService.findActor(actor.getId())); + } + + @Test + void addPhoto() { + actorService.deleteAllActors(); + final Actor actor = actorService.addActor("Victor", "Ivanov", "C:\\Users\\админ\\Pictures\\Screenshots\\Снимок экрана (2).png"); + Actor findActor = actorService.findActor(actor.getId()); + Assertions.assertNotEquals(findActor.getPhoto().length, 0); + } + + @Test + void addFilm() { + actorService.deleteAllActors(); + filmService.findAllFilms(); + final Actor actor = actorService.addActor("Victor", "Ivanov"); + final Film film = filmService.addFilm("Terminator 2"); + + filmService.addActor(film.getId(), actor.getId()); + + final Actor findActor = actorService.findActor(actor.getId()); + Assertions.assertEquals(findActor.getFilms().get(0).getId(), film.getId()); + } } diff --git a/src/test/java/ru/ip/labs/labs/JpaFilmsTest.java b/src/test/java/ru/ip/labs/labs/JpaFilmsTest.java index 27629c2..9786393 100644 --- a/src/test/java/ru/ip/labs/labs/JpaFilmsTest.java +++ b/src/test/java/ru/ip/labs/labs/JpaFilmsTest.java @@ -5,8 +5,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import ru.ip.labs.labs.films.models.Actor; import ru.ip.labs.labs.films.models.Film; +import ru.ip.labs.labs.films.models.Genre; +import ru.ip.labs.labs.films.service.ActorService; import ru.ip.labs.labs.films.service.FilmsService; +import ru.ip.labs.labs.films.service.GenreService; import javax.persistence.EntityNotFoundException; import java.util.List; @@ -17,6 +21,12 @@ public class JpaFilmsTest { @Autowired FilmsService filmService; + @Autowired + GenreService genreService; + + @Autowired + ActorService actorService; + @Test void testFilmCreate() { filmService.deleteAllFilms(); @@ -26,7 +36,7 @@ public class JpaFilmsTest { } @Test - void testStudentReadNotFound() { + void testFilmReadNotFound() { filmService.deleteAllFilms(); Assertions.assertThrows(EntityNotFoundException.class, () -> filmService.findFilm(-1L)); } @@ -41,6 +51,16 @@ public class JpaFilmsTest { Assertions.assertEquals(film.toString(), findFilm.toString()); } + @Test + void updateFilm() { + filmService.deleteAllFilms(); + final Film film = filmService.addFilm("Terminator 2"); + filmService.updateFilm(film.getId(), "Terminator 3"); + final Film findFilm = filmService.findFilm(film.getId()); + log.info(findFilm.toString()); + Assertions.assertEquals(findFilm.getName(), "Terminator 3"); + } + @Test void testReadAll() { filmService.deleteAllFilms(); @@ -58,4 +78,61 @@ public class JpaFilmsTest { filmService.deleteFilm(film.getId()); Assertions.assertThrows(EntityNotFoundException.class, () -> filmService.findFilm(film.getId())); } + + @Test + void findFilmsByGenres() { + filmService.deleteAllFilms(); + genreService.deleteAllGenres(); + final Film film = filmService.addFilm("Terminator 2"); + filmService.addFilm("Terminator 3"); + final Genre genre = genreService.addGenre("Comedy"); + + filmService.addGenre(film.getId(), genre.getId()); + + final List films = filmService.findFilmByGenre(film.getId(), genre.getId()); + Assertions.assertEquals(films.size(), 1); + } + + @Test + void findFilmsByActor() { + filmService.deleteAllFilms(); + genreService.deleteAllGenres(); + final Film film = filmService.addFilm("Terminator 2"); + final Film film2 = filmService.addFilm("Terminator 3"); + filmService.addFilm("Terminator 4"); + final Actor actor = actorService.addActor("Victor", "Ivanov"); + final Actor actor2 = actorService.addActor("Victoria", "Ivanov"); + + filmService.addActor(film.getId(), actor.getId()); + filmService.addActor(film2.getId(), actor2.getId()); + + final List films = filmService.findFilmByActor(film.getId(), "Victor"); + Assertions.assertEquals(films.size(), 2); + } + + @Test + void addGenre() { + filmService.deleteAllFilms(); + genreService.deleteAllGenres(); + final Film film = filmService.addFilm("Terminator 2"); + final Genre genre = genreService.addGenre("Comedy"); + + filmService.addGenre(film.getId(), genre.getId()); + + final Film findFilm = filmService.findFilm(film.getId()); + Assertions.assertEquals(findFilm.getGenres().get(0).toString(), genre.toString()); + } + + @Test + void addActor() { + filmService.deleteAllFilms(); + genreService.deleteAllGenres(); + final Film film = filmService.addFilm("Terminator 2"); + final Actor actor = actorService.addActor("Victor", "Ivanov"); + + filmService.addActor(film.getId(), actor.getId()); + + final Film findFilm = filmService.findFilm(film.getId()); + Assertions.assertEquals(findFilm.getActors().get(0).getId(), actor.getId()); + } } diff --git a/src/test/java/ru/ip/labs/labs/JpaGenresTest.java b/src/test/java/ru/ip/labs/labs/JpaGenresTest.java index d09eb6b..2596689 100644 --- a/src/test/java/ru/ip/labs/labs/JpaGenresTest.java +++ b/src/test/java/ru/ip/labs/labs/JpaGenresTest.java @@ -1,4 +1,77 @@ package ru.ip.labs.labs; +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.ip.labs.labs.films.models.Film; +import ru.ip.labs.labs.films.models.Genre; +import ru.ip.labs.labs.films.service.FilmsService; +import ru.ip.labs.labs.films.service.GenreService; + +import javax.persistence.EntityNotFoundException; +import java.util.List; + +@SpringBootTest public class JpaGenresTest { + private static final Logger log = LoggerFactory.getLogger(JpaFilmsTest.class); + @Autowired + FilmsService filmService; + + @Autowired + GenreService genreService; + + @Test + void testGenreCreate() { + genreService.deleteAllGenres(); + final Genre genre = genreService.addGenre("Comedy"); + log.info(genre.toString()); + Assertions.assertNotNull(genre.getId()); + } + + @Test + void testGenreReadNotFound() { + genreService.deleteAllGenres(); + Assertions.assertThrows(EntityNotFoundException.class, () -> genreService.findGenre(-1L)); + } + + @Test + void findGenre() { + genreService.deleteAllGenres(); + final Genre genre = genreService.addGenre("Comedy"); + log.info(genre.toString()); + final Genre findGenre = genreService.findGenre(genre.getId()); + log.info(findGenre.toString()); + Assertions.assertEquals(genre.toString(), findGenre.toString()); + } + + @Test + void testReadAll() { + genreService.deleteAllGenres(); + final Genre genre = genreService.addGenre("Comedy"); + log.info(genre.toString()); + final List genres = genreService.findAllGenres(); + Assertions.assertEquals(genres.size(), 1); + } + + @Test + void updateGenre() { + genreService.deleteAllGenres(); + final Genre genre = genreService.addGenre("Comedy"); + genreService.updateGenre(genre.getId(), "Drama"); + final Genre findGenre = genreService.findGenre(genre.getId()); + log.info(findGenre.toString()); + Assertions.assertEquals(findGenre.getName(), "Drama"); + } + + @Test + void deleteGenre() { + genreService.deleteAllGenres(); + final Genre genre = genreService.addGenre("Comedy"); + log.info(genre.toString()); + genreService.deleteGenre(genre.getId()); + Assertions.assertThrows(EntityNotFoundException.class, () -> filmService.findFilm(genre.getId())); + } }