third lab
This commit is contained in:
parent
e3ddc3f4b3
commit
995e104e86
1
.gitignore
vendored
1
.gitignore
vendored
@ -9,6 +9,7 @@ build/
|
|||||||
.apt_generated
|
.apt_generated
|
||||||
.classpath
|
.classpath
|
||||||
.factorypath
|
.factorypath
|
||||||
|
|
||||||
.project
|
.project
|
||||||
.settings
|
.settings
|
||||||
.springBeans
|
.springBeans
|
||||||
|
@ -14,6 +14,12 @@ repositories {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
|
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
|
implementation 'com.h2database:h2:2.1.210'
|
||||||
|
|
||||||
|
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
|
||||||
|
|
||||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,11 +2,6 @@ package ru.ip.labs.labs;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class LabsApplication {
|
public class LabsApplication {
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
package ru.ip.labs.labs.calculator.controller;
|
package ru.ip.labs.labs.calculator.controller;
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import ru.ip.labs.labs.calculator.service.CalculatorService;
|
import ru.ip.labs.labs.calculator.service.CalculatorService;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@RequestMapping("/test")
|
||||||
public class CalcController {
|
public class CalcController {
|
||||||
private final CalculatorService calculatorService;
|
private final CalculatorService calculatorService;
|
||||||
|
|
||||||
@ -13,6 +15,11 @@ public class CalcController {
|
|||||||
this.calculatorService = calculatorService;
|
this.calculatorService = calculatorService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/hello")
|
||||||
|
public String hello() {
|
||||||
|
return "Hi man";
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/sumInt")
|
@GetMapping("/sumInt")
|
||||||
public int sumInt(@RequestParam int x, @RequestParam int y) {
|
public int sumInt(@RequestParam int x, @RequestParam int y) {
|
||||||
return calculatorService.getSum(x, y, "int");
|
return calculatorService.getSum(x, y, "int");
|
||||||
|
@ -2,7 +2,7 @@ package ru.ip.labs.labs.calculator.domain;
|
|||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component(value = "array")
|
@Component(value = "testDatabase")
|
||||||
public class CalculatorArray implements Calculator<String> {
|
public class CalculatorArray implements Calculator<String> {
|
||||||
@Override
|
@Override
|
||||||
public String sum(String x, int y) {
|
public String sum(String x, int y) {
|
||||||
|
@ -18,6 +18,7 @@ public class CalculatorService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public <T> T getDiff(T x, int y, String opType) {
|
public <T> T getDiff(T x, int y, String opType) {
|
||||||
|
|
||||||
Calculator<T> calc = (Calculator<T>)_applicationContext.getBean(opType);
|
Calculator<T> calc = (Calculator<T>)_applicationContext.getBean(opType);
|
||||||
return calc.diff(x, y);
|
return calc.diff(x, y);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,56 @@
|
|||||||
|
package ru.ip.labs.labs.films.controller;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import ru.ip.labs.labs.films.models.Film;
|
||||||
|
import ru.ip.labs.labs.films.service.FilmsService;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@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 Film getFilm(@PathVariable Long id) {
|
||||||
|
return filmService.findFilm(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/")
|
||||||
|
public List<Film> getFilms() {
|
||||||
|
return filmService.findAllFilms();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/")
|
||||||
|
public Film createFilm(@RequestParam String name) {
|
||||||
|
return filmService.addFilm(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PatchMapping("/{id}")
|
||||||
|
public Film updateFilm(@PathVariable Long id,
|
||||||
|
@RequestParam("firstName") String name) {
|
||||||
|
return filmService.updateFilm(id, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PatchMapping("/add_genre/{id}")
|
||||||
|
public Film addGenre(@PathVariable Long id, @RequestParam Long genre_id) {
|
||||||
|
return filmService.addGenre(id, genre_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public Film deleteFilm(@PathVariable Long id) {
|
||||||
|
return filmService.deleteFilm(id);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
package ru.ip.labs.labs.films.controller;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
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 java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/genre")
|
||||||
|
public class GenreController {
|
||||||
|
private final GenreService genreService;
|
||||||
|
|
||||||
|
|
||||||
|
public GenreController(GenreService genreService) {
|
||||||
|
this.genreService = genreService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public Genre getGenre(@PathVariable Long id) {
|
||||||
|
return genreService.findGenre(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/")
|
||||||
|
public List<Genre> getGenres() {
|
||||||
|
return genreService.findAllFilms();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/")
|
||||||
|
public Genre createGenre(@RequestParam("name") String name) {
|
||||||
|
return genreService.addGenre(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PatchMapping("/{id}")
|
||||||
|
public Genre updateGenre(@PathVariable Long id,
|
||||||
|
@RequestParam("name") String name) {
|
||||||
|
return genreService.updateGenre(id, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public Genre deleteGenre(@PathVariable Long id) {
|
||||||
|
return genreService.deleteGenre(id);
|
||||||
|
}
|
||||||
|
}
|
56
src/main/java/ru/ip/labs/labs/films/models/Actor.java
Normal file
56
src/main/java/ru/ip/labs/labs/films/models/Actor.java
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package ru.ip.labs.labs.films.models;
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Actor {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
private String name;
|
||||||
|
private String surname;
|
||||||
|
|
||||||
|
@ManyToMany(fetch = FetchType.EAGER)
|
||||||
|
@JoinTable(name="actors_films",
|
||||||
|
joinColumns = @JoinColumn(name="actor_id"),
|
||||||
|
inverseJoinColumns = @JoinColumn(name="film_id")
|
||||||
|
)
|
||||||
|
private List<Film> films;
|
||||||
|
|
||||||
|
public Actor() {}
|
||||||
|
|
||||||
|
public Actor(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSurname() {
|
||||||
|
return surname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSurname(String surname) {
|
||||||
|
this.surname = surname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addFilm(Film f) {
|
||||||
|
films.add(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
String res = "\nFilm{" +
|
||||||
|
"id: " + id + "," +
|
||||||
|
"name: " + name + "," +
|
||||||
|
"surname: " + name + "," +
|
||||||
|
"films:" + (films == null ? "[]" : films.toString()) + "}"
|
||||||
|
;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
56
src/main/java/ru/ip/labs/labs/films/models/Film.java
Normal file
56
src/main/java/ru/ip/labs/labs/films/models/Film.java
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package ru.ip.labs.labs.films.models;
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Film {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@ManyToMany(fetch = FetchType.EAGER)
|
||||||
|
@JoinTable(name="films_genres",
|
||||||
|
joinColumns = @JoinColumn(name="film_id"),
|
||||||
|
inverseJoinColumns = @JoinColumn(name="genre_id")
|
||||||
|
)
|
||||||
|
private List<Genre> genres;
|
||||||
|
|
||||||
|
public Film() {}
|
||||||
|
|
||||||
|
public Film(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addGenre(Genre g) {
|
||||||
|
genres.add(g);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
String res = "\nFilm{" +
|
||||||
|
"id: " + id + "," +
|
||||||
|
"name: " + name + "," +
|
||||||
|
"genres:" + (genres == null ? "[]" : genres.toString()) + "}"
|
||||||
|
;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Iterator<Genre> iter = genres.iterator();
|
||||||
|
while(iter.hasNext()) {
|
||||||
|
Genre curr = iter.next();
|
||||||
|
res += "{ id: " + curr.getId() + ", name: " + curr.getName() + (iter.hasNext() ? "}, " : "}");
|
||||||
|
}
|
||||||
|
|
||||||
|
res += "]\n}";*/
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
39
src/main/java/ru/ip/labs/labs/films/models/Genre.java
Normal file
39
src/main/java/ru/ip/labs/labs/films/models/Genre.java
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package ru.ip.labs.labs.films.models;
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Genre {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,85 @@
|
|||||||
|
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 javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.EntityNotFoundException;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ActorService {
|
||||||
|
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
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);
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
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.addFilm(film);
|
||||||
|
return em.merge(actor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
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 actor;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Actor> findAllActors() {
|
||||||
|
return em.createQuery("select f from Actor f", Actor.class)
|
||||||
|
.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Actor updateActor(Long id, String name) {
|
||||||
|
if (!StringUtils.hasText(name)) {
|
||||||
|
throw new IllegalArgumentException("Actor name is null or empty");
|
||||||
|
}
|
||||||
|
final Actor currentActor = findActor(id);
|
||||||
|
currentActor.setName(name);
|
||||||
|
return em.merge(currentActor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Actor deleteActor(Long id) {
|
||||||
|
final Actor currentActor = findActor(id);
|
||||||
|
em.remove(currentActor);
|
||||||
|
return currentActor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllActors() {
|
||||||
|
em.createQuery("delete from Actor").executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,85 @@
|
|||||||
|
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.Film;
|
||||||
|
import ru.ip.labs.labs.films.models.Genre;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.EntityNotFoundException;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class FilmsService {
|
||||||
|
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Film addFilm(String name) {
|
||||||
|
if (!StringUtils.hasText(name)) {
|
||||||
|
throw new IllegalArgumentException("Student name is null or empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
Film film = new Film(name);
|
||||||
|
|
||||||
|
em.persist(film);
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
return em.merge(film);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
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 film;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Film> findAllFilms() {
|
||||||
|
return em.createQuery("select f from Film f", Film.class)
|
||||||
|
.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Film updateFilm(Long id, String name) {
|
||||||
|
if (!StringUtils.hasText(name)) {
|
||||||
|
throw new IllegalArgumentException("Film name is null or empty");
|
||||||
|
}
|
||||||
|
final Film currentFilm = findFilm(id);
|
||||||
|
currentFilm.setName(name);
|
||||||
|
return em.merge(currentFilm);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Film deleteFilm(Long id) {
|
||||||
|
final Film currentFilm = findFilm(id);
|
||||||
|
em.remove(currentFilm);
|
||||||
|
return currentFilm;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllFilms() {
|
||||||
|
em.createQuery("delete from Film").executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
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.Film;
|
||||||
|
import ru.ip.labs.labs.films.models.Genre;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.EntityNotFoundException;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class GenreService {
|
||||||
|
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Genre addGenre(String name) {
|
||||||
|
if (!StringUtils.hasText(name)) {
|
||||||
|
throw new IllegalArgumentException("Genre name is null or empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
Genre genre = new Genre(name);
|
||||||
|
|
||||||
|
em.persist(genre);
|
||||||
|
return em.find(Genre.class, genre.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
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 genre;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Genre> findAllFilms() {
|
||||||
|
return em.createQuery("select g from Genre g", Genre.class)
|
||||||
|
.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Genre updateGenre(Long id, String name) {
|
||||||
|
if (!StringUtils.hasText(name)) {
|
||||||
|
throw new IllegalArgumentException("Film name is null or empty");
|
||||||
|
}
|
||||||
|
final Genre currentGenre = findGenre(id);
|
||||||
|
currentGenre.setName(name);
|
||||||
|
return em.merge(currentGenre);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Genre deleteGenre(Long id) {
|
||||||
|
final Genre currentGenre = findGenre(id);
|
||||||
|
em.remove(currentGenre);
|
||||||
|
return currentGenre;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllGenres() {
|
||||||
|
em.createQuery("delete from Genre").executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1,11 @@
|
|||||||
|
spring.main.banner-mode=off
|
||||||
|
#server.port=8080
|
||||||
|
spring.datasource.url=jdbc:h2:file:./data
|
||||||
|
spring.datasource.driverClassName=org.h2.Driver
|
||||||
|
spring.datasource.username=sa
|
||||||
|
spring.datasource.password=password
|
||||||
|
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||||
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
|
spring.h2.console.enabled=true
|
||||||
|
spring.h2.console.settings.trace=false
|
||||||
|
spring.h2.console.settings.web-allow-others=false
|
||||||
|
4
src/test/java/ru/ip/labs/labs/JpaActorsTest.java
Normal file
4
src/test/java/ru/ip/labs/labs/JpaActorsTest.java
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
package ru.ip.labs.labs;
|
||||||
|
|
||||||
|
public class JpaActorsTest {
|
||||||
|
}
|
61
src/test/java/ru/ip/labs/labs/JpaFilmsTest.java
Normal file
61
src/test/java/ru/ip/labs/labs/JpaFilmsTest.java
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
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.service.FilmsService;
|
||||||
|
|
||||||
|
import javax.persistence.EntityNotFoundException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class JpaFilmsTest {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(JpaFilmsTest.class);
|
||||||
|
@Autowired
|
||||||
|
FilmsService filmService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testFilmCreate() {
|
||||||
|
filmService.deleteAllFilms();
|
||||||
|
final Film film = filmService.addFilm("Terminator 2");
|
||||||
|
log.info(film.toString());
|
||||||
|
Assertions.assertNotNull(film.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testStudentReadNotFound() {
|
||||||
|
filmService.deleteAllFilms();
|
||||||
|
Assertions.assertThrows(EntityNotFoundException.class, () -> filmService.findFilm(-1L));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findFilm() {
|
||||||
|
filmService.deleteAllFilms();
|
||||||
|
final Film film = filmService.addFilm("Terminator 2");
|
||||||
|
log.info(film.toString());
|
||||||
|
final Film findFilm = filmService.findFilm(film.getId());
|
||||||
|
log.info(findFilm.toString());
|
||||||
|
Assertions.assertEquals(film.toString(), findFilm.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testReadAll() {
|
||||||
|
filmService.deleteAllFilms();
|
||||||
|
final Film film = filmService.addFilm("Terminator 2");
|
||||||
|
log.info(film.toString());
|
||||||
|
final List<Film> films = filmService.findAllFilms();
|
||||||
|
Assertions.assertEquals(films.size(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void deleteFilm() {
|
||||||
|
filmService.deleteAllFilms();
|
||||||
|
final Film film = filmService.addFilm("Terminator 2");
|
||||||
|
log.info(film.toString());
|
||||||
|
filmService.deleteFilm(film.getId());
|
||||||
|
Assertions.assertThrows(EntityNotFoundException.class, () -> filmService.findFilm(film.getId()));
|
||||||
|
}
|
||||||
|
}
|
4
src/test/java/ru/ip/labs/labs/JpaGenresTest.java
Normal file
4
src/test/java/ru/ip/labs/labs/JpaGenresTest.java
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
package ru.ip.labs.labs;
|
||||||
|
|
||||||
|
public class JpaGenresTest {
|
||||||
|
}
|
@ -19,11 +19,6 @@ class LabsApplicationTests {
|
|||||||
Assertions.assertEquals(4, res);
|
Assertions.assertEquals(4, res);
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
void testStringSum() {
|
|
||||||
String res = calculatorService.getSum("hello, ", 2, "string");
|
|
||||||
Assertions.assertEquals("hello, 2", res);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
void testArraySum() {
|
void testArraySum() {
|
||||||
String res = calculatorService.getSum("1,2", 2, "array");
|
String res = calculatorService.getSum("1,2", 2, "array");
|
||||||
Assertions.assertEquals("3,4", res);
|
Assertions.assertEquals("3,4", res);
|
||||||
|
Loading…
Reference in New Issue
Block a user