third labWork

This commit is contained in:
Максим Сергунов 2023-05-16 11:30:27 +04:00
parent 60dfe21df8
commit 06dd4eea2a
13 changed files with 857 additions and 1 deletions

View File

@ -14,6 +14,13 @@ repositories {
dependencies {
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'
}

View File

@ -0,0 +1,56 @@
package com.example.demo.Books.Controller;
import com.example.demo.Books.Service.BookService;
import com.example.demo.Books.Models.Book;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/book")
public class BookController {
private final BookService bookService;
public BookController(BookService bookService) {
this.bookService = bookService;
}
@GetMapping("/test")
public String test() {
return "Test request";
}
@GetMapping("/{id}")
public Book getBook(@PathVariable Long id) {
return bookService.findBook(id);
}
@GetMapping("/")
public List<Book> getBooks() {
return bookService.findAllBooks();
}
@PostMapping("/")
public Book createBook(@RequestParam String name) {
return bookService.addBook(name);
}
@PatchMapping("/{id}")
public Book updateBook(@PathVariable Long id,
@RequestParam("firstName") String name) {
return bookService.updateBook(id, name);
}
@PatchMapping("/add_genre/{id}")
public Book addGenre(@PathVariable Long id, @RequestParam Long genre_id) {
return bookService.addGenre(id, genre_id);
}
@DeleteMapping("/{id}")
public Book deleteBook(@PathVariable Long id) {
return bookService.deleteBook(id);
}
}

View File

@ -0,0 +1,43 @@
package com.example.demo.Books.Controller;
import com.example.demo.Books.Service.GenreService;
import com.example.demo.Books.Models.Genre;
import org.springframework.web.bind.annotation.*;
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.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);
}
}

View File

@ -0,0 +1,61 @@
package com.example.demo.Books.Models;
import jakarta.persistence.Entity;
import jakarta.persistence.*;
import java.util.List;
@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String surname;
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "authors")
private List<Book> books;
public Author() {}
public Author(String name, String surname) {
this.name = name;
this.surname = surname;
}
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<Book> getBooks() {
return books;
}
@Override
public String toString() {
String res = "\nBooks{" +
"id: " + id + "," +
"name: " + name + "," +
"surname: " + name + "," +
"books:" + (books == null ? "[]" : books.toString()) + "}"
;
return res;
}
}

View File

@ -0,0 +1,73 @@
package com.example.demo.Books.Models;
import jakarta.persistence.*;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import java.util.List;
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToMany
@LazyCollection(LazyCollectionOption.FALSE)
@JoinTable(name="books_genres",
joinColumns = @JoinColumn(name="book_id"),
inverseJoinColumns = @JoinColumn(name="genre_id")
)
private List<Genre> genres;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name="authors_books",
joinColumns = @JoinColumn(name="book_id"),
inverseJoinColumns = @JoinColumn(name="authors_id")
)
private List<Author> authors;
public Book() {}
public Book(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);
}
public void addAuthor(Author author) {
authors.add(author);
}
public List<Author> getAuthors() {
return authors;
}
public List<Genre> getGenres() {
return genres;
}
@Override
public String toString() {
String res = "\nBook{" +
"id: " + id + "," +
"name: " + name + "," +
"genres:" + (genres == null ? "[]" : genres.toString()) + "}"
;
return res;
}
}

View File

@ -0,0 +1,36 @@
package com.example.demo.Books.Models;
import jakarta.persistence.*;
@Entity
public class Genre {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
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 + '\'' +
'}';
}
}

View File

