third lab

This commit is contained in:
parap 2023-04-17 12:53:57 +03:00
parent 995e104e86
commit e739ceadec
10 changed files with 360 additions and 31 deletions

View File

@ -25,7 +25,7 @@ public class GenreController {
@GetMapping("/") @GetMapping("/")
public List<Genre> getGenres() { public List<Genre> getGenres() {
return genreService.findAllFilms(); return genreService.findAllGenres();
} }
@PostMapping("/") @PostMapping("/")

View File

@ -1,5 +1,8 @@
package ru.ip.labs.labs.films.models; package ru.ip.labs.labs.films.models;
import javax.persistence.*; import javax.persistence.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List; import java.util.List;
@Entity @Entity
@ -9,24 +12,36 @@ public class Actor {
private Long id; private Long id;
private String name; private String name;
private String surname; private String surname;
@Lob
private byte[] photo;
@ManyToMany(fetch = FetchType.EAGER) @ManyToMany(fetch = FetchType.EAGER, mappedBy = "actors")
@JoinTable(name="actors_films",
joinColumns = @JoinColumn(name="actor_id"),
inverseJoinColumns = @JoinColumn(name="film_id")
)
private List<Film> films; private List<Film> films;
public Actor() {} public Actor() {}
public Actor(String name) { public Actor(String name, String surname) {
this.name = name; 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() { public Long getId() {
return id; return id;
} }
public String getName() {
return name;
}
public String getSurname() { public String getSurname() {
return surname; return surname;
} }
@ -39,8 +54,8 @@ public class Actor {
this.name = name; this.name = name;
} }
public void addFilm(Film f) { public List<Film> getFilms() {
films.add(f); return films;
} }
@Override @Override

View File

@ -1,4 +1,7 @@
package ru.ip.labs.labs.films.models; package ru.ip.labs.labs.films.models;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import javax.persistence.*; import javax.persistence.*;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -10,13 +13,21 @@ public class Film {
private Long id; private Long id;
private String name; private String name;
@ManyToMany(fetch = FetchType.EAGER) @ManyToMany
@LazyCollection(LazyCollectionOption.FALSE)
@JoinTable(name="films_genres", @JoinTable(name="films_genres",
joinColumns = @JoinColumn(name="film_id"), joinColumns = @JoinColumn(name="film_id"),
inverseJoinColumns = @JoinColumn(name="genre_id") inverseJoinColumns = @JoinColumn(name="genre_id")
) )
private List<Genre> genres; 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() {}
public Film(String name) { public Film(String name) {
@ -27,6 +38,10 @@ public class Film {
return id; return id;
} }
public String getName() {
return name;
}
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
@ -35,6 +50,18 @@ public class Film {
genres.add(g); genres.add(g);
} }
public void addActor(Actor actor) {
actors.add(actor);
}
public List<Actor> getActors() {
return actors;
}
public List<Genre> getGenres() {
return genres;
}
@Override @Override
public String toString() { public String toString() {
String res = "\nFilm{" + String res = "\nFilm{" +

View File

@ -9,9 +9,6 @@ public class Genre {
private Long id; private Long id;
private String name; private String name;
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "genres")
private List<Film> films;
public Genre() {} public Genre() {}
public Genre(String name) { public Genre(String name) {
this.name = name; this.name = name;

View File

@ -9,6 +9,7 @@ import ru.ip.labs.labs.films.models.Film;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException; import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
import java.io.IOException;
import java.util.List; import java.util.List;
@Service @Service
@ -23,26 +24,25 @@ public class ActorService {
throw new IllegalArgumentException("Student name is null or empty"); throw new IllegalArgumentException("Student name is null or empty");
} }
Actor actor = new Actor(name); Actor actor = new Actor(name, surname);
em.persist(actor); em.persist(actor);
return em.find(Actor.class, actor.getId()); return em.find(Actor.class, actor.getId());
} }
@Transactional @Transactional
public Actor addFilm(Long actorId, Long filmId) { public Actor addActor(String name, String surname, String path) {
final Actor actor = em.find(Actor.class, actorId); if (!StringUtils.hasText(name)) {
if (actor == null) { throw new IllegalArgumentException("Student name is null or empty");
throw new EntityNotFoundException(String.format("Actor with id [%s] is not found", actorId));
} }
final Film film = em.find(Film.class, filmId); Actor actor = new Actor(name, surname);
if (film == null) { try {
throw new EntityNotFoundException(String.format("Film with id [%s] is not found", filmId)); actor.setPhoto(path);
} catch(IOException err) {
System.out.println(err.getMessage());
} }
em.persist(actor);
actor.addFilm(film); return em.find(Actor.class, actor.getId());
return em.merge(actor);
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
@ -54,7 +54,6 @@ public class ActorService {
return actor; return actor;
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<Actor> findAllActors() { public List<Actor> findAllActors() {
return em.createQuery("select f from Actor f", Actor.class) return em.createQuery("select f from Actor f", Actor.class)
@ -62,12 +61,13 @@ public class ActorService {
} }
@Transactional @Transactional
public Actor updateActor(Long id, String name) { public Actor updateActor(Long id, String name, String surname) {
if (!StringUtils.hasText(name)) { if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Actor name is null or empty"); throw new IllegalArgumentException("Actor name is null or empty");
} }
final Actor currentActor = findActor(id); final Actor currentActor = findActor(id);
currentActor.setName(name); if(name != null) currentActor.setName(name);
if(surname != null) currentActor.setSurname(surname);
return em.merge(currentActor); return em.merge(currentActor);
} }

View File

@ -3,12 +3,15 @@ package ru.ip.labs.labs.films.service;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; 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.Film;
import ru.ip.labs.labs.films.models.Genre; import ru.ip.labs.labs.films.models.Genre;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException; import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.time.LocalDate;
import java.util.List; import java.util.List;
@Service @Service
@ -29,9 +32,13 @@ public class FilmsService {
return em.find(Film.class, film.getId()); return em.find(Film.class, film.getId());
} }
// фильмы по жанру
// фильмы по актеру
@Transactional @Transactional
public Film addGenre(Long filmId, Long genreId) { public Film addGenre(Long filmId, Long genreId) {
final Film film = em.find(Film.class, filmId); final Film film = em.find(Film.class, filmId);
if (film == null) { if (film == null) {
throw new EntityNotFoundException(String.format("Film with id [%s] is not found", filmId)); throw new EntityNotFoundException(String.format("Film with id [%s] is not found", filmId));
} }
@ -45,6 +52,22 @@ public class FilmsService {
return em.merge(film); 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) @Transactional(readOnly = true)
public Film findFilm(Long id) { public Film findFilm(Long id) {
final Film film = em.find(Film.class, id); final Film film = em.find(Film.class, id);
@ -54,6 +77,28 @@ public class FilmsService {
return film; 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) @Transactional(readOnly = true)
public List<Film> findAllFilms() { public List<Film> findAllFilms() {

View File

@ -38,9 +38,8 @@ public class GenreService {
return genre; return genre;
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<Genre> findAllFilms() { public List<Genre> findAllGenres() {
return em.createQuery("select g from Genre g", Genre.class) return em.createQuery("select g from Genre g", Genre.class)
.getResultList(); .getResultList();
} }

View File

@ -1,4 +1,100 @@
package ru.ip.labs.labs; 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 { 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());
}
} }

View File

@ -5,8 +5,12 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; 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.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.FilmsService;
import ru.ip.labs.labs.films.service.GenreService;
import javax.persistence.EntityNotFoundException; import javax.persistence.EntityNotFoundException;
import java.util.List; import java.util.List;
@ -17,6 +21,12 @@ public class JpaFilmsTest {
@Autowired @Autowired
FilmsService filmService; FilmsService filmService;
@Autowired
GenreService genreService;
@Autowired
ActorService actorService;
@Test @Test
void testFilmCreate() { void testFilmCreate() {
filmService.deleteAllFilms(); filmService.deleteAllFilms();
@ -26,7 +36,7 @@ public class JpaFilmsTest {
} }
@Test @Test
void testStudentReadNotFound() { void testFilmReadNotFound() {
filmService.deleteAllFilms(); filmService.deleteAllFilms();
Assertions.assertThrows(EntityNotFoundException.class, () -> filmService.findFilm(-1L)); Assertions.assertThrows(EntityNotFoundException.class, () -> filmService.findFilm(-1L));
} }
@ -41,6 +51,16 @@ public class JpaFilmsTest {
Assertions.assertEquals(film.toString(), findFilm.toString()); 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 @Test
void testReadAll() { void testReadAll() {
filmService.deleteAllFilms(); filmService.deleteAllFilms();
@ -58,4 +78,61 @@ public class JpaFilmsTest {
filmService.deleteFilm(film.getId()); filmService.deleteFilm(film.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> filmService.findFilm(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());
}
} }

View File

@ -1,4 +1,77 @@
package ru.ip.labs.labs; 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 { 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()));
}
} }