ЛР3 все кроме тестов
This commit is contained in:
parent
dfe84a7b21
commit
3df09259bd
@ -14,6 +14,14 @@ 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'
|
||||||
|
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||||
|
|
||||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BIN
data.mv.db
Normal file
BIN
data.mv.db
Normal file
Binary file not shown.
@ -0,0 +1,56 @@
|
|||||||
|
package ru.ulstu.is.lab1.DataBase.controller;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PatchMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import ru.ulstu.is.lab1.DataBase.model.Collection;
|
||||||
|
import ru.ulstu.is.lab1.DataBase.service.CollectionService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/collection")
|
||||||
|
public class CollectionController {
|
||||||
|
private final CollectionService collectionService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public CollectionController(CollectionService collectionService) {
|
||||||
|
this.collectionService = collectionService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public Collection getCollection(@PathVariable Long id) {
|
||||||
|
return collectionService.findCollection(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/")
|
||||||
|
public List<Collection> getCollection() {
|
||||||
|
return collectionService.findAllCollections();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/")
|
||||||
|
public Collection createCollection(@RequestParam("name") String name) {
|
||||||
|
return collectionService.addCollection(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PatchMapping("/{id}")
|
||||||
|
public Collection updateCollection(@PathVariable Long id, @RequestParam("name") String name) {
|
||||||
|
return collectionService.updateCollection(id, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PatchMapping("/add_film/{id}")
|
||||||
|
public Collection addFilm(@PathVariable Long id, @RequestParam Long film_id) {
|
||||||
|
return collectionService.addFilm(id, film_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public Collection deleteCollection(@PathVariable Long id) {
|
||||||
|
return collectionService.deleteCollection(id);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
package ru.ulstu.is.lab1.DataBase.controller;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PatchMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import ru.ulstu.is.lab1.DataBase.model.Film;
|
||||||
|
import ru.ulstu.is.lab1.DataBase.service.FilmService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/film")
|
||||||
|
public class FilmController {
|
||||||
|
private final FilmService filmService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public FilmController(FilmService 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> getFilm() {
|
||||||
|
return filmService.findAllFilms();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/")
|
||||||
|
public Film createFilm(@RequestParam("name") String name) {
|
||||||
|
return filmService.addFilm(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PatchMapping("/{id}")
|
||||||
|
public Film updateFilm(@PathVariable Long id, @RequestParam("name") 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,51 @@
|
|||||||
|
package ru.ulstu.is.lab1.DataBase.controller;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PatchMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import ru.ulstu.is.lab1.DataBase.model.Genre;
|
||||||
|
import ru.ulstu.is.lab1.DataBase.service.GenreService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/genre")
|
||||||
|
public class GenreController {
|
||||||
|
private final GenreService genreService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public GenreController(GenreService genreService) {
|
||||||
|
this.genreService = genreService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public Genre getGenre(@PathVariable Long id) {
|
||||||
|
return genreService.findGenre(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/")
|
||||||
|
public List<Genre> getGenre() {
|
||||||
|
return genreService.findAllGenres();
|
||||||
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
package ru.ulstu.is.lab1.DataBase.model;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Collection {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
private String name;
|
||||||
|
@ManyToMany(fetch = FetchType.EAGER)
|
||||||
|
@JoinTable(name="collections_films",
|
||||||
|
joinColumns = @JoinColumn(name="collection_id"),
|
||||||
|
inverseJoinColumns = @JoinColumn(name="film_id")
|
||||||
|
)
|
||||||
|
private List<Film> films;
|
||||||
|
|
||||||
|
public Collection() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addFilm(Film f) {
|
||||||
|
films.add(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Collection{" +
|
||||||
|
"id=" + id +
|
||||||
|
", name='" + name + '\'' +
|
||||||
|
"films:" + (films == null ? "[]" : films.toString()) +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
53
src/main/java/ru/ulstu/is/lab1/DataBase/model/Film.java
Normal file
53
src/main/java/ru/ulstu/is/lab1/DataBase/model/Film.java
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package ru.ulstu.is.lab1.DataBase.model;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
|
||||||
|
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;
|
||||||
|
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "films")
|
||||||
|
private List<Collection> collections;
|
||||||
|
|
||||||
|
public Film() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Film(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addGenre(Genre g) {
|
||||||
|
genres.add(g);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Film{" +
|
||||||
|
"id=" + id +
|
||||||
|
", name='" + name + '\'' +
|
||||||
|
"genres:" + (genres == null ? "[]" : genres.toString()) +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
42
src/main/java/ru/ulstu/is/lab1/DataBase/model/Genre.java
Normal file
42
src/main/java/ru/ulstu/is/lab1/DataBase/model/Genre.java
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package ru.ulstu.is.lab1.DataBase.model;
|
||||||
|
|
||||||
|
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,82 @@
|
|||||||
|
package ru.ulstu.is.lab1.DataBase.service;
|
||||||
|
|
||||||
|
import ru.ulstu.is.lab1.DataBase.model.Collection;
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.EntityNotFoundException;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import ru.ulstu.is.lab1.DataBase.model.Film;
|
||||||
|
import ru.ulstu.is.lab1.DataBase.model.Genre;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CollectionService {
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Collection addCollection(String name) {
|
||||||
|
if (!StringUtils.hasText(name)) {
|
||||||
|
throw new IllegalArgumentException("Collection name is null or empty");
|
||||||
|
}
|
||||||
|
final Collection collection = new Collection(name);
|
||||||
|
em.persist(collection);
|
||||||
|
return collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Collection addFilm(Long collectionId, Long filmId) {
|
||||||
|
final Collection collection = em.find(Collection.class, collectionId);
|
||||||
|
if (collection == null) {
|
||||||
|
throw new EntityNotFoundException(String.format("Film with id [%s] is not found", collectionId));
|
||||||
|
}
|
||||||
|
|
||||||
|
final Film film = em.find(Film.class, filmId);
|
||||||
|
if (film == null) {
|
||||||
|
throw new EntityNotFoundException(String.format("Genre with id [%s] is not found", filmId));
|
||||||
|
}
|
||||||
|
|
||||||
|
collection.addFilm(film);
|
||||||
|
return em.merge(collection);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Collection findCollection(Long id) {
|
||||||
|
final Collection collection = em.find(Collection.class, id);
|
||||||
|
if (collection == null) {
|
||||||
|
throw new EntityNotFoundException(String.format("Collection with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
return collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Collection> findAllCollections() {
|
||||||
|
return em.createQuery("select c from Collection c", Collection.class)
|
||||||
|
.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Collection updateCollection(Long id, String name) {
|
||||||
|
if (!StringUtils.hasText(name)) {
|
||||||
|
throw new IllegalArgumentException("Collection name is null or empty");
|
||||||
|
}
|
||||||
|
final Collection currentCollection = findCollection(id);
|
||||||
|
currentCollection.setName(name);
|
||||||
|
return em.merge(currentCollection);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Collection deleteCollection(Long id) {
|
||||||
|
final Collection currentCollection = findCollection(id);
|
||||||
|
em.remove(currentCollection);
|
||||||
|
return currentCollection;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllCollections() {
|
||||||
|
em.createQuery("delete from Collection").executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,81 @@
|
|||||||
|
package ru.ulstu.is.lab1.DataBase.service;
|
||||||
|
|
||||||
|
import ru.ulstu.is.lab1.DataBase.model.Film;
|
||||||
|
import ru.ulstu.is.lab1.DataBase.model.Genre;
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.EntityNotFoundException;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class FilmService {
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Film addFilm(String name) {
|
||||||
|
if (!StringUtils.hasText(name)) {
|
||||||
|
throw new IllegalArgumentException("Film name is null or empty");
|
||||||
|
}
|
||||||
|
final Film film = new Film(name);
|
||||||
|
em.persist(film);
|
||||||
|
return film;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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,64 @@
|
|||||||
|
package ru.ulstu.is.lab1.DataBase.service;
|
||||||
|
|
||||||
|
import ru.ulstu.is.lab1.DataBase.model.Genre;
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.EntityNotFoundException;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
final Genre genre = new Genre(name);
|
||||||
|
em.persist(genre);
|
||||||
|
return genre;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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> findAllGenres() {
|
||||||
|
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("Genre 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,43 +0,0 @@
|
|||||||
package ru.ulstu.is.lab1.speaker.controller;
|
|
||||||
|
|
||||||
import ru.ulstu.is.lab1.speaker.service.MethodService;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
public class MethodController {
|
|
||||||
private final MethodService speakerService;
|
|
||||||
|
|
||||||
public MethodController(MethodService speakerService) {
|
|
||||||
this.speakerService = speakerService;
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/sum")
|
|
||||||
public String Sum(@RequestParam(value = "first", defaultValue = "1") Object first,
|
|
||||||
@RequestParam(value = "second", defaultValue = "1") Object second,
|
|
||||||
@RequestParam(value = "type", defaultValue = "int") String type) {
|
|
||||||
return speakerService.Sum(first, second, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/minus")
|
|
||||||
public String Ras(@RequestParam(value = "first", defaultValue = "1") Object first,
|
|
||||||
@RequestParam(value = "second", defaultValue = "1") Object second,
|
|
||||||
@RequestParam(value = "type", defaultValue = "int") String type) {
|
|
||||||
return speakerService.Ras(first, second, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/reverse")
|
|
||||||
public String Pros(@RequestParam(value = "first", defaultValue = "1") Object first,
|
|
||||||
@RequestParam(value = "second", defaultValue = "1") Object second,
|
|
||||||
@RequestParam(value = "type", defaultValue = "int") String type) {
|
|
||||||
return speakerService.Rev(first, second, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/comparison")
|
|
||||||
public String Del(@RequestParam(value = "first", defaultValue = "1") Object first,
|
|
||||||
@RequestParam(value = "second", defaultValue = "1") Object second,
|
|
||||||
@RequestParam(value = "type", defaultValue = "int") String type) {
|
|
||||||
return speakerService.Com(first, second, type);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
package ru.ulstu.is.lab1.speaker.domain;
|
|
||||||
|
|
||||||
public interface IMethod<T> {
|
|
||||||
T Sum(T first, T second);
|
|
||||||
|
|
||||||
T Minus(T first, T second);
|
|
||||||
|
|
||||||
T Reverse(T first, T second);
|
|
||||||
|
|
||||||
T Comparison(T first, T second);
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
package ru.ulstu.is.lab1.speaker.domain;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Component(value="int")
|
|
||||||
public class MethodInt implements IMethod<Integer>{
|
|
||||||
public Integer Sum(Integer first, Integer second) {
|
|
||||||
return first + second;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer Minus(Integer first, Integer second) {
|
|
||||||
return first - second;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer Reverse(Integer first, Integer second) {
|
|
||||||
return (first + second) * (-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer Comparison(Integer first, Integer second) {
|
|
||||||
if (first >= second){
|
|
||||||
return first;
|
|
||||||
}else{
|
|
||||||
return second;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
package ru.ulstu.is.lab1.speaker.domain;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Component(value="string")
|
|
||||||
public class MethodString implements IMethod<String>{
|
|
||||||
@Override
|
|
||||||
public String Sum(String first, String second) {
|
|
||||||
return first.concat(second);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String Minus(String first, String second) {
|
|
||||||
String[] arr = first.split("");
|
|
||||||
for(int i = 0; i < first.length(); i++){
|
|
||||||
if (second.contains(arr[i])){
|
|
||||||
arr[i] = "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return String.join("", arr);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String Reverse(String first, String second) {
|
|
||||||
String ourStr = first.concat(second);
|
|
||||||
StringBuilder newStr = new StringBuilder();
|
|
||||||
for(int i = ourStr.length() - 1; i >= 0; i--){
|
|
||||||
newStr.append(ourStr.charAt(i));
|
|
||||||
}
|
|
||||||
return newStr.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String Comparison(String first, String second) {
|
|
||||||
if (first.length() >= second.length()){
|
|
||||||
return first;
|
|
||||||
}else{
|
|
||||||
return second;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,51 +0,0 @@
|
|||||||
package ru.ulstu.is.lab1.speaker.service;
|
|
||||||
|
|
||||||
import ru.ulstu.is.lab1.speaker.domain.IMethod;
|
|
||||||
import ru.ulstu.is.lab1.speaker.domain.MethodString;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class MethodService {
|
|
||||||
private final ApplicationContext applicationContext;
|
|
||||||
|
|
||||||
public MethodService(ApplicationContext applicationContext) {
|
|
||||||
this.applicationContext = applicationContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String Sum(Object first, Object second, String type) {
|
|
||||||
final IMethod speaker = (IMethod) applicationContext.getBean(type);
|
|
||||||
if (speaker instanceof MethodString){
|
|
||||||
return String.format("%s", speaker.Sum(first,second));
|
|
||||||
}else{
|
|
||||||
return String.format("%s", speaker.Sum(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public String Ras(Object first, Object second, String type) {
|
|
||||||
final IMethod speaker = (IMethod) applicationContext.getBean(type);
|
|
||||||
if (speaker instanceof MethodString){
|
|
||||||
return String.format("%s", speaker.Minus(first,second));
|
|
||||||
}else{
|
|
||||||
return String.format("%s", speaker.Minus(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public String Rev(Object first, Object second, String type) {
|
|
||||||
final IMethod speaker = (IMethod) applicationContext.getBean(type);
|
|
||||||
if (speaker instanceof MethodString){
|
|
||||||
return String.format("%s", speaker.Reverse(first,second));
|
|
||||||
}else{
|
|
||||||
return String.format("%s", speaker.Reverse(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public String Com(Object first, Object second, String type) {
|
|
||||||
final IMethod speaker = (IMethod) applicationContext.getBean(type);
|
|
||||||
if (speaker instanceof MethodString){
|
|
||||||
return String.format("%s", speaker.Comparison(first,second));
|
|
||||||
}else {
|
|
||||||
return String.format("%s", speaker.Comparison(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
11
src/main/resources/application.properties
Normal file
11
src/main/resources/application.properties
Normal file
@ -0,0 +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
|
@ -1,63 +1,24 @@
|
|||||||
package ru.ulstu.is.lab1;
|
package ru.ulstu.is.lab1;
|
||||||
|
|
||||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
import ru.ulstu.is.lab1.DataBase.model.Film;
|
||||||
import ru.ulstu.is.lab1.speaker.service.MethodService;
|
import ru.ulstu.is.lab1.DataBase.model.Collection;
|
||||||
|
import ru.ulstu.is.lab1.DataBase.model.Genre;
|
||||||
|
import ru.ulstu.is.lab1.DataBase.service.FilmService;
|
||||||
|
import ru.ulstu.is.lab1.DataBase.service.CollectionService;
|
||||||
|
import ru.ulstu.is.lab1.DataBase.service.GenreService;
|
||||||
|
import javax.persistence.EntityNotFoundException;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
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 java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class Lab1ApplicationTests {
|
class Lab1ApplicationTests {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
MethodService methodService;
|
|
||||||
@Test
|
|
||||||
void contextLoads() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testPlusInt() {
|
|
||||||
final String res = methodService.Sum(10, 10, "int");
|
|
||||||
Assertions.assertEquals(20, Integer.parseInt(res));
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
void testMinusInt() {
|
|
||||||
final String res = methodService.Ras(8, 4, "int");
|
|
||||||
Assertions.assertEquals(4, Integer.parseInt(res));
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
void testReverseInt() {
|
|
||||||
final String res = methodService.Rev(10, 10, "int");
|
|
||||||
Assertions.assertEquals(-20, Integer.parseInt(res));
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
void testComparisonInt() {
|
|
||||||
final String res = methodService.Com(8, 4, "int");
|
|
||||||
Assertions.assertEquals(8, Integer.parseInt(res));
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
void testPlusStr() {
|
|
||||||
final String res = methodService.Sum("10", "10", "string");
|
|
||||||
Assertions.assertEquals("1010", res);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
void testMinusStr() {
|
|
||||||
final String res = methodService.Ras("846734", "4", "string");
|
|
||||||
Assertions.assertEquals("8673", res);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
void testReverseStr() {
|
|
||||||
final String res = methodService.Rev("846734", "312", "string");
|
|
||||||
Assertions.assertEquals("213437648", res);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
void testComparisonStr() {
|
|
||||||
final String res = methodService.Com("846734", "312", "string");
|
|
||||||
Assertions.assertEquals("846734", res);
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
void testNumberFormatException() {
|
|
||||||
Assertions.assertThrows(NumberFormatException.class, () -> methodService.Sum("п", 3, "int"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
6
src/test/resources/application.properties
Normal file
6
src/test/resources/application.properties
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
spring.datasource.url=jdbc:h2:mem:testdb
|
||||||
|
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=create-drop
|
Loading…
Reference in New Issue
Block a user