@ -0,0 +1,76 @@
package com.example.demo.Books.Service;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import com.example.demo.Books.Models.Author;
import jakarta.persistence.*;
@Service
public class AuthorService {
@PersistenceContext
private EntityManager em;
@Transactional
public Author addAuthor(String name, String surname) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Author name is null or empty");
}
Author author = new Author(name, surname);
em.persist(author);
return em.find(Author.class, author.getId());
}
@Transactional
public Author addAuthor(String name, String surname, String path) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Author name is null or empty");
}
Author author = new Author(name, surname);
em.persist(author);
return em.find(Author.class, author.getId());
}
@Transactional(readOnly = true)
public Author findAuthor(Long id) {
final Author author = em.find(Author.class, id);
if (author == null) {
throw new EntityNotFoundException(String.format("Author with id [%s] is not found", id));
}
return author;
}
@Transactional(readOnly = true)
public List<Author> findAllAuthors() {
return em.createQuery("select f from Author f", Author.class)
.getResultList();
}
@Transactional
public Author updateAuthor(Long id, String name, String surname) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Author name is null or empty");
}
final Author currentAuthor = findAuthor(id);
if(name != null) currentAuthor.setName(name);
if(surname != null) currentAuthor.setSurname(surname);
return em.merge(currentAuthor);
}
@Transactional
public Author deleteAuthor(Long id) {
final Author currentAuthor = findAuthor(id);
em.remove(currentAuthor);
return currentAuthor;
}
@Transactional
public void deleteAllAuthors() {
em.createQuery("delete from Author").executeUpdate();
}
}

View File

@ -0,0 +1,122 @@
package com.example.demo.Books.Service;
import jakarta.persistence.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.example.demo.Books.Models.Author;
import com.example.demo.Books.Models.Book;
import com.example.demo.Books.Models.Genre;
import java.util.List;
@Service
public class BookService {
@PersistenceContext
private EntityManager em;
@Transactional
public Book addBook(String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Book name is null or empty");
}
Book book = new Book(name);
em.persist(book);
return em.find(Book.class, book.getId());
}
@Transactional
public Book addGenre(Long bookId, Long genreId) {
final Book book = em.find(Book.class, bookId);
if (book == null) {
throw new EntityNotFoundException(String.format("Book with id [%s] is not found", bookId));
}
final Genre genre = em.find(Genre.class, genreId);
if (genre == null) {
throw new EntityNotFoundException(String.format("Genre with id [%s] is not found", genreId));
}
book.addGenre(genre);
return em.merge(book);
}
@Transactional
public Book addAuthor(Long bookId, Long authorId) {
final Book book = em.find(Book.class, bookId);
if (book == null) {
throw new EntityNotFoundException(String.format("Book with id [%s] is not found", bookId));
}
final Author author = em.find(Author.class, authorId);
if (author == null) {
throw new EntityNotFoundException(String.format("Genre with id [%s] is not found", authorId));
}
book.addAuthor(author);
return em.merge(book);
}
@Transactional(readOnly = true)
public Book findBook(Long id) {
final Book book = em.find(Book.class, id);
if (book == null) {
throw new EntityNotFoundException(String.format("Book with id [%s] is not found", id));
}
return book;
}
@Transactional(readOnly = true)
public List<Book> findBookByGenre(Long id, Long genreId) {
Query query = em.createQuery("select f from Book f " +
"join f.genres g " +
"where g.id = :genreId", Book.class)
.setParameter("genreId", genreId);
List<Book> result = query.getResultList();
return result;
}
@Transactional(readOnly = true)
public List<Book> findBookByAuthor(Long id, String name) {
Query query = em.createQuery("select f from Book f " +
"join f.authors a " +
"where a.name like :authorName", Book.class)
.setParameter("authorName", '%' + name + '%');
List<Book> result = query.getResultList();
return result;
}
@Transactional(readOnly = true)
public List<Book> findAllBooks() {
return em.createQuery("select f from Book f", Book.class)
.getResultList();
}
@Transactional
public Book updateBook(Long id, String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Book name is null or empty");
}
final Book currentBook = findBook(id);
currentBook.setName(name);
return em.merge(currentBook);
}
@Transactional
public Book deleteBook(Long id) {
final Book currentBook = findBook(id);
em.remove(currentBook);
return currentBook;
}
@Transactional
public void deleteAllBooks() {
em.createQuery("delete from Book").executeUpdate();
}
}

View File

@ -0,0 +1,65 @@
package com.example.demo.Books.Service;
import com.example.demo.Books.Models.Genre;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import jakarta.persistence.*;
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> 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();
}
}

View File

@ -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

View File

