third lab
This commit is contained in:
parent
995e104e86
commit
e739ceadec
@ -25,7 +25,7 @@ public class GenreController {
|
||||
|
||||
@GetMapping("/")
|
||||
public List<Genre> getGenres() {
|
||||
return genreService.findAllFilms();
|
||||
return genreService.findAllGenres();
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
|
@ -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<Film> 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<Film> getFilms() {
|
||||
return films;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -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<Genre> genres;
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@JoinTable(name="actors_films",
|
||||
joinColumns = @JoinColumn(name="film_id"),
|
||||
inverseJoinColumns = @JoinColumn(name="actor_id")
|
||||
)
|
||||
private List<Actor> 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<Actor> getActors() {
|
||||
return actors;
|
||||
}
|
||||
|
||||
public List<Genre> getGenres() {
|
||||
return genres;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String res = "\nFilm{" +
|
||||
|
@ -9,9 +9,6 @@ public class Genre {
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "genres")
|
||||
private List<Film> films;
|
||||
|
||||
public Genre() {}
|
||||
public Genre(String name) {
|
||||
this.name = name;
|
||||
|
@ -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<Actor> 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);
|
||||
}
|
||||
|
||||
|
@ -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<Film> 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<Film> result = query.getResultList();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Film> 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<Film> result = query.getResultList();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Film> findAllFilms() {
|
||||
|
@ -38,9 +38,8 @@ public class GenreService {
|
||||
return genre;
|
||||
}
|
||||
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Genre> findAllFilms() {
|
||||
public List<Genre> findAllGenres() {
|
||||
return em.createQuery("select g from Genre g", Genre.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
@ -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<Actor> 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());
|
||||
}
|
||||
}
|
||||
|
@ -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<Film> 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<Film> 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());
|
||||
}
|
||||
}
|
||||
|
@ -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<Genre> 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()));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user