Compare commits

..

No commits in common. "4d916bdfe6810a9f5afb81028582f682fc583201" and "e739ceadec2271658731555da5a0b65098422732" have entirely different histories.

16 changed files with 163 additions and 543 deletions

View File

@ -1,43 +0,0 @@
package ru.ip.labs.labs.films.controller;
import org.springframework.web.bind.annotation.*;
import ru.ip.labs.labs.films.dto.ActorDTO;
import ru.ip.labs.labs.films.service.ActorService;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/actors")
public class ActorController {
private final ActorService actorService;
public ActorController(ActorService actorService) {
this.actorService = actorService;
}
@GetMapping("/{id}")
public ActorDTO getActor(@PathVariable Long id) {
return actorService.findActor(id);
}
@GetMapping("")
public List<ActorDTO> getActors() {
return actorService.findAllActors();
}
@PostMapping("")
public ActorDTO createActor(@RequestBody @Valid ActorDTO actor) {
return actorService.addActor(actor.getName(), actor.getSurname());
}
@PatchMapping("")
public ActorDTO updateActor(@PathVariable Long id, @RequestBody @Valid ActorDTO actor) {
return actorService.updateActor(id, actor.getName(), actor.getSurname());
}
@DeleteMapping("/{id}")
public ActorDTO deleteActor(@PathVariable Long id) {
return actorService.deleteActor(id);
}
}

View File