@ -0,0 +1,92 @@
package com.example.demo;
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 com.example.demo.Books.Models.Author;
import com.example.demo.Books.Models.Book;
import com.example.demo.Books.Models.Genre;
import com.example.demo.Books.Service.AuthorService;
import com.example.demo.Books.Service.BookService;
import com.example.demo.Books.Service.GenreService;
import jakarta.persistence.*;
import java.util.List;
@SpringBootTest
public class JpaAuthorsTest {
private static final Logger log = LoggerFactory.getLogger(JpaBooksTest.class);
@Autowired
BookService bookService;
@Autowired
AuthorService authorService;
@Test
void testBookCreate() {
authorService.deleteAllAuthors();
final Author author = authorService.addAuthor("Ivan", "Ivanov");
log.info(author.toString());
Assertions.assertNotNull(author.getId());
}
@Test
void testBookReadNotFound() {
authorService.deleteAllAuthors();
Assertions.assertThrows(EntityNotFoundException.class, () -> authorService.findAuthor(-1L));
}
@Test
void findBook() {
authorService.deleteAllAuthors();
final Author author = authorService.addAuthor("Ivan", "Ivanov");
log.info(author.toString());
final Author findAuthor = authorService.findAuthor(author.getId());
log.info(findAuthor.toString());
Assertions.assertEquals(author.toString(), findAuthor.toString());
}
@Test
void testReadAll() {
authorService.deleteAllAuthors();
final Author author = authorService.addAuthor("Ivan", "Ivanov");
log.info(author.toString());
final List<Author> authors = authorService.findAllAuthors();
Assertions.assertEquals(authors.size(), 1);
}
@Test
void updateAuthor() {
authorService.deleteAllAuthors();
final Author author = authorService.addAuthor("Ivan", "Ivanov");
authorService.updateAuthor(author.getId(), "Alexei", null);
final Author findAuthor = authorService.findAuthor(author.getId());
log.info(findAuthor.toString());
Assertions.assertEquals(findAuthor.getName() + " " + findAuthor.getSurname(), "Alexei Ivanov");
}
@Test
void deleteAuthor() {
authorService.deleteAllAuthors();
final Author author = authorService.addAuthor("Ivan", "Ivanov");
log.info(author.toString());
authorService.deleteAuthor(author.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> authorService.findAuthor(author.getId()));
}
@Test
void addBook() {
authorService.deleteAllAuthors();
bookService.findAllBooks();
final Author author = authorService.addAuthor("Ivan", "Ivanov");
final Book book = bookService.addBook("book");
bookService.addAuthor(book.getId(), author.getId());
final Author findAuthor = authorService.findAuthor(author.getId());
Assertions.assertEquals(findAuthor.getBooks().get(0).getId(), book.getId());
}
}

View File

