BackEnd написан, но не факт, что работает
This commit is contained in:
parent
f7ad06d262
commit
c8e62cbc4f
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
@ -1,4 +1,43 @@
|
||||
package ru.ip.labworks.labworks.bookshop.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ip.labworks.labworks.bookshop.service.AuthorService;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/author")
|
||||
public class AuthorController {
|
||||
private final AuthorService authorService;
|
||||
|
||||
public AuthorController(AuthorService authorService){
|
||||
this.authorService = authorService;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public AuthorDto getAuthor(@PathVariable Long id){
|
||||
return new AuthorDto(authorService.findAuthor(id));
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<AuthorDto> getAuthors(){
|
||||
return authorService.findAllAuthors().stream().map(AuthorDto::new).toList();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public AuthorDto createAuthor(@RequestBody @Valid AuthorDto authorDto) throws IOException {
|
||||
return new AuthorDto(authorService.addAuthor(authorDto));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public AuthorDto updateAuthor(@PathVariable Long id, @RequestBody @Valid AuthorDto authorDto){
|
||||
return new AuthorDto(authorService.updateAuthor(id, authorDto));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public void deleteAuthor(@PathVariable Long id){
|
||||
authorService.deleteAuthor(id);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
package ru.ip.labworks.labworks.bookshop.controller;
|
||||
|
||||
import ru.ip.labworks.labworks.bookshop.model.Author;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class AuthorDto {
|
||||
private Long id;
|
||||
private String firstname;
|
||||
private String lastname;
|
||||
private String photo;
|
||||
|
||||
public AuthorDto(){}
|
||||
public AuthorDto(Author author){
|
||||
id = author.getId();
|
||||
firstname = author.getFirstnameName();
|
||||
lastname = author.getLastName();
|
||||
photo = new String(author.getPhoto(), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public Long getId(){return id;}
|
||||
public String getFirstname(){return firstname;}
|
||||
public String getLastname(){return lastname;}
|
||||
public String getPhoto(){return photo;}
|
||||
}
|
@ -1,4 +1,43 @@
|
||||
package ru.ip.labworks.labworks.bookshop.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ip.labworks.labworks.bookshop.service.BookService;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/post")
|
||||
public class BookController {
|
||||
private final BookService bookService;
|
||||
|
||||
public BookController(BookService bookService){
|
||||
this.bookService = bookService;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public BookDto getBook(@PathVariable Long id){
|
||||
return new BookDto(bookService.findBook(id));
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<BookDto> getBooks(){
|
||||
return bookService.findAllBooks().stream().map(BookDto::new).toList();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public BookDto createBook(@RequestBody @Valid BookDto bookDto) throws IOException {
|
||||
return new BookDto(bookService.addBook(bookDto));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public BookDto updateBook(@PathVariable Long id, @RequestBody @Valid BookDto bookDto){
|
||||
return new BookDto(bookService.updateBook(id, bookDto));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public void deleteBook(@PathVariable Long id){
|
||||
bookService.deleteBook(id);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,26 @@
|
||||
package ru.ip.labworks.labworks.bookshop.controller;
|
||||
|
||||
import ru.ip.labworks.labworks.bookshop.model.Book;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
|
||||
public class BookDto {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Date release;
|
||||
private String cover;
|
||||
|
||||
public BookDto(){}
|
||||
public BookDto(Book book){
|
||||
id = book.getId();
|
||||
name = book.getName();
|
||||
release = book.getRelease();
|
||||
cover = new String(book.getCover(), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public Long getId(){return id;}
|
||||
public String getName(){return name;}
|
||||
public Date getRelease(){return release;}
|
||||
public String getCover(){return cover;}
|
||||
}
|
@ -1,4 +1,43 @@
|
||||
package ru.ip.labworks.labworks.bookshop.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ip.labworks.labworks.bookshop.service.GenreService;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/genre")
|
||||
public class GenreController {
|
||||
private final GenreService genreService;
|
||||
|
||||
public GenreController(GenreService genreService){
|
||||
this.genreService = genreService;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public GenreDto getGenre(@PathVariable Long id){
|
||||
return new GenreDto(genreService.findGenre(id));
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<GenreDto> getGenres(){
|
||||
return genreService.findAllGenres().stream().map(GenreDto::new).toList();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public GenreDto createGenre(@RequestBody @Valid GenreDto genreDto) throws IOException {
|
||||
return new GenreDto(genreService.addGenre(genreDto));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public GenreDto updateGenre(@PathVariable Long id, @RequestBody @Valid GenreDto genreDto){
|
||||
return new GenreDto(genreService.updateGenre(id, genreDto));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public void deleteGenre(@PathVariable Long id){
|
||||
genreService.deleteGenre(id);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,17 @@
|
||||
package ru.ip.labworks.labworks.bookshop.controller;
|
||||
|
||||
import ru.ip.labworks.labworks.bookshop.model.Genre;
|
||||
|
||||
public class GenreDto {
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
public GenreDto(){}
|
||||
public GenreDto(Genre genre){
|
||||
id = genre.getId();
|
||||
name = genre.getName();
|
||||
}
|
||||
|
||||
public Long getId(){return id;}
|
||||
public String getName(){return name;}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
package ru.ip.labworks.labworks.bookshop.model;
|
||||
|
||||
import ru.ip.labworks.labworks.bookshop.controller.AuthorDto;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.*;
|
||||
|
||||
@ -24,6 +26,11 @@ public class Author {
|
||||
this.lastname = lastname;
|
||||
this.photo = photo;
|
||||
}
|
||||
public Author(AuthorDto authorDto){
|
||||
this.firstname = authorDto.getFirstname();
|
||||
this.lastname = authorDto.getLastname();
|
||||
this.photo = authorDto.getPhoto().getBytes();
|
||||
}
|
||||
|
||||
public Long getId(){return id;}
|
||||
|
||||
|
@ -2,6 +2,7 @@ package ru.ip.labworks.labworks.bookshop.model;
|
||||
|
||||
import org.hibernate.annotations.LazyCollection;
|
||||
import org.hibernate.annotations.LazyCollectionOption;
|
||||
import ru.ip.labworks.labworks.bookshop.controller.BookDto;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.*;
|
||||
@ -31,6 +32,11 @@ public class Book {
|
||||
this.release = release;
|
||||
this.cover = cover;
|
||||
}
|
||||
public Book(BookDto bookDto){
|
||||
this.name = bookDto.getName();
|
||||
this.release = bookDto.getRelease();
|
||||
this.cover = bookDto.getCover().getBytes();
|
||||
}
|
||||
|
||||
public Long getId(){return id;}
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
package ru.ip.labworks.labworks.bookshop.model;
|
||||
|
||||
import ru.ip.labworks.labworks.bookshop.controller.GenreDto;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.*;
|
||||
|
||||
@ -14,6 +16,9 @@ public class Genre {
|
||||
public Genre(String name){
|
||||
this.name = name;
|
||||
}
|
||||
public Genre(GenreDto genreDto){
|
||||
this.name = genreDto.getName();
|
||||
}
|
||||
|
||||
public Long getId(){return id;}
|
||||
|
||||
|
@ -0,0 +1,15 @@
|
||||
package ru.ip.labworks.labworks.bookshop.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import ru.ip.labworks.labworks.bookshop.model.Author;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface AuthorRepository extends JpaRepository<Author, Long> {
|
||||
@Query("select a.lastname as author, b.name as book " +
|
||||
"from Author a " +
|
||||
"join a.books b " +
|
||||
"group by a.id")
|
||||
List<Object[]> getAuthorsWithBooks();
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package ru.ip.labworks.labworks.bookshop.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.ip.labworks.labworks.bookshop.model.Book;
|
||||
|
||||
public interface BookRepository extends JpaRepository<Book, Long> {
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package ru.ip.labworks.labworks.bookshop.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.ip.labworks.labworks.bookshop.model.Genre;
|
||||
|
||||
public interface GenreRepository extends JpaRepository<Genre, Long> {
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package ru.ip.labworks.labworks.bookshop.service;
|
||||
|
||||
public class AuthorNotFoundException extends RuntimeException{
|
||||
public AuthorNotFoundException(Long id) {
|
||||
super(String.format("Author with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -1,24 +1,37 @@
|
||||
package ru.ip.labworks.labworks.bookshop.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.ip.labworks.labworks.bookshop.controller.AuthorDto;
|
||||
import ru.ip.labworks.labworks.bookshop.model.*;
|
||||
import org.hibernate.query.Query;
|
||||
import ru.ip.labworks.labworks.bookshop.repository.AuthorRepository;
|
||||
import ru.ip.labworks.labworks.bookshop.repository.BookRepository;
|
||||
import ru.ip.labworks.labworks.util.validation.ValidatorUtil;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Tuple;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class AuthorService {
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
@Autowired
|
||||
private final AuthorRepository authorRepository;
|
||||
@Autowired
|
||||
private final BookRepository bookRepository;
|
||||
@Autowired
|
||||
private final ValidatorUtil validatorUtil;
|
||||
|
||||
public AuthorService(AuthorRepository authorRepository, BookRepository bookRepository, ValidatorUtil validatorUtil){
|
||||
this.authorRepository = authorRepository;
|
||||
this.bookRepository = bookRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Author addAuthor(String firstname, String lastname, File photo){
|
||||
@ -26,22 +39,31 @@ public class AuthorService {
|
||||
throw new IllegalArgumentException("Author firstname and lastname is null or empty");
|
||||
}
|
||||
final Author author = new Author(firstname, lastname, ImageHelper.ImageToByte(photo));
|
||||
entityManager.persist(author);
|
||||
return author;
|
||||
validatorUtil.validate(author);
|
||||
return authorRepository.save(author);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Author addAuthor(AuthorDto authorDto) throws IOException {
|
||||
final Author author = new Author(authorDto);
|
||||
validatorUtil.validate(author);
|
||||
return authorRepository.save(author);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void saveAuthor(Author author){
|
||||
authorRepository.save(author);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Author findAuthor(Long id) {
|
||||
final Author author = entityManager.find(Author.class, id);
|
||||
if (author == null) {
|
||||
throw new EntityNotFoundException(String.format("Author with id [%s] is not found", id));
|
||||
}
|
||||
return author;
|
||||
final Optional<Author> author = authorRepository.findById(id);
|
||||
return author.orElseThrow(() -> new AuthorNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Author> findAllAuthors() {
|
||||
return entityManager.createQuery("select s from Author s", Author.class).getResultList();
|
||||
return authorRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@ -53,48 +75,51 @@ public class AuthorService {
|
||||
currentAuthor.setFirstnameName(firstname);
|
||||
currentAuthor.setLastName(lastname);
|
||||
currentAuthor.setPhoto(ImageHelper.ImageToByte(photo));
|
||||
return entityManager.merge(currentAuthor);
|
||||
validatorUtil.validate(currentAuthor);
|
||||
return authorRepository.save(currentAuthor);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Author deleteAuthor(Long id) {
|
||||
public Author updateAuthor(Long id, AuthorDto authorDto){
|
||||
final Author currentAuthor = findAuthor(id);
|
||||
entityManager.remove(currentAuthor);
|
||||
return currentAuthor;
|
||||
currentAuthor.setFirstnameName(authorDto.getFirstname());
|
||||
currentAuthor.setLastName(authorDto.getLastname());
|
||||
currentAuthor.setPhoto(authorDto.getPhoto().getBytes());
|
||||
validatorUtil.validate(currentAuthor);
|
||||
return authorRepository.save(currentAuthor);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAuthor(Long id) {
|
||||
authorRepository.deleteById(id);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllAuthors() {
|
||||
entityManager.createQuery("delete from Author").executeUpdate();
|
||||
authorRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void addBookToAuthor(Long id, Book book){
|
||||
final Author author = findAuthor(id);
|
||||
if(author == null){
|
||||
throw new IllegalArgumentException("Author with id " + id + " not found");
|
||||
public void addBookToAuthor(Long id, Long bookId){
|
||||
Optional<Author> author = authorRepository.findById(id);
|
||||
if (author.isPresent()){
|
||||
author.get().addBook(bookRepository.findById(bookId).get());
|
||||
}
|
||||
author.addBook(entityManager.find(book.getClass(), book.getId()));
|
||||
entityManager.merge(author);
|
||||
authorRepository.save(author.get());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void removeBookFromAuthor(Long id, Book book){
|
||||
final Author author = findAuthor(id);
|
||||
if(author == null){
|
||||
throw new IllegalArgumentException("Author with id " + id + " not found");
|
||||
public void removeBookFromAuthor(Long id, Long bookId){
|
||||
Optional<Author> author = authorRepository.findById(id);
|
||||
if(author.isPresent()){
|
||||
author.get().removeBook(bookRepository.findById(bookId).get());
|
||||
}
|
||||
author.removeBook(entityManager.find(book.getClass(), book.getId()));
|
||||
entityManager.merge(author);
|
||||
authorRepository.save(author.get());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String, List<String>> AllAuthorsAndBooks(){
|
||||
List<Object[]> temp = entityManager.createQuery("select a.lastname as author, b.name as book " +
|
||||
"from Author a " +
|
||||
"join a.books b " +
|
||||
"group by a.id").getResultList();
|
||||
return temp.stream()
|
||||
return authorRepository.getAuthorsWithBooks().stream()
|
||||
.collect(
|
||||
Collectors.groupingBy(
|
||||
o -> (String) o[0],
|
||||
|
@ -0,0 +1,7 @@
|
||||
package ru.ip.labworks.labworks.bookshop.service;
|
||||
|
||||
public class BookNotFoundException extends RuntimeException{
|
||||
public BookNotFoundException(Long id) {
|
||||
super(String.format("Book with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -1,24 +1,35 @@
|
||||
package ru.ip.labworks.labworks.bookshop.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.ip.labworks.labworks.bookshop.controller.BookDto;
|
||||
import ru.ip.labworks.labworks.bookshop.model.Book;
|
||||
import ru.ip.labworks.labworks.bookshop.model.Genre;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import ru.ip.labworks.labworks.bookshop.repository.BookRepository;
|
||||
import ru.ip.labworks.labworks.bookshop.repository.GenreRepository;
|
||||
import ru.ip.labworks.labworks.util.validation.ValidatorUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
public class BookService {
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
@Autowired
|
||||
private final BookRepository bookRepository;
|
||||
@Autowired
|
||||
private final ValidatorUtil validatorUtil;
|
||||
@Autowired
|
||||
private final GenreRepository genreRepository;
|
||||
|
||||
public BookService(BookRepository bookRepository, ValidatorUtil validatorUtil, GenreRepository genreRepository){
|
||||
this.bookRepository = bookRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
this.genreRepository = genreRepository;
|
||||
}
|
||||
|
||||
private Date ParseToDate(String s){
|
||||
Date release;
|
||||
@ -37,22 +48,26 @@ public class BookService {
|
||||
throw new IllegalArgumentException("Book name and release is null or empty");
|
||||
}
|
||||
final Book book = new Book(name, ParseToDate(release), ImageHelper.ImageToByte(cover));
|
||||
entityManager.persist(book);
|
||||
return book;
|
||||
validatorUtil.validate(book);
|
||||
return bookRepository.save(book);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Book addBook(BookDto bookDto) throws IOException {
|
||||
final Book book = new Book(bookDto);
|
||||
validatorUtil.validate(book);
|
||||
return bookRepository.save(book);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Book findBook(Long id) {
|
||||
final Book book = entityManager.find(Book.class, id);
|
||||
if (book == null) {
|
||||
throw new EntityNotFoundException(String.format("Book with id [%s] is not found", id));
|
||||
}
|
||||
return book;
|
||||
final Optional<Book> book = bookRepository.findById(id);
|
||||
return book.orElseThrow(() -> new BookNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Book> findAllBooks() {
|
||||
return entityManager.createQuery("select s from Book s", Book.class).getResultList();
|
||||
return bookRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@ -64,38 +79,45 @@ public class BookService {
|
||||
currentBook.setName(name);
|
||||
currentBook.setRelease(ParseToDate(release));
|
||||
currentBook.setCover(ImageHelper.ImageToByte(cover));
|
||||
return entityManager.merge(currentBook);
|
||||
validatorUtil.validate(currentBook);
|
||||
return bookRepository.save(currentBook);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Book deleteBook(Long id) {
|
||||
public Book updateBook(Long id, BookDto bookDto){
|
||||
final Book currentBook = findBook(id);
|
||||
entityManager.remove(currentBook);
|
||||
return currentBook;
|
||||
currentBook.setName(bookDto.getName());
|
||||
currentBook.setRelease(bookDto.getRelease());
|
||||
currentBook.setCover(bookDto.getCover().getBytes());
|
||||
validatorUtil.validate(currentBook);
|
||||
return bookRepository.save(currentBook);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteBook(Long id) {
|
||||
bookRepository.deleteById(id);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllBooks() {
|
||||
entityManager.createQuery("delete from Book").executeUpdate();
|
||||
bookRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void addGenreToBook(Long id, Genre genre){
|
||||
final Book book = findBook(id);
|
||||
if(book == null){
|
||||
throw new IllegalArgumentException("Book with id " + id + " not found");
|
||||
public void addGenreToBook(Long id, Long genreId){
|
||||
Optional<Book> book = bookRepository.findById(id);
|
||||
if (book.isPresent()){
|
||||
book.get().addGenre(genreRepository.findById(genreId).get());
|
||||
}
|
||||
book.addGenre(entityManager.find(genre.getClass(), genre.getId()));
|
||||
entityManager.merge(book);
|
||||
bookRepository.save(book.get());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void removeGenreFromBook(Long id, Genre genre){
|
||||
final Book book = findBook(id);
|
||||
if(book == null){
|
||||
throw new IllegalArgumentException("Book with id " + id + " not found");
|
||||
public void removeGenreFromBook(Long id, Long genreId){
|
||||
Optional<Book> book = bookRepository.findById(id);
|
||||
if(book.isPresent()){
|
||||
book.get().removeGenre(genreRepository.findById(genreId).get());
|
||||
}
|
||||
book.removeGenre(entityManager.find(genre.getClass(), genre.getId()));
|
||||
entityManager.merge(book);
|
||||
bookRepository.save(book.get());
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
package ru.ip.labworks.labworks.bookshop.service;
|
||||
|
||||
public class GenreNotFoundException extends RuntimeException{
|
||||
public GenreNotFoundException(Long id) {
|
||||
super(String.format("Genre with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -1,19 +1,29 @@
|
||||
package ru.ip.labworks.labworks.bookshop.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.ip.labworks.labworks.bookshop.controller.GenreDto;
|
||||
import ru.ip.labworks.labworks.bookshop.model.Genre;
|
||||
import ru.ip.labworks.labworks.bookshop.repository.GenreRepository;
|
||||
import ru.ip.labworks.labworks.util.validation.ValidatorUtil;
|
||||
|
||||
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 GenreService {
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
@Autowired
|
||||
private final GenreRepository genreRepository;
|
||||
@Autowired
|
||||
private final ValidatorUtil validatorUtil;
|
||||
|
||||
public GenreService(GenreRepository genreRepository, ValidatorUtil validatorUtil){
|
||||
this.genreRepository = genreRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Genre addGenre(String name){
|
||||
@ -21,22 +31,24 @@ public class GenreService {
|
||||
throw new IllegalArgumentException("Genre name is null or empty");
|
||||
}
|
||||
final Genre genre = new Genre(name);
|
||||
entityManager.persist(genre);
|
||||
return genre;
|
||||
return genreRepository.save(genre);
|
||||
}
|
||||
@Transactional
|
||||
public Genre addGenre(GenreDto genreDto) throws IOException {
|
||||
final Genre genre = new Genre(genreDto);
|
||||
validatorUtil.validate(genre);
|
||||
return genreRepository.save(genre);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Genre findGenre(Long id) {
|
||||
final Genre genre = entityManager.find(Genre.class, id);
|
||||
if (genre == null) {
|
||||
throw new EntityNotFoundException(String.format("Genre with id [%s] is not found", id));
|
||||
}
|
||||
return genre;
|
||||
final Optional<Genre> genre = genreRepository.findById(id);
|
||||
return genre.orElseThrow(() -> new BookNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Genre> findAllGenres() {
|
||||
return entityManager.createQuery("select s from Genre s", Genre.class).getResultList();
|
||||
return genreRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@ -46,18 +58,23 @@ public class GenreService {
|
||||
}
|
||||
final Genre currentGenre = findGenre(id);
|
||||
currentGenre.setName(name);
|
||||
return entityManager.merge(currentGenre);
|
||||
return genreRepository.save(currentGenre);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Genre deleteGenre(Long id) {
|
||||
public Genre updateGenre(Long id, GenreDto genreDto){
|
||||
final Genre currentGenre = findGenre(id);
|
||||
entityManager.remove(currentGenre);
|
||||
return currentGenre;
|
||||
currentGenre.setName(genreDto.getName());
|
||||
return genreRepository.save(currentGenre);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteGenre(Long id) {
|
||||
genreRepository.deleteById(id);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllGenres() {
|
||||
entityManager.createQuery("delete from Genre").executeUpdate();
|
||||
genreRepository.deleteAll();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,36 @@
|
||||
package ru.ip.labworks.labworks.util.error;
|
||||
|
||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import ru.ip.labworks.labworks.util.validation.ValidationException;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ControllerAdvice
|
||||
public class AdviceController {
|
||||
@ExceptionHandler({
|
||||
ValidationException.class
|
||||
})
|
||||
public ResponseEntity<Object> handleException(Throwable e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseEntity<Object> handleBindException(MethodArgumentNotValidException e) {
|
||||
final ValidationException validationException = new ValidationException(
|
||||
e.getBindingResult().getAllErrors().stream()
|
||||
.map(DefaultMessageSourceResolvable::getDefaultMessage)
|
||||
.collect(Collectors.toSet()));
|
||||
return handleException(validationException);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<Object> handleUnknownException(Throwable e) {
|
||||
e.printStackTrace();
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package ru.ip.labworks.labworks.util.validation;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class ValidationException extends RuntimeException {
|
||||
public ValidationException(Set<String> errors) {
|
||||
super(String.join("\n", errors));
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package ru.ip.labworks.labworks.util.validation;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
public class ValidatorUtil {
|
||||
private final Validator validator;
|
||||
|
||||
public ValidatorUtil() {
|
||||
try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) {
|
||||
this.validator = factory.getValidator();
|
||||
}
|
||||
}
|
||||
|
||||
public <T> void validate(T object) {
|
||||
final Set<ConstraintViolation<T>> errors = validator.validate(object);
|
||||
if (!errors.isEmpty()) {
|
||||
throw new ValidationException(errors.stream()
|
||||
.map(ConstraintViolation::getMessage)
|
||||
.collect(Collectors.toSet()));
|
||||
}
|
||||
}
|
||||
}
|
@ -17,7 +17,7 @@ import java.util.Map;
|
||||
|
||||
@SpringBootTest
|
||||
public class JpaAuthorTests {
|
||||
@Autowired
|
||||
/*@Autowired
|
||||
AuthorService authorService;
|
||||
@Autowired
|
||||
BookService bookService;
|
||||
@ -112,5 +112,5 @@ public class JpaAuthorTests {
|
||||
result.put(author.getLastName(), temp);
|
||||
Assertions.assertEquals(authorService.AllAuthorsAndBooks(), result);
|
||||
Assertions.assertNotNull(authorService.AllAuthorsAndBooks());
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
public class JpaBookTests {
|
||||
@Autowired
|
||||
/*@Autowired
|
||||
BookService bookService;
|
||||
@Autowired
|
||||
GenreService genreService;
|
||||
@ -98,5 +98,5 @@ public class JpaBookTests {
|
||||
bookService.removeGenreFromBook(book.getId(), genre);
|
||||
book = bookService.findBook(book.getId());
|
||||
Assertions.assertEquals(book.getGenres().size(), 0);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
public class JpaGenreTests {
|
||||
@Autowired
|
||||
/*@Autowired
|
||||
GenreService genreService;
|
||||
|
||||
@Test
|
||||
@ -65,5 +65,5 @@ public class JpaGenreTests {
|
||||
final Genre genre = genreService.addGenre("Test");
|
||||
genreService.deleteGenre(genre.getId());
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> genreService.findGenre(1L));
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user