@ -1,59 +1,56 @@
package ru.ip.labs.labs.films.controller;
import org.springframework.web.bind.annotation.*;
import ru.ip.labs.labs.films.dto.FilmDTO;
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 javax.validation.Valid;
import java.util.Iterator;
import java.util.List;
@RestController
@RequestMapping("/films")
@RequestMapping("/film")
public class FilmController {
private final FilmsService filmService;
public FilmController(FilmsService filmService) {
this.filmService = filmService;
}
@GetMapping("/test")
public String test() {
return "Test request";
}
@GetMapping("/{id}")
public FilmDTO getFilm(@PathVariable Long id) {
public Film getFilm(@PathVariable Long id) {
return filmService.findFilm(id);
}
@GetMapping("")
public List<FilmDTO> getFilms() {
@GetMapping("/")
public List<Film> getFilms() {
return filmService.findAllFilms();
}
@PostMapping("")
public FilmDTO createFilm(@RequestBody @Valid FilmDTO film) {
FilmDTO result = filmService.addFilm(film.getName());
return filmService.updateGenres(result.getId(), film.getGenre());
@PostMapping("/")
public Film createFilm(@RequestParam String name) {
return filmService.addFilm(name);
}
@PatchMapping("/{id}")
public FilmDTO updateFilm(@PathVariable Long id,
@RequestBody @Valid FilmDTO film) {
FilmDTO result = filmService.updateFilm(id, film.getName());
return filmService.updateGenres(result.getId(), film.getGenre());
public Film updateFilm(@PathVariable Long id,
@RequestParam("firstName") String name) {
return filmService.updateFilm(id, name);
}
@PatchMapping("/add_genre/{id}")
public FilmDTO addGenre(@PathVariable Long id, @RequestParam Long genre_id) {
public Film addGenre(@PathVariable Long id, @RequestParam Long genre_id) {
return filmService.addGenre(id, genre_id);
}
@PatchMapping("/add_actor/{id}")
public FilmDTO addActor(@PathVariable Long id, @RequestParam Long actor_id) {
return filmService.addActor(id, actor_id);
}
@DeleteMapping("/{id}")
public FilmDTO deleteFilm(@PathVariable Long id) {
public Film deleteFilm(@PathVariable Long id) {
return filmService.deleteFilm(id);
}
}

View File

@ -1,18 +1,15 @@
package ru.ip.labs.labs.films.controller;
import org.springframework.web.bind.annotation.*;
import ru.ip.labs.labs.films.dto.FilmDTO;
import ru.ip.labs.labs.films.dto.GenreDTO;
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.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/genres")
@RequestMapping("/genre")
public class GenreController {
private final GenreService genreService;
@ -22,28 +19,28 @@ public class GenreController {
}
@GetMapping("/{id}")
public GenreDTO getGenre(@PathVariable Long id) {
public Genre getGenre(@PathVariable Long id) {
return genreService.findGenre(id);
}
@GetMapping("")
public List<GenreDTO> getGenres() {
@GetMapping("/")
public List<Genre> getGenres() {
return genreService.findAllGenres();
}
@PostMapping("")
public GenreDTO createGenre(@RequestBody @Valid GenreDTO genre) {
return genreService.addGenre(genre.getName());
@PostMapping("/")
public Genre createGenre(@RequestParam("name") String name) {
return genreService.addGenre(name);
}
@PatchMapping("/{id}")
public GenreDTO updateGenre(@PathVariable Long id,
@RequestBody @Valid GenreDTO genre) {
return genreService.updateGenre(id, genre.getName());
public Genre updateGenre(@PathVariable Long id,
@RequestParam("name") String name) {
return genreService.updateGenre(id, name);
}
@DeleteMapping("/{id}")
public GenreDTO deleteGenre(@PathVariable Long id) {
public Genre deleteGenre(@PathVariable Long id) {
return genreService.deleteGenre(id);
}
}

View File

@ -1,83 +0,0 @@
package ru.ip.labs.labs.films.dto;
import ru.ip.labs.labs.films.models.Actor;
import ru.ip.labs.labs.films.models.Film;
import javax.persistence.FetchType;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
public class ActorDTO {
private Long id;
private String name;
private String surname;
private String fullName;
private byte[] photo;
private List<Film> films;
public ActorDTO() {}
public ActorDTO(Actor actor) {
this.id = actor.getId();
this.name = actor.getName();
this.surname = actor.getSurname();
this.photo = actor.getPhoto();
this.fullName = this.name + this.surname;
}
public ActorDTO(Long id, String name, String surname, byte[] photo) {
this.id = id;
this.name = name;
this.surname = surname;
this.fullName = this.name + this.surname;
this.photo = photo;
}
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;
}
public void setSurname(String surname) {
this.surname = surname;
}
public void setName(String name) {
this.name = name;
}
public List<Film> getFilms() {
return films;
}
@Override
public String toString() {
String res = "\nFilm{" +
"id: " + id + "," +
"name: " + name + "," +
"surname: " + name + "," +
"films:" + (films == null ? "[]" : films.toString()) + "}"
;
return res;
}
}

View File

@ -1,74 +0,0 @@
package ru.ip.labs.labs.films.dto;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
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.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import java.util.List;
public class FilmDTO {
private Long id;
private String name;
private List<String> genre;
private List<Actor> actors;
public FilmDTO() {}
public FilmDTO(Film film) {
this.id = film.getId();
this.name = film.getName();
if(film.getGenres() == null) return;
this.genre = film.getGenres().stream().map(g -> {
return g.getName();
}).toList();
}
public FilmDTO(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addGenre(String g) {
genre.add(g);
}
public void addActor(Actor actor) {
actors.add(actor);
}
public List<Actor> getActors() {
return actors;
}
public List<String> getGenre() {
return genre;
}
@Override
public String toString() {
String res = "\nFilm{" +
"id: " + id + "," +
"name: " + name + "," +
"genres:" + (genre == null ? "[]" : genre.toString()) + "}";
return res;
}
}

View File

@ -1,39 +0,0 @@
package ru.ip.labs.labs.films.dto;
import ru.ip.labs.labs.films.models.Genre;
public class GenreDTO {
private Long id;
private String name;
public GenreDTO() {}
public GenreDTO(Genre genre) {
this.id = genre.getId();
this.name = genre.getName();
}
public GenreDTO(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Genre{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}

View File

@ -3,7 +3,6 @@ import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@ -48,20 +47,10 @@ public class Film {
}
public void addGenre(Genre g) {
if(genres == null) genres = new ArrayList<>();
genres.add(g);
}
public void deleteGenres() {
genres = null;
}
public void deleteActors() {
actors = null;
}
public void addActor(Actor actor) {
if(actors == null) actors = new ArrayList<>();
actors.add(actor);
}

View File

@ -1,8 +0,0 @@
package ru.ip.labs.labs.films.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ip.labs.labs.films.models.Actor;
public interface ActorRepository extends JpaRepository<Actor, Long> {
Actor findActorByNameAndSurname(String name, String surname);
}

View File

@ -1,7 +0,0 @@
package ru.ip.labs.labs.films.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ip.labs.labs.films.models.Film;
public interface FilmRepository extends JpaRepository<Film, Long> {
}

View File

@ -1,8 +0,0 @@
package ru.ip.labs.labs.films.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ip.labs.labs.films.models.Genre;
public interface GenreRepository extends JpaRepository<Genre, Long> {
Genre findByName(String name);
}

View File

@ -3,39 +3,34 @@ 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.dto.ActorDTO;
import ru.ip.labs.labs.films.models.Actor;
import ru.ip.labs.labs.films.models.Film;
import ru.ip.labs.labs.films.repository.ActorRepository;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
@Service
public class ActorService {
private ActorRepository repo;
public ActorService(ActorRepository repo) {
this.repo = repo;
}
@PersistenceContext
private EntityManager em;
@Transactional
public ActorDTO addActor(String name, String surname) {
public Actor addActor(String name, String surname) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Student name is null or empty");
}
Actor actor = new Actor(name, surname);
return new ActorDTO(repo.save(actor));
em.persist(actor);
return em.find(Actor.class, actor.getId());
}
@Transactional
public ActorDTO addActor(String name, String surname, String path) {
public Actor addActor(String name, String surname, String path) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Student name is null or empty");
}
@ -44,58 +39,47 @@ public class ActorService {
try {
actor.setPhoto(path);
} catch(IOException err) {
// System.out.println(err.getMessage());
throw new RuntimeException(err);
System.out.println(err.getMessage());
}
return new ActorDTO(repo.save(actor));
em.persist(actor);
return em.find(Actor.class, actor.getId());
}
@Transactional(readOnly = true)
public ActorDTO findActor(Long id) {
final Optional<Actor> actor = repo.findById(id);
if (actor.isEmpty()) {
public Actor findActor(Long id) {
final Actor actor = em.find(Actor.class, id);
if (actor == null) {
throw new EntityNotFoundException(String.format("Actor with id [%s] is not found", id));
}
return new ActorDTO(actor.get());
return actor;
}
@Transactional(readOnly = true)
public List<ActorDTO> findAllActors() {
return repo.findAll().stream().map(x -> new ActorDTO(x)).toList();
public List<Actor> findAllActors() {
return em.createQuery("select f from Actor f", Actor.class)
.getResultList();
}
@Transactional
public ActorDTO updateActor(Long id, String name, String surname) {
public Actor updateActor(Long id, String name, String surname) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Actor name is null or empty");
}
final Optional<Actor> currentActorOpt = repo.findById(id);
if(currentActorOpt.isEmpty()) {
return null;
}
final Actor currentActor = currentActorOpt.get();
final Actor currentActor = findActor(id);
if(name != null) currentActor.setName(name);
if(surname != null) currentActor.setSurname(surname);
return new ActorDTO(repo.save(currentActor));
return em.merge(currentActor);
}
@Transactional
public ActorDTO deleteActor(Long id) {
final Optional<Actor> currentActor = repo.findById(id);
if(currentActor.isEmpty()) {
return null;
}
repo.deleteById(id);
return new ActorDTO(currentActor.get());
public Actor deleteActor(Long id) {
final Actor currentActor = findActor(id);
em.remove(currentActor);
return currentActor;
}
@Transactional
public void deleteAllActors() {
repo.deleteAll();
em.createQuery("delete from Actor").executeUpdate();
}
}

View File

@ -3,13 +3,9 @@ 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.dto.FilmDTO;
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.repository.ActorRepository;
import ru.ip.labs.labs.films.repository.FilmRepository;
import ru.ip.labs.labs.films.repository.GenreRepository;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
@ -17,7 +13,6 @@ import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
@Service
public class FilmsService {
@ -25,166 +20,111 @@ public class FilmsService {
@PersistenceContext
private EntityManager em;
private FilmRepository repo;
private GenreRepository repoGenre;
private ActorRepository repoActor;
public FilmsService(FilmRepository repo, GenreRepository repoGenre, ActorRepository repoActor) {
this.repo = repo;
this.repoGenre = repoGenre;
this.repoActor = repoActor;
}
@Transactional
public FilmDTO addFilm(String name) {
public Film addFilm(String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Student name is null or empty");
}
Film film = new Film(name);
return new FilmDTO(repo.save(film));
em.persist(film);
return em.find(Film.class, film.getId());
}
// фильмы по жанру
// фильмы по актеру
@Transactional
public FilmDTO addGenre(Long filmId, Long genreId) {
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));
}
final Optional<Genre> genre = repoGenre.findById(genreId);
if (genre.isEmpty()) {
final Genre genre = em.find(Genre.class, genreId);
if (genre == null) {
throw new EntityNotFoundException(String.format("Genre with id [%s] is not found", genreId));
}
film.addGenre(genre.get());
return new FilmDTO(repo.save(film));
film.addGenre(genre);
return em.merge(film);
}
@Transactional
public FilmDTO updateGenres(Long filmId, List<String> genres) {
final Film film = em.find(Film.class, filmId);
if (film == null) {
throw new EntityNotFoundException(String.format("Film with id [%s] is not found", filmId));
}
film.deleteGenres();
for(String g : genres) {
Genre genre = repoGenre.findByName(g);
film.addGenre(genre);
}
return new FilmDTO(repo.save(film));
}
@Transactional
public FilmDTO updateActors(Long filmId, List<String> fullNames) {
final Film film = em.find(Film.class, filmId);
if (film == null) {
throw new EntityNotFoundException(String.format("Film with id [%s] is not found", filmId));
}
film.deleteActors();
for(String fn : fullNames) {
String arr[] = fn.split(" ");
Actor actor = repoActor.findActorByNameAndSurname(arr[0], arr[1]);
film.addActor(actor);
}
return new FilmDTO(repo.save(film));
}
@Transactional
public FilmDTO addActor(Long filmId, Long actorId) {
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 Optional<Actor> actor = repoActor.findById(actorId);
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.get());
return new FilmDTO(repo.save(film));
film.addActor(actor);
return em.merge(film);
}
@Transactional(readOnly = true)
public FilmDTO findFilm(Long id) {
final Optional<Film> film = repo.findById(id);
if (film.isEmpty()) {
public Film findFilm(Long id) {
final Film film = em.find(Film.class, id);
if (film == null) {
throw new EntityNotFoundException(String.format("Film with id [%s] is not found", id));
}
return new FilmDTO(film.get());
return film;
}
@Transactional(readOnly = true)
public List<FilmDTO> findFilmByGenre(Long id, Long genreId) {
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.stream().map(x -> new FilmDTO(x)).toList();
return result;
}
@Transactional(readOnly = true)
public List<FilmDTO> findFilmByActor(Long id, String name) {
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.stream().map(x -> new FilmDTO(x)).toList();
return result;
}
@Transactional(readOnly = true)
public List<FilmDTO> findAllFilms() {
return repo.findAll().stream().map(x -> new FilmDTO(x)).toList();
public List<Film> findAllFilms() {
return em.createQuery("select f from Film f", Film.class)
.getResultList();
}
@Transactional
public FilmDTO updateFilm(Long id, String name) {
public Film updateFilm(Long id, String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Film name is null or empty");
}
final Optional<Film> currentFilmOpt = repo.findById(id);
if(currentFilmOpt.isEmpty()) {
return null;
}
final Film currentFilm = currentFilmOpt.get();
final Film currentFilm = findFilm(id);
currentFilm.setName(name);
return new FilmDTO(repo.save(currentFilm));
return em.merge(currentFilm);
}
@Transactional
public FilmDTO deleteFilm(Long id) {
final Optional<Film> currentFilm = repo.findById(id);
if(currentFilm.isEmpty()) {
return null;
}
repo.deleteById(id);
return new FilmDTO(currentFilm.get());
public Film deleteFilm(Long id) {
final Film currentFilm = findFilm(id);
em.remove(currentFilm);
return currentFilm;
}
@Transactional
public void deleteAllFilms() {
repo.deleteAll();
em.createQuery("delete from Film").executeUpdate();
}
}

View File

@ -3,80 +3,66 @@ 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.dto.GenreDTO;
import ru.ip.labs.labs.films.models.Film;
import ru.ip.labs.labs.films.models.Genre;
import ru.ip.labs.labs.films.repository.ActorRepository;
import ru.ip.labs.labs.films.repository.GenreRepository;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.Optional;
@Service
public class GenreService {
private GenreRepository repo;
@PersistenceContext
private EntityManager em;
public GenreService(GenreRepository repo) {
this.repo = repo;
}
@Transactional
public GenreDTO addGenre(String name) {
public Genre addGenre(String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Genre name is null or empty");
}
Genre genre = new Genre(name);
return new GenreDTO(repo.save(genre));
em.persist(genre);
return em.find(Genre.class, genre.getId());
}
@Transactional(readOnly = true)
public GenreDTO findGenre(Long id) {
final Optional<Genre> genre = repo.findById(id);
if (genre.isEmpty()) {
public Genre findGenre(Long id) {
final Genre genre = em.find(Genre.class, id);
if (genre == null) {
throw new EntityNotFoundException(String.format("Genre with id [%s] is not found", id));
}
return new GenreDTO(genre.get());
return genre;
}
@Transactional(readOnly = true)
public List<GenreDTO> findAllGenres() {
return repo.findAll().stream().map(x -> new GenreDTO(x)).toList();
public List<Genre> findAllGenres() {
return em.createQuery("select g from Genre g", Genre.class)
.getResultList();
}
@Transactional
public GenreDTO updateGenre(Long id, String name) {
public Genre updateGenre(Long id, String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Film name is null or empty");
}
final Optional<Genre> currentGenreOpt = repo.findById(id);
if(currentGenreOpt.isEmpty()) {
return null;
}
final Genre currentGenre = currentGenreOpt.get();
final Genre currentGenre = findGenre(id);
currentGenre.setName(name);
return new GenreDTO(repo.save(currentGenre));
return em.merge(currentGenre);
}
@Transactional
public GenreDTO deleteGenre(Long id) {
Optional<Genre> genre = repo.findById(id);
if(genre.isEmpty()) {
return null;
}
repo.deleteById(id);
return new GenreDTO(genre.get());
public Genre deleteGenre(Long id) {
final Genre currentGenre = findGenre(id);
em.remove(currentGenre);
return currentGenre;
}
@Transactional
public void deleteAllGenres() {
repo.deleteAll();
em.createQuery("delete from Genre").executeUpdate();
}
}

View File

@ -6,8 +6,6 @@ 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.dto.ActorDTO;
import ru.ip.labs.labs.films.dto.FilmDTO;
import ru.ip.labs.labs.films.models.Actor;
import ru.ip.labs.labs.films.models.Film;
import ru.ip.labs.labs.films.models.Genre;
@ -28,57 +26,52 @@ public class JpaActorsTest {
ActorService actorService;
@Test
void testActorCreate() {
filmService.deleteAllFilms();
void testFilmCreate() {
actorService.deleteAllActors();
final ActorDTO actor = actorService.addActor("Victor", "Ivanov");
final Actor actor = actorService.addActor("Victor", "Ivanov");
log.info(actor.toString());
Assertions.assertNotNull(actor.getId());
}
@Test
void testActorReadNotFound() {
void testFilmReadNotFound() {
actorService.deleteAllActors();
Assertions.assertThrows(EntityNotFoundException.class, () -> actorService.findActor(-1L));
}
@Test
void findActor() {
filmService.deleteAllFilms();
void findFilm() {
actorService.deleteAllActors();
final ActorDTO actor = actorService.addActor("Victor", "Ivanov");
final Actor actor = actorService.addActor("Victor", "Ivanov");
log.info(actor.toString());
final ActorDTO findActor = actorService.findActor(actor.getId());
final Actor findActor = actorService.findActor(actor.getId());
log.info(findActor.toString());
Assertions.assertEquals(actor.toString(), findActor.toString());
}
@Test
void testReadAll() {
filmService.deleteAllFilms();
actorService.deleteAllActors();
final ActorDTO actor = actorService.addActor("Victor", "Ivanov");
final Actor actor = actorService.addActor("Victor", "Ivanov");
log.info(actor.toString());
final List<ActorDTO> actors = actorService.findAllActors();
final List<Actor> actors = actorService.findAllActors();
Assertions.assertEquals(actors.size(), 1);
}
@Test
void updateActor() {
filmService.deleteAllFilms();
actorService.deleteAllActors();
final ActorDTO actor = actorService.addActor("Victor", "Ivanov");
final Actor actor = actorService.addActor("Victor", "Ivanov");
actorService.updateActor(actor.getId(), "Terminator", null);
final ActorDTO findActor = actorService.findActor(actor.getId());
final Actor findActor = actorService.findActor(actor.getId());
log.info(findActor.toString());
Assertions.assertEquals(findActor.getName() + " " + findActor.getSurname(), "Terminator Ivanov");
}
@Test
void deleteActor() {
filmService.deleteAllFilms();
actorService.deleteAllActors();
final ActorDTO actor = actorService.addActor("Victor", "Ivanov");
final Actor actor = actorService.addActor("Victor", "Ivanov");
log.info(actor.toString());
actorService.deleteActor(actor.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> actorService.findActor(actor.getId()));
@ -86,23 +79,22 @@ public class JpaActorsTest {
@Test
void addPhoto() {
filmService.deleteAllFilms();
actorService.deleteAllActors();
final ActorDTO actor = actorService.addActor("Victor", "Ivanov", "C:\\Users\\parap\\OneDrive\\Рабочий стол\\work\\ferrari.jpg");
ActorDTO findActor = actorService.findActor(actor.getId());
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() {
filmService.findAllFilms();
actorService.deleteAllActors();
final ActorDTO actor = actorService.addActor("Victor", "Ivanov");
final FilmDTO film = filmService.addFilm("Terminator 2");
filmService.findAllFilms();
final Actor actor = actorService.addActor("Victor", "Ivanov");
final Film film = filmService.addFilm("Terminator 2");
filmService.addActor(film.getId(), actor.getId());
final ActorDTO findActor = actorService.findActor(actor.getId());
final Actor findActor = actorService.findActor(actor.getId());
Assertions.assertEquals(findActor.getFilms().get(0).getId(), film.getId());
}
}

View File

@ -5,8 +5,6 @@ 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.dto.ActorDTO;
import ru.ip.labs.labs.films.dto.FilmDTO;
import ru.ip.labs.labs.films.models.Actor;
import ru.ip.labs.labs.films.models.Film;
import ru.ip.labs.labs.films.models.Genre;
@ -32,7 +30,7 @@ public class JpaFilmsTest {
@Test
void testFilmCreate() {
filmService.deleteAllFilms();
final FilmDTO film = filmService.addFilm("Terminator 2");
final Film film = filmService.addFilm("Terminator 2");
log.info(film.toString());
Assertions.assertNotNull(film.getId());
}
@ -46,9 +44,9 @@ public class JpaFilmsTest {
@Test
void findFilm() {
filmService.deleteAllFilms();
final FilmDTO film = filmService.addFilm("Terminator 2");
final Film film = filmService.addFilm("Terminator 2");
log.info(film.toString());
final FilmDTO findFilm = filmService.findFilm(film.getId());
final Film findFilm = filmService.findFilm(film.getId());
log.info(findFilm.toString());
Assertions.assertEquals(film.toString(), findFilm.toString());
}
@ -56,9 +54,9 @@ public class JpaFilmsTest {
@Test
void updateFilm() {
filmService.deleteAllFilms();
final FilmDTO film = filmService.addFilm("Terminator 2");
final Film film = filmService.addFilm("Terminator 2");
filmService.updateFilm(film.getId(), "Terminator 3");
final FilmDTO findFilm = filmService.findFilm(film.getId());
final Film findFilm = filmService.findFilm(film.getId());
log.info(findFilm.toString());
Assertions.assertEquals(findFilm.getName(), "Terminator 3");
}
@ -66,16 +64,16 @@ public class JpaFilmsTest {
@Test
void testReadAll() {
filmService.deleteAllFilms();
final FilmDTO film = filmService.addFilm("Terminator 2");
final Film film = filmService.addFilm("Terminator 2");
log.info(film.toString());
final List<FilmDTO> films = filmService.findAllFilms();
final List<Film> films = filmService.findAllFilms();
Assertions.assertEquals(films.size(), 1);
}
@Test
void deleteFilm() {
filmService.deleteAllFilms();
final FilmDTO film = filmService.addFilm("Terminator 2");
final Film film = filmService.addFilm("Terminator 2");
log.info(film.toString());
filmService.deleteFilm(film.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> filmService.findFilm(film.getId()));
@ -85,30 +83,30 @@ public class JpaFilmsTest {
void findFilmsByGenres() {
filmService.deleteAllFilms();
genreService.deleteAllGenres();
// final FilmDTO film = filmService.addFilm("Terminator 2");
// filmService.addFilm("Terminator 3");
// final Genre genre = genreService.addGenre("Comedy");
//
// filmService.addGenre(film.getId(), genre.getId());
//
// final List<FilmDTO> films = filmService.findFilmByGenre(film.getId(), genre.getId());
// Assertions.assertEquals(films.size(), 1);
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 FilmDTO film = filmService.addFilm("Terminator 2");
final FilmDTO film2 = filmService.addFilm("Terminator 3");
final Film film = filmService.addFilm("Terminator 2");
final Film film2 = filmService.addFilm("Terminator 3");
filmService.addFilm("Terminator 4");
final ActorDTO actor = actorService.addActor("Victor", "Ivanov");
final ActorDTO actor2 = actorService.addActor("Victoria", "Ivanov");
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<FilmDTO> films = filmService.findFilmByActor(film.getId(), "Victor");
final List<Film> films = filmService.findFilmByActor(film.getId(), "Victor");
Assertions.assertEquals(films.size(), 2);
}
@ -116,25 +114,25 @@ public class JpaFilmsTest {
void addGenre() {
filmService.deleteAllFilms();
genreService.deleteAllGenres();
final FilmDTO film = filmService.addFilm("Terminator 2");
// final Genre genre = genreService.addGenre("Comedy");
//
// filmService.addGenre(film.getId(), genre.getId());
//
// final FilmDTO findFilm = filmService.findFilm(film.getId());
// Assertions.assertEquals(findFilm.getGenre().get(0).toString(), genre.toString());
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 FilmDTO film = filmService.addFilm("Terminator 2");
final ActorDTO actor = actorService.addActor("Victor", "Ivanov");
final Film film = filmService.addFilm("Terminator 2");
final Actor actor = actorService.addActor("Victor", "Ivanov");
filmService.addActor(film.getId(), actor.getId());
final FilmDTO findFilm = filmService.findFilm(film.getId());
final Film findFilm = filmService.findFilm(film.getId());
Assertions.assertEquals(findFilm.getActors().get(0).getId(), actor.getId());
}
}

View File

@ -6,7 +6,6 @@ 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.dto.GenreDTO;
import ru.ip.labs.labs.films.models.Film;
import ru.ip.labs.labs.films.models.Genre;
import ru.ip.labs.labs.films.service.FilmsService;
@ -27,7 +26,7 @@ public class JpaGenresTest {
@Test
void testGenreCreate() {
genreService.deleteAllGenres();
final GenreDTO genre = genreService.addGenre("Comedy");
final Genre genre = genreService.addGenre("Comedy");
log.info(genre.toString());
Assertions.assertNotNull(genre.getId());
}
@ -41,9 +40,9 @@ public class JpaGenresTest {
@Test
void findGenre() {
genreService.deleteAllGenres();
final GenreDTO genre = genreService.addGenre("Comedy");
final Genre genre = genreService.addGenre("Comedy");
log.info(genre.toString());
final GenreDTO findGenre = genreService.findGenre(genre.getId());
final Genre findGenre = genreService.findGenre(genre.getId());
log.info(findGenre.toString());
Assertions.assertEquals(genre.toString(), findGenre.toString());
}
@ -51,18 +50,18 @@ public class JpaGenresTest {
@Test
void testReadAll() {
genreService.deleteAllGenres();
final GenreDTO genre = genreService.addGenre("Comedy");
final Genre genre = genreService.addGenre("Comedy");
log.info(genre.toString());
final List<GenreDTO> genres = genreService.findAllGenres();
final List<Genre> genres = genreService.findAllGenres();
Assertions.assertEquals(genres.size(), 1);
}
@Test
void updateGenre() {
genreService.deleteAllGenres();
final GenreDTO genre = genreService.addGenre("Comedy");
final Genre genre = genreService.addGenre("Comedy");
genreService.updateGenre(genre.getId(), "Drama");
final GenreDTO findGenre = genreService.findGenre(genre.getId());
final Genre findGenre = genreService.findGenre(genre.getId());
log.info(findGenre.toString());
Assertions.assertEquals(findGenre.getName(), "Drama");
}
@ -70,7 +69,7 @@ public class JpaGenresTest {
@Test
void deleteGenre() {
genreService.deleteAllGenres();
final GenreDTO genre = genreService.addGenre("Comedy");
final Genre genre = genreService.addGenre("Comedy");
log.info(genre.toString());
genreService.deleteGenre(genre.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> filmService.findFilm(genre.getId()));