@ -0,0 +1,139 @@
package com.example.demo;
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 com.example.demo.Books.Models.Author;
import com.example.demo.Books.Models.Book;
import com.example.demo.Books.Models.Genre;
import com.example.demo.Books.Service.AuthorService;
import com.example.demo.Books.Service.BookService;
import com.example.demo.Books.Service.GenreService;
import jakarta.persistence.*;
import java.util.List;
@SpringBootTest
public class JpaBooksTest {
private static final Logger log = LoggerFactory.getLogger(JpaBooksTest.class);
@Autowired
BookService bookService;
@Autowired
GenreService genreService;
@Autowired
AuthorService authorService;
@Test
void testBookCreate() {
bookService.deleteAllBooks();
final Book book = bookService.addBook("book");
log.info(book.toString());
Assertions.assertNotNull(book.getId());
}
@Test
void testBookReadNotFound() {
bookService.deleteAllBooks();
Assertions.assertThrows(EntityNotFoundException.class, () -> bookService.findBook(-1L));
}
@Test
void findBook() {
bookService.deleteAllBooks();
final Book book = bookService.addBook("book");
log.info(book.toString());
final Book findBook = bookService.findBook(book.getId());
log.info(findBook.toString());
Assertions.assertEquals(book.toString(), findBook.toString());
}
@Test
void updateBook() {
bookService.deleteAllBooks();
final Book book = bookService.addBook("book");
bookService.updateBook(book.getId(), "book 2");
final Book findBook = bookService.findBook(book.getId());
log.info(findBook.toString());
Assertions.assertEquals(findBook.getName(), "book 2");
}
@Test
void testReadAll() {
bookService.deleteAllBooks();
final Book book = bookService.addBook("book");
log.info(book.toString());
final List<Book> books = bookService.findAllBooks();
Assertions.assertEquals(books.size(), 1);
}
@Test
void deleteBook() {
bookService.deleteAllBooks();
final Book book = bookService.addBook("book");
log.info(book.toString());
bookService.deleteBook(book.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> bookService.findBook(book.getId()));
}
@Test
void findBooksByGenres() {
bookService.deleteAllBooks();
genreService.deleteAllGenres();
final Book book = bookService.addBook("book");
bookService.addBook("book 2");
final Genre genre = genreService.addGenre("Comedy");
bookService.addGenre(book.getId(), genre.getId());
final List<Book> books = bookService.findBookByGenre(book.getId(), genre.getId());
Assertions.assertEquals(books.size(), 1);
}
@Test
void findBooksByActor() {
bookService.deleteAllBooks();
genreService.deleteAllGenres();
final Book book = bookService.addBook("book");
final Book book2 = bookService.addBook("book 2");
bookService.addBook("book 3");
final Author author = authorService.addAuthor("Ivan", "Ivanov");
final Author author2 = authorService.addAuthor("Alexei", "Ivanov");
bookService.addAuthor(book.getId(), author.getId());
bookService.addAuthor(book2.getId(), author2.getId());
final List<Book> books = bookService.findBookByAuthor(book.getId(), "Alexei");
Assertions.assertEquals(books.size(), 2);
}
@Test
void addGenre() {
bookService.deleteAllBooks();
genreService.deleteAllGenres();
final Book book = bookService.addBook("book");
final Genre genre = genreService.addGenre("Comedy");
bookService.addGenre(book.getId(), genre.getId());
final Book findBook = bookService.findBook(book.getId());
Assertions.assertEquals(findBook.getGenres().get(0).toString(), genre.toString());
}
@Test
void addAuthor() {
bookService.deleteAllBooks();
genreService.deleteAllGenres();
final Book book = bookService.addBook("book");
final Author author = authorService.addAuthor("Ivan", "Ivanov");
bookService.addAuthor(book.getId(), author.getId());
final Book findBook = bookService.findBook(book.getId());
Assertions.assertEquals(findBook.getAuthors().get(0).getId(), author.getId());
}
}

View File

@ -0,0 +1,76 @@
package com.example.demo;
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 com.example.demo.Books.Models.Genre;
import com.example.demo.Books.Service.BookService;
import com.example.demo.Books.Service.GenreService;
import jakarta.persistence.*;
import java.util.List;
@SpringBootTest
public class JpaGenresTest {
private static final Logger log = LoggerFactory.getLogger(JpaGenresTest.class);
@Autowired
BookService bookService;
@Autowired
GenreService genreService;
@Test
void testGenreCreate() {
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Comedy");
log.info(genre.toString());
Assertions.assertNotNull(genre.getId());
}
@Test
void testGenreReadNotFound() {
genreService.deleteAllGenres();
Assertions.assertThrows(EntityNotFoundException.class, () -> genreService.findGenre(-1L));
}
@Test
void findGenre() {
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Comedy");
log.info(genre.toString());
final Genre findGenre = genreService.findGenre(genre.getId());
log.info(findGenre.toString());
Assertions.assertEquals(genre.toString(), findGenre.toString());
}
@Test
void testReadAll() {
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Comedy");
log.info(genre.toString());
final List<Genre> genres = genreService.findAllGenres();
Assertions.assertEquals(genres.size(), 1);
}
@Test
void updateGenre() {
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Comedy");
genreService.updateGenre(genre.getId(), "Roman");
final Genre findGenre = genreService.findGenre(genre.getId());
log.info(findGenre.toString());
Assertions.assertEquals(findGenre.getName(), "Roman");
}
@Test
void deleteGenre() {
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Comedy");
log.info(genre.toString());
genreService.deleteGenre(genre.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> bookService.findBook(genre.getId()));
}
}