From 53dc05e85b31caefda67e84afeb9a50ce9590a05 Mon Sep 17 00:00:00 2001 From: stvort Date: Fri, 17 Nov 2023 01:38:26 +0400 Subject: [PATCH 1/3] templates updated --- templates/hw01-xml-config/pom.xml | 4 +- .../{AppConfig.java => AppProperties.java} | 2 +- .../java/ru/otus/hw/dao/CsvQuestionDao.java | 1 + templates/hw02-annotation-config/pom.xml | 4 +- .../{AppConfig.java => AppProperties.java} | 4 +- .../java/ru/otus/hw/dao/CsvQuestionDao.java | 1 + templates/hw03-spring-boot/pom.xml | 2 +- .../{AppConfig.java => AppProperties.java} | 3 +- .../java/ru/otus/hw/dao/CsvQuestionDao.java | 1 + .../ru/otus/hw/commands/BookCommands.java | 52 ------- .../ru/otus/hw/converters/BookConverter.java | 27 ---- .../src/main/java/ru/otus/hw/models/Book.java | 20 --- .../hw/repositories/AuthorRepositoryJdbc.java | 33 ----- .../hw/repositories/BookRepositoryJdbc.java | 112 --------------- .../otus/hw/repositories/GenreRepository.java | 11 -- .../hw/repositories/GenreRepositoryJdbc.java | 32 ----- .../java/ru/otus/hw/services/BookService.java | 18 --- .../ru/otus/hw/services/BookServiceImpl.java | 60 -------- .../src/main/resources/data.sql | 14 -- .../src/main/resources/schema.sql | 24 ---- .../repositories/BookRepositoryJdbcTest.java | 134 ------------------ .../src/test/resources/data.sql | 14 -- .../.gitignore | 1 - .../pom.xml | 2 +- .../src/main/java/ru/otus/hw/Application.java | 0 .../ru/otus/hw/commands/AuthorCommands.java | 25 ---- .../ru/otus/hw/commands/GenreCommands.java | 25 ---- .../otus/hw/converters/AuthorConverter.java | 11 -- .../ru/otus/hw/converters/GenreConverter.java | 11 -- .../exceptions/EntityNotFoundException.java | 7 - .../main/java/ru/otus/hw/models/Author.java | 14 -- .../main/java/ru/otus/hw/models/Genre.java | 14 -- .../hw/repositories/AuthorRepository.java | 12 -- .../otus/hw/repositories/BookRepository.java | 16 --- .../ru/otus/hw/services/AuthorService.java | 9 -- .../otus/hw/services/AuthorServiceImpl.java | 19 --- .../ru/otus/hw/services/GenreService.java | 9 -- .../ru/otus/hw/services/GenreServiceImpl.java | 19 --- .../src/main/resources/application.yml | 8 -- .../src/test/resources/application.yml | 8 -- .../.gitignore | 1 - .../pom.xml | 2 +- .../src/main/java/ru/otus/hw/Application.java | 0 .../ru/otus/hw/commands/AuthorCommands.java | 0 .../ru/otus/hw/commands/BookCommands.java | 3 + .../ru/otus/hw/commands/GenreCommands.java | 0 .../otus/hw/converters/AuthorConverter.java | 0 .../ru/otus/hw/converters/BookConverter.java | 0 .../ru/otus/hw/converters/GenreConverter.java | 0 .../exceptions/EntityNotFoundException.java | 0 .../main/java/ru/otus/hw/models/Author.java | 0 .../src/main/java/ru/otus/hw/models/Book.java | 0 .../main/java/ru/otus/hw/models/Genre.java | 0 .../hw/repositories/AuthorRepository.java | 0 .../otus/hw/repositories/BookRepository.java | 0 .../otus/hw/repositories/GenreRepository.java | 0 .../repositories/JdbcAuthorRepository.java} | 2 +- .../hw/repositories/JdbcBookRepository.java} | 3 +- .../hw/repositories/JdbcGenreRepository.java} | 2 +- .../ru/otus/hw/services/AuthorService.java | 0 .../otus/hw/services/AuthorServiceImpl.java | 0 .../java/ru/otus/hw/services/BookService.java | 0 .../ru/otus/hw/services/BookServiceImpl.java | 0 .../ru/otus/hw/services/GenreService.java | 0 .../ru/otus/hw/services/GenreServiceImpl.java | 0 .../src/main/resources/application.yml | 0 .../src/main/resources/data.sql | 0 .../src/main/resources/schema.sql | 0 .../repositories/JdbcBookRepositoryTest.java} | 6 +- .../src/test/resources/application.yml | 0 .../src/test/resources/data.sql | 0 71 files changed, 25 insertions(+), 777 deletions(-) rename templates/hw01-xml-config/src/main/java/ru/otus/hw/config/{AppConfig.java => AppProperties.java} (75%) rename templates/hw02-annotation-config/src/main/java/ru/otus/hw/config/{AppConfig.java => AppProperties.java} (79%) rename templates/hw03-spring-boot/src/main/java/ru/otus/hw/config/{AppConfig.java => AppProperties.java} (87%) delete mode 100644 templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/commands/BookCommands.java delete mode 100644 templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/converters/BookConverter.java delete mode 100644 templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/models/Book.java delete mode 100644 templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/AuthorRepositoryJdbc.java delete mode 100644 templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/BookRepositoryJdbc.java delete mode 100644 templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/GenreRepository.java delete mode 100644 templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/GenreRepositoryJdbc.java delete mode 100644 templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/services/BookService.java delete mode 100644 templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/services/BookServiceImpl.java delete mode 100644 templates/hw05-jdbc-hard-template/src/main/resources/data.sql delete mode 100644 templates/hw05-jdbc-hard-template/src/main/resources/schema.sql delete mode 100644 templates/hw05-jdbc-hard-template/src/test/java/ru/otus/hw/repositories/BookRepositoryJdbcTest.java delete mode 100644 templates/hw05-jdbc-hard-template/src/test/resources/data.sql rename templates/{hw05-jdbc-simple-template => hw05-jdbc-hard}/.gitignore (94%) rename templates/{hw05-jdbc-hard-template => hw05-jdbc-hard}/pom.xml (99%) rename templates/{hw05-jdbc-hard-template => hw05-jdbc-hard}/src/main/java/ru/otus/hw/Application.java (100%) delete mode 100644 templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/commands/AuthorCommands.java delete mode 100644 templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/commands/GenreCommands.java delete mode 100644 templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/converters/AuthorConverter.java delete mode 100644 templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/converters/GenreConverter.java delete mode 100644 templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/exceptions/EntityNotFoundException.java delete mode 100644 templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/models/Author.java delete mode 100644 templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/models/Genre.java delete mode 100644 templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/AuthorRepository.java delete mode 100644 templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/BookRepository.java delete mode 100644 templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/AuthorService.java delete mode 100644 templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/AuthorServiceImpl.java delete mode 100644 templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/GenreService.java delete mode 100644 templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/GenreServiceImpl.java delete mode 100644 templates/hw05-jdbc-simple-template/src/main/resources/application.yml delete mode 100644 templates/hw05-jdbc-simple-template/src/test/resources/application.yml rename templates/{hw05-jdbc-hard-template => hw05-jdbc-simple}/.gitignore (94%) rename templates/{hw05-jdbc-simple-template => hw05-jdbc-simple}/pom.xml (99%) rename templates/{hw05-jdbc-simple-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/Application.java (100%) rename templates/{hw05-jdbc-hard-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/commands/AuthorCommands.java (100%) rename templates/{hw05-jdbc-simple-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/commands/BookCommands.java (91%) rename templates/{hw05-jdbc-hard-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/commands/GenreCommands.java (100%) rename templates/{hw05-jdbc-hard-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/converters/AuthorConverter.java (100%) rename templates/{hw05-jdbc-simple-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/converters/BookConverter.java (100%) rename templates/{hw05-jdbc-hard-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/converters/GenreConverter.java (100%) rename templates/{hw05-jdbc-hard-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/exceptions/EntityNotFoundException.java (100%) rename templates/{hw05-jdbc-hard-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/models/Author.java (100%) rename templates/{hw05-jdbc-simple-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/models/Book.java (100%) rename templates/{hw05-jdbc-hard-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/models/Genre.java (100%) rename templates/{hw05-jdbc-hard-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/repositories/AuthorRepository.java (100%) rename templates/{hw05-jdbc-hard-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/repositories/BookRepository.java (100%) rename templates/{hw05-jdbc-simple-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/repositories/GenreRepository.java (100%) rename templates/{hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/AuthorRepositoryJdbc.java => hw05-jdbc-simple/src/main/java/ru/otus/hw/repositories/JdbcAuthorRepository.java} (91%) rename templates/{hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/BookRepositoryJdbc.java => hw05-jdbc-simple/src/main/java/ru/otus/hw/repositories/JdbcBookRepository.java} (87%) rename templates/{hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/GenreRepositoryJdbc.java => hw05-jdbc-simple/src/main/java/ru/otus/hw/repositories/JdbcGenreRepository.java} (91%) rename templates/{hw05-jdbc-hard-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/services/AuthorService.java (100%) rename templates/{hw05-jdbc-hard-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/services/AuthorServiceImpl.java (100%) rename templates/{hw05-jdbc-simple-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/services/BookService.java (100%) rename templates/{hw05-jdbc-simple-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/services/BookServiceImpl.java (100%) rename templates/{hw05-jdbc-hard-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/services/GenreService.java (100%) rename templates/{hw05-jdbc-hard-template => hw05-jdbc-simple}/src/main/java/ru/otus/hw/services/GenreServiceImpl.java (100%) rename templates/{hw05-jdbc-hard-template => hw05-jdbc-simple}/src/main/resources/application.yml (100%) rename templates/{hw05-jdbc-simple-template => hw05-jdbc-simple}/src/main/resources/data.sql (100%) rename templates/{hw05-jdbc-simple-template => hw05-jdbc-simple}/src/main/resources/schema.sql (100%) rename templates/{hw05-jdbc-simple-template/src/test/java/ru/otus/hw/repositories/BookRepositoryJdbcTest.java => hw05-jdbc-simple/src/test/java/ru/otus/hw/repositories/JdbcBookRepositoryTest.java} (96%) rename templates/{hw05-jdbc-hard-template => hw05-jdbc-simple}/src/test/resources/application.yml (100%) rename templates/{hw05-jdbc-simple-template => hw05-jdbc-simple}/src/test/resources/data.sql (100%) diff --git a/templates/hw01-xml-config/pom.xml b/templates/hw01-xml-config/pom.xml index a16f06356..cd2909ecb 100644 --- a/templates/hw01-xml-config/pom.xml +++ b/templates/hw01-xml-config/pom.xml @@ -13,12 +13,12 @@ 17 17 UTF-8 - 6.0.12 + 6.0.13 5.8 5.10.0 5.4.0 3.24.2 - 1.18.28 + 1.18.30 3.2.2 10.11.0 diff --git a/templates/hw01-xml-config/src/main/java/ru/otus/hw/config/AppConfig.java b/templates/hw01-xml-config/src/main/java/ru/otus/hw/config/AppProperties.java similarity index 75% rename from templates/hw01-xml-config/src/main/java/ru/otus/hw/config/AppConfig.java rename to templates/hw01-xml-config/src/main/java/ru/otus/hw/config/AppProperties.java index def57dd36..08f8f182d 100644 --- a/templates/hw01-xml-config/src/main/java/ru/otus/hw/config/AppConfig.java +++ b/templates/hw01-xml-config/src/main/java/ru/otus/hw/config/AppProperties.java @@ -5,7 +5,7 @@ @AllArgsConstructor @Data -public class AppConfig implements TestFileNameProvider { +public class AppProperties implements TestFileNameProvider { private String testFileName; diff --git a/templates/hw01-xml-config/src/main/java/ru/otus/hw/dao/CsvQuestionDao.java b/templates/hw01-xml-config/src/main/java/ru/otus/hw/dao/CsvQuestionDao.java index 3941a647d..b77547f3d 100644 --- a/templates/hw01-xml-config/src/main/java/ru/otus/hw/dao/CsvQuestionDao.java +++ b/templates/hw01-xml-config/src/main/java/ru/otus/hw/dao/CsvQuestionDao.java @@ -16,6 +16,7 @@ public List findAll() { // Использовать CsvToBean // https://opencsv.sourceforge.net/#collection_based_bean_fields_one_to_many_mappings // Использовать QuestionReadException + // Про ресурсы: https://mkyong.com/java/java-read-a-file-from-resources-folder/ return new ArrayList<>(); } diff --git a/templates/hw02-annotation-config/pom.xml b/templates/hw02-annotation-config/pom.xml index c99477bcd..8faef9826 100644 --- a/templates/hw02-annotation-config/pom.xml +++ b/templates/hw02-annotation-config/pom.xml @@ -13,12 +13,12 @@ 17 17 UTF-8 - 6.0.12 + 6.0.13 5.8 5.10.0 5.4.0 3.24.2 - 1.18.28 + 1.18.30 3.2.2 10.11.0 diff --git a/templates/hw02-annotation-config/src/main/java/ru/otus/hw/config/AppConfig.java b/templates/hw02-annotation-config/src/main/java/ru/otus/hw/config/AppProperties.java similarity index 79% rename from templates/hw02-annotation-config/src/main/java/ru/otus/hw/config/AppConfig.java rename to templates/hw02-annotation-config/src/main/java/ru/otus/hw/config/AppProperties.java index 974574881..6ff25ebad 100644 --- a/templates/hw02-annotation-config/src/main/java/ru/otus/hw/config/AppConfig.java +++ b/templates/hw02-annotation-config/src/main/java/ru/otus/hw/config/AppProperties.java @@ -1,8 +1,10 @@ package ru.otus.hw.config; +import lombok.Setter; import org.springframework.beans.factory.annotation.Value; -public class AppConfig implements TestConfig, TestFileNameProvider { +@Setter +public class AppProperties implements TestConfig, TestFileNameProvider { // внедрить свойство из application.properties private int rightAnswersCountToPass; diff --git a/templates/hw02-annotation-config/src/main/java/ru/otus/hw/dao/CsvQuestionDao.java b/templates/hw02-annotation-config/src/main/java/ru/otus/hw/dao/CsvQuestionDao.java index 3941a647d..b77547f3d 100644 --- a/templates/hw02-annotation-config/src/main/java/ru/otus/hw/dao/CsvQuestionDao.java +++ b/templates/hw02-annotation-config/src/main/java/ru/otus/hw/dao/CsvQuestionDao.java @@ -16,6 +16,7 @@ public List findAll() { // Использовать CsvToBean // https://opencsv.sourceforge.net/#collection_based_bean_fields_one_to_many_mappings // Использовать QuestionReadException + // Про ресурсы: https://mkyong.com/java/java-read-a-file-from-resources-folder/ return new ArrayList<>(); } diff --git a/templates/hw03-spring-boot/pom.xml b/templates/hw03-spring-boot/pom.xml index dc0ff1ef2..6647c64ac 100644 --- a/templates/hw03-spring-boot/pom.xml +++ b/templates/hw03-spring-boot/pom.xml @@ -20,7 +20,7 @@ 17 UTF-8 5.8 - 1.18.28 + 1.18.30 3.2.2 10.11.0 diff --git a/templates/hw03-spring-boot/src/main/java/ru/otus/hw/config/AppConfig.java b/templates/hw03-spring-boot/src/main/java/ru/otus/hw/config/AppProperties.java similarity index 87% rename from templates/hw03-spring-boot/src/main/java/ru/otus/hw/config/AppConfig.java rename to templates/hw03-spring-boot/src/main/java/ru/otus/hw/config/AppProperties.java index c202024d2..4ffaf6877 100644 --- a/templates/hw03-spring-boot/src/main/java/ru/otus/hw/config/AppConfig.java +++ b/templates/hw03-spring-boot/src/main/java/ru/otus/hw/config/AppProperties.java @@ -9,9 +9,8 @@ @Setter // Использовать @ConfigurationProperties. // Сейчас класс соответствует файлу настроек. Чтобы они сюда отобразились нужно только правильно разместить аннотации -public class AppConfig implements TestConfig, TestFileNameProvider, LocaleConfig { +public class AppProperties implements TestConfig, TestFileNameProvider, LocaleConfig { - @Getter private int rightAnswersCountToPass; @Getter diff --git a/templates/hw03-spring-boot/src/main/java/ru/otus/hw/dao/CsvQuestionDao.java b/templates/hw03-spring-boot/src/main/java/ru/otus/hw/dao/CsvQuestionDao.java index 9551a8b7a..f940d8cae 100644 --- a/templates/hw03-spring-boot/src/main/java/ru/otus/hw/dao/CsvQuestionDao.java +++ b/templates/hw03-spring-boot/src/main/java/ru/otus/hw/dao/CsvQuestionDao.java @@ -18,6 +18,7 @@ public List findAll() { // Использовать CsvToBean // https://opencsv.sourceforge.net/#collection_based_bean_fields_one_to_many_mappings // Использовать QuestionReadException + // Про ресурсы: https://mkyong.com/java/java-read-a-file-from-resources-folder/ return new ArrayList<>(); } diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/commands/BookCommands.java b/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/commands/BookCommands.java deleted file mode 100644 index 0716dd6a7..000000000 --- a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/commands/BookCommands.java +++ /dev/null @@ -1,52 +0,0 @@ -package ru.otus.hw.commands; - -import lombok.RequiredArgsConstructor; -import org.springframework.shell.standard.ShellComponent; -import org.springframework.shell.standard.ShellMethod; -import ru.otus.hw.converters.BookConverter; -import ru.otus.hw.services.BookService; - -import java.util.List; -import java.util.stream.Collectors; - -@RequiredArgsConstructor -@ShellComponent -public class BookCommands { - - private final BookService bookService; - - private final BookConverter bookConverter; - - @ShellMethod(value = "Find all books", key = "ab") - public String findAllBooks() { - return bookService.findAll().stream() - .map(bookConverter::bookToString) - .collect(Collectors.joining("," + System.lineSeparator())); - } - - @ShellMethod(value = "Find book by id", key = "bbid") - public String findBookById(long id) { - return bookService.findById(id) - .map(bookConverter::bookToString) - .orElse("Book with id %d not found".formatted(id)); - } - - //bins aaaaaaaaaaaaa 1 1,6//bins aaaaaaaaaaaaa 1 1,6 - @ShellMethod(value = "Insert book", key = "bins") - public String insertBook(String title, long authorId, List genresIds) { - var savedBook = bookService.insert(title, authorId, genresIds); - return bookConverter.bookToString(savedBook); - } - - //bupd 4 dfasdfasdfasd 3 2,5 - @ShellMethod(value = "Update book", key = "bupd") - public String updateBook(long id, String title, long authorId, List genresIds) { - var savedBook = bookService.update(id, title, authorId, genresIds); - return bookConverter.bookToString(savedBook); - } - - @ShellMethod(value = "Delete book by id", key = "bdel") - public void updateBook(long id) { - bookService.deleteById(id); - } -} diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/converters/BookConverter.java b/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/converters/BookConverter.java deleted file mode 100644 index c174791d3..000000000 --- a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/converters/BookConverter.java +++ /dev/null @@ -1,27 +0,0 @@ -package ru.otus.hw.converters; - -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Component; -import ru.otus.hw.models.Book; - -import java.util.stream.Collectors; - -@RequiredArgsConstructor -@Component -public class BookConverter { - private final AuthorConverter authorConverter; - - private final GenreConverter genreConverter; - - public String bookToString(Book book) { - var genresString = book.getGenres().stream() - .map(genreConverter::genreToString) - .map("{%s}"::formatted) - .collect(Collectors.joining(", ")); - return "Id: %d, title: %s, author: {%s}, genres: [%s]".formatted( - book.getId(), - book.getTitle(), - authorConverter.authorToString(book.getAuthor()), - genresString); - } -} diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/models/Book.java b/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/models/Book.java deleted file mode 100644 index c71d15e16..000000000 --- a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/models/Book.java +++ /dev/null @@ -1,20 +0,0 @@ -package ru.otus.hw.models; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -import java.util.List; - -@Data -@AllArgsConstructor -@NoArgsConstructor -public class Book { - private long id; - - private String title; - - private Author author; - - private List genres; -} diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/AuthorRepositoryJdbc.java b/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/AuthorRepositoryJdbc.java deleted file mode 100644 index b12c0c2c1..000000000 --- a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/AuthorRepositoryJdbc.java +++ /dev/null @@ -1,33 +0,0 @@ -package ru.otus.hw.repositories; - -import org.springframework.jdbc.core.RowMapper; -import org.springframework.stereotype.Repository; -import ru.otus.hw.models.Author; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; - -@Repository -public class AuthorRepositoryJdbc implements AuthorRepository { - - @Override - public List findAll() { - return new ArrayList<>(); - } - - @Override - public Optional findById(long id) { - return Optional.empty(); - } - - private static class AuthorRowMapper implements RowMapper { - - @Override - public Author mapRow(ResultSet rs, int i) throws SQLException { - return null; - } - } -} diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/BookRepositoryJdbc.java b/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/BookRepositoryJdbc.java deleted file mode 100644 index bb99e66e0..000000000 --- a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/BookRepositoryJdbc.java +++ /dev/null @@ -1,112 +0,0 @@ -package ru.otus.hw.repositories; - -import lombok.RequiredArgsConstructor; -import org.springframework.dao.DataAccessException; -import org.springframework.jdbc.core.ResultSetExtractor; -import org.springframework.jdbc.core.RowMapper; -import org.springframework.jdbc.support.GeneratedKeyHolder; -import org.springframework.stereotype.Repository; -import ru.otus.hw.models.Book; -import ru.otus.hw.models.Genre; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; - -@Repository -@RequiredArgsConstructor -public class BookRepositoryJdbc implements BookRepository { - - private final GenreRepository genreRepository; - - @Override - public Optional findById(long id) { - return Optional.empty(); - } - - @Override - public List findAll() { - var genres = genreRepository.findAll(); - var relations = getAllGenreRelations(); - var books = getAllBooksWithoutGenres(); - mergeBooksInfo(books, genres, relations); - return books; - } - - @Override - public Book save(Book book) { - if (book.getId() == 0) { - return insert(book); - } - return update(book); - } - - @Override - public void deleteById(long id) { - //... - } - - private List getAllBooksWithoutGenres() { - return new ArrayList<>(); - } - - private List getAllGenreRelations() { - return new ArrayList<>(); - } - - private void mergeBooksInfo(List booksWithoutGenres, List genres, - List relations) { - // Добавить книгам (booksWithoutGenres) жанры (genres) в соответствии со связями (relations) - } - - private Book insert(Book book) { - var keyHolder = new GeneratedKeyHolder(); - - //... - - //noinspection DataFlowIssue - book.setId(keyHolder.getKeyAs(Long.class)); - batchInsertGenresRelationsFor(book); - return book; - } - - private Book update(Book book) { - //... - - removeGenresRelationsFor(book); - batchInsertGenresRelationsFor(book); - - return book; - } - - private void batchInsertGenresRelationsFor(Book book) { - // batchUpdate - } - - private void removeGenresRelationsFor(Book book) { - //... - } - - private static class BookRowMapper implements RowMapper { - - @Override - public Book mapRow(ResultSet rs, int rowNum) throws SQLException { - return null; - } - } - - @SuppressWarnings("ClassCanBeRecord") - @RequiredArgsConstructor - private static class BookResultSetExtractor implements ResultSetExtractor> { - - @Override - public List extractData(ResultSet rs) throws SQLException, DataAccessException { - return new ArrayList<>(); - } - } - - private record BookGenreRelation(long bookId, long genreId) { - } -} diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/GenreRepository.java b/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/GenreRepository.java deleted file mode 100644 index 02e87b108..000000000 --- a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/GenreRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package ru.otus.hw.repositories; - -import ru.otus.hw.models.Genre; - -import java.util.List; - -public interface GenreRepository { - List findAll(); - - List findAllByIds(List ids); -} diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/GenreRepositoryJdbc.java b/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/GenreRepositoryJdbc.java deleted file mode 100644 index e87daa58f..000000000 --- a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/GenreRepositoryJdbc.java +++ /dev/null @@ -1,32 +0,0 @@ -package ru.otus.hw.repositories; - -import org.springframework.jdbc.core.RowMapper; -import org.springframework.stereotype.Repository; -import ru.otus.hw.models.Genre; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; - -@Repository -public class GenreRepositoryJdbc implements GenreRepository { - - @Override - public List findAll() { - return new ArrayList<>(); - } - - @Override - public List findAllByIds(List ids) { - return new ArrayList<>(); - } - - private static class GnreRowMapper implements RowMapper { - - @Override - public Genre mapRow(ResultSet rs, int i) throws SQLException { - return null; - } - } -} diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/services/BookService.java b/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/services/BookService.java deleted file mode 100644 index 8b37a4b02..000000000 --- a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/services/BookService.java +++ /dev/null @@ -1,18 +0,0 @@ -package ru.otus.hw.services; - -import ru.otus.hw.models.Book; - -import java.util.List; -import java.util.Optional; - -public interface BookService { - Optional findById(long id); - - List findAll(); - - Book insert(String title, long authorId, List genresIds); - - Book update(long id, String title, long authorId, List genresIds); - - void deleteById(long id); -} diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/services/BookServiceImpl.java b/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/services/BookServiceImpl.java deleted file mode 100644 index a6495210f..000000000 --- a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/services/BookServiceImpl.java +++ /dev/null @@ -1,60 +0,0 @@ -package ru.otus.hw.services; - -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; -import ru.otus.hw.exceptions.EntityNotFoundException; -import ru.otus.hw.models.Book; -import ru.otus.hw.repositories.AuthorRepository; -import ru.otus.hw.repositories.BookRepository; -import ru.otus.hw.repositories.GenreRepository; - -import java.util.List; -import java.util.Optional; - -import static org.springframework.util.CollectionUtils.isEmpty; - -@RequiredArgsConstructor -@Service -public class BookServiceImpl implements BookService { - private final AuthorRepository authorRepository; - - private final GenreRepository genreRepository; - - private final BookRepository bookRepository; - - @Override - public Optional findById(long id) { - return bookRepository.findById(id); - } - - @Override - public List findAll() { - return bookRepository.findAll(); - } - - @Override - public Book insert(String title, long authorId, List genresIds) { - return save(0, title, authorId, genresIds); - } - - @Override - public Book update(long id, String title, long authorId, List genresIds) { - return save(id, title, authorId, genresIds); - } - - @Override - public void deleteById(long id) { - bookRepository.deleteById(id); - } - - private Book save(long id, String title, long authorId, List genresIds) { - var author = authorRepository.findById(authorId) - .orElseThrow(() -> new EntityNotFoundException("Author with id %d not found".formatted(authorId))); - var genres = genreRepository.findAllByIds(genresIds); - if (isEmpty(genres)) { - throw new EntityNotFoundException("Genres with ids %s not found".formatted(genresIds)); - } - var book = new Book(id, title, author, genres); - return bookRepository.save(book); - } -} diff --git a/templates/hw05-jdbc-hard-template/src/main/resources/data.sql b/templates/hw05-jdbc-hard-template/src/main/resources/data.sql deleted file mode 100644 index 3dd3a1664..000000000 --- a/templates/hw05-jdbc-hard-template/src/main/resources/data.sql +++ /dev/null @@ -1,14 +0,0 @@ -insert into authors(full_name) -values ('Author_1'), ('Author_2'), ('Author_3'); - -insert into genres(name) -values ('Genre_1'), ('Genre_2'), ('Genre_3'), - ('Genre_4'), ('Genre_5'), ('Genre_6'); - -insert into books(title, author_id) -values ('BookTitle_1', 1), ('BookTitle_2', 2), ('BookTitle_3', 3); - -insert into books_genres(book_id, genre_id) -values (1, 1), (1, 2), - (2, 3), (2, 4), - (3, 5), (3, 6); diff --git a/templates/hw05-jdbc-hard-template/src/main/resources/schema.sql b/templates/hw05-jdbc-hard-template/src/main/resources/schema.sql deleted file mode 100644 index b085e18a8..000000000 --- a/templates/hw05-jdbc-hard-template/src/main/resources/schema.sql +++ /dev/null @@ -1,24 +0,0 @@ -create table authors ( - id bigserial, - full_name varchar(255), - primary key (id) -); - -create table genres ( - id bigserial, - name varchar(255), - primary key (id) -); - -create table books ( - id bigserial, - title varchar(255), - author_id bigint references authors (id) on delete cascade, - primary key (id) -); - -create table books_genres ( - book_id bigint references books(id) on delete cascade, - genre_id bigint references genres(id) on delete cascade, - primary key (book_id, genre_id) -); \ No newline at end of file diff --git a/templates/hw05-jdbc-hard-template/src/test/java/ru/otus/hw/repositories/BookRepositoryJdbcTest.java b/templates/hw05-jdbc-hard-template/src/test/java/ru/otus/hw/repositories/BookRepositoryJdbcTest.java deleted file mode 100644 index a226d7ab7..000000000 --- a/templates/hw05-jdbc-hard-template/src/test/java/ru/otus/hw/repositories/BookRepositoryJdbcTest.java +++ /dev/null @@ -1,134 +0,0 @@ -package ru.otus.hw.repositories; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; -import org.springframework.context.annotation.Import; -import ru.otus.hw.models.Author; -import ru.otus.hw.models.Book; -import ru.otus.hw.models.Genre; - -import java.util.List; -import java.util.stream.IntStream; - -import static org.assertj.core.api.Assertions.assertThat; - -@DisplayName("Репозиторий на основе Jdbc для работы с книгами ") -@JdbcTest -@Import({BookRepositoryJdbc.class, GenreRepositoryJdbc.class}) -class BookRepositoryJdbcTest { - - @Autowired - private BookRepositoryJdbc repositoryJdbc; - - private List dbAuthors; - - private List dbGenres; - - private List dbBooks; - - @BeforeEach - void setUp() { - dbAuthors = getDbAuthors(); - dbGenres = getDbGenres(); - dbBooks = getDbBooks(dbAuthors, dbGenres); - } - - @DisplayName("должен загружать книгу по id") - @ParameterizedTest - @MethodSource("getDbBooks") - void shouldReturnCorrectBookById(Book expectedBook) { - var actualBook = repositoryJdbc.findById(expectedBook.getId()); - assertThat(actualBook).isPresent() - .get() - .isEqualTo(expectedBook); - } - - @DisplayName("должен загружать список всех книг") - @Test - void shouldReturnCorrectBooksList() { - var actualBooks = repositoryJdbc.findAll(); - var expectedBooks = dbBooks; - - assertThat(actualBooks).containsExactlyElementsOf(expectedBooks); - actualBooks.forEach(System.out::println); - } - - @DisplayName("должен сохранять новую книгу") - @Test - void shouldSaveNewBook() { - var expectedBook = new Book(0, "BookTitle_10500", dbAuthors.get(0), - List.of(dbGenres.get(0), dbGenres.get(2))); - var returnedBook = repositoryJdbc.save(expectedBook); - assertThat(returnedBook).isNotNull() - .matches(book -> book.getId() > 0) - .usingRecursiveComparison().ignoringExpectedNullFields().isEqualTo(expectedBook); - - assertThat(repositoryJdbc.findById(returnedBook.getId())) - .isPresent() - .get() - .isEqualTo(returnedBook); - } - - @DisplayName("должен сохранять измененную книгу") - @Test - void shouldSaveUpdatedBook() { - var expectedBook = new Book(1L, "BookTitle_10500", dbAuthors.get(2), - List.of(dbGenres.get(4), dbGenres.get(5))); - - assertThat(repositoryJdbc.findById(expectedBook.getId())) - .isPresent() - .get() - .isNotEqualTo(expectedBook); - - var returnedBook = repositoryJdbc.save(expectedBook); - assertThat(returnedBook).isNotNull() - .matches(book -> book.getId() > 0) - .usingRecursiveComparison().ignoringExpectedNullFields().isEqualTo(expectedBook); - - assertThat(repositoryJdbc.findById(returnedBook.getId())) - .isPresent() - .get() - .isEqualTo(returnedBook); - } - - @DisplayName("должен удалять книгу по id ") - @Test - void shouldDeleteBook() { - assertThat(repositoryJdbc.findById(1L)).isPresent(); - repositoryJdbc.deleteById(1L); - assertThat(repositoryJdbc.findById(1L)).isEmpty(); - } - - private static List getDbAuthors() { - return IntStream.range(1, 4).boxed() - .map(id -> new Author(id, "Author_" + id)) - .toList(); - } - - private static List getDbGenres() { - return IntStream.range(1, 7).boxed() - .map(id -> new Genre(id, "Genre_" + id)) - .toList(); - } - - private static List getDbBooks(List dbAuthors, List dbGenres) { - return IntStream.range(1, 4).boxed() - .map(id -> new Book(id, - "BookTitle_" + id, - dbAuthors.get(id - 1), - dbGenres.subList((id - 1) * 2, (id - 1) * 2 + 2) - )) - .toList(); - } - - private static List getDbBooks() { - var dbAuthors = getDbAuthors(); - var dbGenres = getDbGenres(); - return getDbBooks(dbAuthors, dbGenres); - } -} \ No newline at end of file diff --git a/templates/hw05-jdbc-hard-template/src/test/resources/data.sql b/templates/hw05-jdbc-hard-template/src/test/resources/data.sql deleted file mode 100644 index 3dd3a1664..000000000 --- a/templates/hw05-jdbc-hard-template/src/test/resources/data.sql +++ /dev/null @@ -1,14 +0,0 @@ -insert into authors(full_name) -values ('Author_1'), ('Author_2'), ('Author_3'); - -insert into genres(name) -values ('Genre_1'), ('Genre_2'), ('Genre_3'), - ('Genre_4'), ('Genre_5'), ('Genre_6'); - -insert into books(title, author_id) -values ('BookTitle_1', 1), ('BookTitle_2', 2), ('BookTitle_3', 3); - -insert into books_genres(book_id, genre_id) -values (1, 1), (1, 2), - (2, 3), (2, 4), - (3, 5), (3, 6); diff --git a/templates/hw05-jdbc-simple-template/.gitignore b/templates/hw05-jdbc-hard/.gitignore similarity index 94% rename from templates/hw05-jdbc-simple-template/.gitignore rename to templates/hw05-jdbc-hard/.gitignore index b96df348b..153c9335e 100644 --- a/templates/hw05-jdbc-simple-template/.gitignore +++ b/templates/hw05-jdbc-hard/.gitignore @@ -16,7 +16,6 @@ HELP.md *.iws *.iml *.ipr -spring-shell.log ### NetBeans ### /nbproject/private/ diff --git a/templates/hw05-jdbc-hard-template/pom.xml b/templates/hw05-jdbc-hard/pom.xml similarity index 99% rename from templates/hw05-jdbc-hard-template/pom.xml rename to templates/hw05-jdbc-hard/pom.xml index 86f97d4b4..9c467b1a2 100644 --- a/templates/hw05-jdbc-hard-template/pom.xml +++ b/templates/hw05-jdbc-hard/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 3.1.2 + 3.1.5 diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/Application.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/Application.java similarity index 100% rename from templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/Application.java rename to templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/Application.java diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/commands/AuthorCommands.java b/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/commands/AuthorCommands.java deleted file mode 100644 index c832e8ea0..000000000 --- a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/commands/AuthorCommands.java +++ /dev/null @@ -1,25 +0,0 @@ -package ru.otus.hw.commands; - -import lombok.RequiredArgsConstructor; -import org.springframework.shell.standard.ShellComponent; -import org.springframework.shell.standard.ShellMethod; -import ru.otus.hw.converters.AuthorConverter; -import ru.otus.hw.services.AuthorService; - -import java.util.stream.Collectors; - -@RequiredArgsConstructor -@ShellComponent -public class AuthorCommands { - - private final AuthorService authorService; - - private final AuthorConverter authorConverter; - - @ShellMethod(value = "Find all authors", key = "aa") - public String findAllAuthors() { - return authorService.findAll().stream() - .map(authorConverter::authorToString) - .collect(Collectors.joining("," + System.lineSeparator())); - } -} diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/commands/GenreCommands.java b/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/commands/GenreCommands.java deleted file mode 100644 index 2b5705b8c..000000000 --- a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/commands/GenreCommands.java +++ /dev/null @@ -1,25 +0,0 @@ -package ru.otus.hw.commands; - -import lombok.RequiredArgsConstructor; -import org.springframework.shell.standard.ShellComponent; -import org.springframework.shell.standard.ShellMethod; -import ru.otus.hw.converters.GenreConverter; -import ru.otus.hw.services.GenreService; - -import java.util.stream.Collectors; - -@RequiredArgsConstructor -@ShellComponent -public class GenreCommands { - - private final GenreService genreService; - - private final GenreConverter genreConverter; - - @ShellMethod(value = "Find all genres", key = "ag") - public String findAllGenres() { - return genreService.findAll().stream() - .map(genreConverter::genreToString) - .collect(Collectors.joining("," + System.lineSeparator())); - } -} diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/converters/AuthorConverter.java b/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/converters/AuthorConverter.java deleted file mode 100644 index 7d4d6c9cb..000000000 --- a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/converters/AuthorConverter.java +++ /dev/null @@ -1,11 +0,0 @@ -package ru.otus.hw.converters; - -import org.springframework.stereotype.Component; -import ru.otus.hw.models.Author; - -@Component -public class AuthorConverter { - public String authorToString(Author author) { - return "Id: %d, FullName: %s".formatted(author.getId(), author.getFullName()); - } -} diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/converters/GenreConverter.java b/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/converters/GenreConverter.java deleted file mode 100644 index 92d500ca9..000000000 --- a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/converters/GenreConverter.java +++ /dev/null @@ -1,11 +0,0 @@ -package ru.otus.hw.converters; - -import org.springframework.stereotype.Component; -import ru.otus.hw.models.Genre; - -@Component -public class GenreConverter { - public String genreToString(Genre genre) { - return "Id: %d, Name: %s".formatted(genre.getId(), genre.getName()); - } -} diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/exceptions/EntityNotFoundException.java b/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/exceptions/EntityNotFoundException.java deleted file mode 100644 index eb27d4915..000000000 --- a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/exceptions/EntityNotFoundException.java +++ /dev/null @@ -1,7 +0,0 @@ -package ru.otus.hw.exceptions; - -public class EntityNotFoundException extends RuntimeException { - public EntityNotFoundException(String message) { - super(message); - } -} diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/models/Author.java b/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/models/Author.java deleted file mode 100644 index 7db6dbbe4..000000000 --- a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/models/Author.java +++ /dev/null @@ -1,14 +0,0 @@ -package ru.otus.hw.models; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -@Data -@AllArgsConstructor -@NoArgsConstructor -public class Author { - private long id; - - private String fullName; -} diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/models/Genre.java b/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/models/Genre.java deleted file mode 100644 index e59ddab35..000000000 --- a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/models/Genre.java +++ /dev/null @@ -1,14 +0,0 @@ -package ru.otus.hw.models; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -@Data -@AllArgsConstructor -@NoArgsConstructor -public class Genre { - private long id; - - private String name; -} diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/AuthorRepository.java b/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/AuthorRepository.java deleted file mode 100644 index 743de402a..000000000 --- a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/AuthorRepository.java +++ /dev/null @@ -1,12 +0,0 @@ -package ru.otus.hw.repositories; - -import ru.otus.hw.models.Author; - -import java.util.List; -import java.util.Optional; - -public interface AuthorRepository { - List findAll(); - - Optional findById(long id); -} diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/BookRepository.java b/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/BookRepository.java deleted file mode 100644 index ab507c333..000000000 --- a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/BookRepository.java +++ /dev/null @@ -1,16 +0,0 @@ -package ru.otus.hw.repositories; - -import ru.otus.hw.models.Book; - -import java.util.List; -import java.util.Optional; - -public interface BookRepository { - Optional findById(long id); - - List findAll(); - - Book save(Book book); - - void deleteById(long id); -} diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/AuthorService.java b/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/AuthorService.java deleted file mode 100644 index 87e92f8c1..000000000 --- a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/AuthorService.java +++ /dev/null @@ -1,9 +0,0 @@ -package ru.otus.hw.services; - -import ru.otus.hw.models.Author; - -import java.util.List; - -public interface AuthorService { - List findAll(); -} diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/AuthorServiceImpl.java b/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/AuthorServiceImpl.java deleted file mode 100644 index 36740ca15..000000000 --- a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/AuthorServiceImpl.java +++ /dev/null @@ -1,19 +0,0 @@ -package ru.otus.hw.services; - -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; -import ru.otus.hw.models.Author; -import ru.otus.hw.repositories.AuthorRepository; - -import java.util.List; - -@RequiredArgsConstructor -@Service -public class AuthorServiceImpl implements AuthorService { - private final AuthorRepository authorRepository; - - @Override - public List findAll() { - return authorRepository.findAll(); - } -} diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/GenreService.java b/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/GenreService.java deleted file mode 100644 index 3fd4170b7..000000000 --- a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/GenreService.java +++ /dev/null @@ -1,9 +0,0 @@ -package ru.otus.hw.services; - -import ru.otus.hw.models.Genre; - -import java.util.List; - -public interface GenreService { - List findAll(); -} diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/GenreServiceImpl.java b/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/GenreServiceImpl.java deleted file mode 100644 index 8d1f620a7..000000000 --- a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/GenreServiceImpl.java +++ /dev/null @@ -1,19 +0,0 @@ -package ru.otus.hw.services; - -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; -import ru.otus.hw.models.Genre; -import ru.otus.hw.repositories.GenreRepository; - -import java.util.List; - -@RequiredArgsConstructor -@Service -public class GenreServiceImpl implements GenreService { - private final GenreRepository genreRepository; - - @Override - public List findAll() { - return genreRepository.findAll(); - } -} diff --git a/templates/hw05-jdbc-simple-template/src/main/resources/application.yml b/templates/hw05-jdbc-simple-template/src/main/resources/application.yml deleted file mode 100644 index c037758d3..000000000 --- a/templates/hw05-jdbc-simple-template/src/main/resources/application.yml +++ /dev/null @@ -1,8 +0,0 @@ -spring: - datasource: - url: jdbc:h2:mem:maindb - sql: - init: - mode: always - data-locations: data.sql - schema-locations: schema.sql \ No newline at end of file diff --git a/templates/hw05-jdbc-simple-template/src/test/resources/application.yml b/templates/hw05-jdbc-simple-template/src/test/resources/application.yml deleted file mode 100644 index 45c16b3d8..000000000 --- a/templates/hw05-jdbc-simple-template/src/test/resources/application.yml +++ /dev/null @@ -1,8 +0,0 @@ -spring: - datasource: - url: jdbc:h2:mem:testdb - sql: - init: - mode: always - data-locations: data.sql - schema-locations: schema.sql \ No newline at end of file diff --git a/templates/hw05-jdbc-hard-template/.gitignore b/templates/hw05-jdbc-simple/.gitignore similarity index 94% rename from templates/hw05-jdbc-hard-template/.gitignore rename to templates/hw05-jdbc-simple/.gitignore index b96df348b..153c9335e 100644 --- a/templates/hw05-jdbc-hard-template/.gitignore +++ b/templates/hw05-jdbc-simple/.gitignore @@ -16,7 +16,6 @@ HELP.md *.iws *.iml *.ipr -spring-shell.log ### NetBeans ### /nbproject/private/ diff --git a/templates/hw05-jdbc-simple-template/pom.xml b/templates/hw05-jdbc-simple/pom.xml similarity index 99% rename from templates/hw05-jdbc-simple-template/pom.xml rename to templates/hw05-jdbc-simple/pom.xml index 86f97d4b4..9c467b1a2 100644 --- a/templates/hw05-jdbc-simple-template/pom.xml +++ b/templates/hw05-jdbc-simple/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 3.1.2 + 3.1.5 diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/Application.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/Application.java similarity index 100% rename from templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/Application.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/Application.java diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/commands/AuthorCommands.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/commands/AuthorCommands.java similarity index 100% rename from templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/commands/AuthorCommands.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/commands/AuthorCommands.java diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/commands/BookCommands.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/commands/BookCommands.java similarity index 91% rename from templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/commands/BookCommands.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/commands/BookCommands.java index 2a6124f03..fb65dba13 100644 --- a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/commands/BookCommands.java +++ b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/commands/BookCommands.java @@ -8,6 +8,7 @@ import java.util.stream.Collectors; +@SuppressWarnings({"SpellCheckingInspection", "unused"}) @RequiredArgsConstructor @ShellComponent public class BookCommands { @@ -30,12 +31,14 @@ public String findBookById(long id) { .orElse("Book with id %d not found".formatted(id)); } + // bins newBook 1 1 @ShellMethod(value = "Insert book", key = "bins") public String insertBook(String title, long authorId, long genreId) { var savedBook = bookService.insert(title, authorId, genreId); return bookConverter.bookToString(savedBook); } + // bupd 4 editedBook 3 2 @ShellMethod(value = "Update book", key = "bupd") public String updateBook(long id, String title, long authorId, long genreId) { var savedBook = bookService.update(id, title, authorId, genreId); diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/commands/GenreCommands.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/commands/GenreCommands.java similarity index 100% rename from templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/commands/GenreCommands.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/commands/GenreCommands.java diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/converters/AuthorConverter.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/converters/AuthorConverter.java similarity index 100% rename from templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/converters/AuthorConverter.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/converters/AuthorConverter.java diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/converters/BookConverter.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/converters/BookConverter.java similarity index 100% rename from templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/converters/BookConverter.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/converters/BookConverter.java diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/converters/GenreConverter.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/converters/GenreConverter.java similarity index 100% rename from templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/converters/GenreConverter.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/converters/GenreConverter.java diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/exceptions/EntityNotFoundException.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/exceptions/EntityNotFoundException.java similarity index 100% rename from templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/exceptions/EntityNotFoundException.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/exceptions/EntityNotFoundException.java diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/models/Author.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/models/Author.java similarity index 100% rename from templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/models/Author.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/models/Author.java diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/models/Book.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/models/Book.java similarity index 100% rename from templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/models/Book.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/models/Book.java diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/models/Genre.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/models/Genre.java similarity index 100% rename from templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/models/Genre.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/models/Genre.java diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/AuthorRepository.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/repositories/AuthorRepository.java similarity index 100% rename from templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/AuthorRepository.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/repositories/AuthorRepository.java diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/BookRepository.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/repositories/BookRepository.java similarity index 100% rename from templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/repositories/BookRepository.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/repositories/BookRepository.java diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/GenreRepository.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/repositories/GenreRepository.java similarity index 100% rename from templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/GenreRepository.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/repositories/GenreRepository.java diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/AuthorRepositoryJdbc.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/repositories/JdbcAuthorRepository.java similarity index 91% rename from templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/AuthorRepositoryJdbc.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/repositories/JdbcAuthorRepository.java index b12c0c2c1..892acbcc2 100644 --- a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/AuthorRepositoryJdbc.java +++ b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/repositories/JdbcAuthorRepository.java @@ -11,7 +11,7 @@ import java.util.Optional; @Repository -public class AuthorRepositoryJdbc implements AuthorRepository { +public class JdbcAuthorRepository implements AuthorRepository { @Override public List findAll() { diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/BookRepositoryJdbc.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/repositories/JdbcBookRepository.java similarity index 87% rename from templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/BookRepositoryJdbc.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/repositories/JdbcBookRepository.java index fb3c70c3d..6dc8760de 100644 --- a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/BookRepositoryJdbc.java +++ b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/repositories/JdbcBookRepository.java @@ -12,7 +12,7 @@ import java.util.Optional; @Repository -public class BookRepositoryJdbc implements BookRepository { +public class JdbcBookRepository implements BookRepository { @Override public Optional findById(long id) { @@ -49,6 +49,7 @@ private Book insert(Book book) { private Book update(Book book) { //... + // Выбросить EntityNotFoundException если не обновлено ни одной записи в БД return book; } diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/GenreRepositoryJdbc.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/repositories/JdbcGenreRepository.java similarity index 91% rename from templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/GenreRepositoryJdbc.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/repositories/JdbcGenreRepository.java index 075e7f828..dc4e28373 100644 --- a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/repositories/GenreRepositoryJdbc.java +++ b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/repositories/JdbcGenreRepository.java @@ -11,7 +11,7 @@ import java.util.Optional; @Repository -public class GenreRepositoryJdbc implements GenreRepository { +public class JdbcGenreRepository implements GenreRepository { @Override public List findAll() { diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/services/AuthorService.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/services/AuthorService.java similarity index 100% rename from templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/services/AuthorService.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/services/AuthorService.java diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/services/AuthorServiceImpl.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/services/AuthorServiceImpl.java similarity index 100% rename from templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/services/AuthorServiceImpl.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/services/AuthorServiceImpl.java diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/BookService.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/services/BookService.java similarity index 100% rename from templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/BookService.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/services/BookService.java diff --git a/templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/BookServiceImpl.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/services/BookServiceImpl.java similarity index 100% rename from templates/hw05-jdbc-simple-template/src/main/java/ru/otus/hw/services/BookServiceImpl.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/services/BookServiceImpl.java diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/services/GenreService.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/services/GenreService.java similarity index 100% rename from templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/services/GenreService.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/services/GenreService.java diff --git a/templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/services/GenreServiceImpl.java b/templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/services/GenreServiceImpl.java similarity index 100% rename from templates/hw05-jdbc-hard-template/src/main/java/ru/otus/hw/services/GenreServiceImpl.java rename to templates/hw05-jdbc-simple/src/main/java/ru/otus/hw/services/GenreServiceImpl.java diff --git a/templates/hw05-jdbc-hard-template/src/main/resources/application.yml b/templates/hw05-jdbc-simple/src/main/resources/application.yml similarity index 100% rename from templates/hw05-jdbc-hard-template/src/main/resources/application.yml rename to templates/hw05-jdbc-simple/src/main/resources/application.yml diff --git a/templates/hw05-jdbc-simple-template/src/main/resources/data.sql b/templates/hw05-jdbc-simple/src/main/resources/data.sql similarity index 100% rename from templates/hw05-jdbc-simple-template/src/main/resources/data.sql rename to templates/hw05-jdbc-simple/src/main/resources/data.sql diff --git a/templates/hw05-jdbc-simple-template/src/main/resources/schema.sql b/templates/hw05-jdbc-simple/src/main/resources/schema.sql similarity index 100% rename from templates/hw05-jdbc-simple-template/src/main/resources/schema.sql rename to templates/hw05-jdbc-simple/src/main/resources/schema.sql diff --git a/templates/hw05-jdbc-simple-template/src/test/java/ru/otus/hw/repositories/BookRepositoryJdbcTest.java b/templates/hw05-jdbc-simple/src/test/java/ru/otus/hw/repositories/JdbcBookRepositoryTest.java similarity index 96% rename from templates/hw05-jdbc-simple-template/src/test/java/ru/otus/hw/repositories/BookRepositoryJdbcTest.java rename to templates/hw05-jdbc-simple/src/test/java/ru/otus/hw/repositories/JdbcBookRepositoryTest.java index 10e1b6401..4583cec57 100644 --- a/templates/hw05-jdbc-simple-template/src/test/java/ru/otus/hw/repositories/BookRepositoryJdbcTest.java +++ b/templates/hw05-jdbc-simple/src/test/java/ru/otus/hw/repositories/JdbcBookRepositoryTest.java @@ -19,11 +19,11 @@ @DisplayName("Репозиторий на основе Jdbc для работы с книгами ") @JdbcTest -@Import({BookRepositoryJdbc.class, GenreRepositoryJdbc.class}) -class BookRepositoryJdbcTest { +@Import({JdbcBookRepository.class, JdbcGenreRepository.class}) +class JdbcBookRepositoryTest { @Autowired - private BookRepositoryJdbc repositoryJdbc; + private JdbcBookRepository repositoryJdbc; private List dbAuthors; diff --git a/templates/hw05-jdbc-hard-template/src/test/resources/application.yml b/templates/hw05-jdbc-simple/src/test/resources/application.yml similarity index 100% rename from templates/hw05-jdbc-hard-template/src/test/resources/application.yml rename to templates/hw05-jdbc-simple/src/test/resources/application.yml diff --git a/templates/hw05-jdbc-simple-template/src/test/resources/data.sql b/templates/hw05-jdbc-simple/src/test/resources/data.sql similarity index 100% rename from templates/hw05-jdbc-simple-template/src/test/resources/data.sql rename to templates/hw05-jdbc-simple/src/test/resources/data.sql From ad3547f899547bc227453b761f4698256bda8596 Mon Sep 17 00:00:00 2001 From: stvort Date: Fri, 17 Nov 2023 01:41:59 +0400 Subject: [PATCH 2/3] template updated 2 --- .../ru/otus/hw/commands/AuthorCommands.java | 25 ++++ .../ru/otus/hw/commands/BookCommands.java | 53 +++++++ .../ru/otus/hw/commands/GenreCommands.java | 25 ++++ .../otus/hw/converters/AuthorConverter.java | 11 ++ .../ru/otus/hw/converters/BookConverter.java | 27 ++++ .../ru/otus/hw/converters/GenreConverter.java | 11 ++ .../exceptions/EntityNotFoundException.java | 7 + .../main/java/ru/otus/hw/models/Author.java | 14 ++ .../src/main/java/ru/otus/hw/models/Book.java | 20 +++ .../main/java/ru/otus/hw/models/Genre.java | 14 ++ .../hw/repositories/AuthorRepository.java | 12 ++ .../otus/hw/repositories/BookRepository.java | 16 +++ .../otus/hw/repositories/GenreRepository.java | 12 ++ .../hw/repositories/JdbcAuthorRepository.java | 33 +++++ .../hw/repositories/JdbcBookRepository.java | 114 +++++++++++++++ .../hw/repositories/JdbcGenreRepository.java | 33 +++++ .../ru/otus/hw/services/AuthorService.java | 9 ++ .../otus/hw/services/AuthorServiceImpl.java | 19 +++ .../java/ru/otus/hw/services/BookService.java | 19 +++ .../ru/otus/hw/services/BookServiceImpl.java | 66 +++++++++ .../ru/otus/hw/services/GenreService.java | 9 ++ .../ru/otus/hw/services/GenreServiceImpl.java | 19 +++ .../src/main/resources/application.yml | 8 ++ .../src/main/resources/data.sql | 14 ++ .../src/main/resources/schema.sql | 24 ++++ .../repositories/JdbcBookRepositoryTest.java | 134 ++++++++++++++++++ .../src/test/resources/application.yml | 8 ++ .../src/test/resources/data.sql | 14 ++ 28 files changed, 770 insertions(+) create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/commands/AuthorCommands.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/commands/BookCommands.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/commands/GenreCommands.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/converters/AuthorConverter.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/converters/BookConverter.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/converters/GenreConverter.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/exceptions/EntityNotFoundException.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/models/Author.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/models/Book.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/models/Genre.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/AuthorRepository.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/BookRepository.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/GenreRepository.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/JdbcAuthorRepository.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/JdbcBookRepository.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/JdbcGenreRepository.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/AuthorService.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/AuthorServiceImpl.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/BookService.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/BookServiceImpl.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/GenreService.java create mode 100644 templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/GenreServiceImpl.java create mode 100644 templates/hw05-jdbc-hard/src/main/resources/application.yml create mode 100644 templates/hw05-jdbc-hard/src/main/resources/data.sql create mode 100644 templates/hw05-jdbc-hard/src/main/resources/schema.sql create mode 100644 templates/hw05-jdbc-hard/src/test/java/ru/otus/hw/repositories/JdbcBookRepositoryTest.java create mode 100644 templates/hw05-jdbc-hard/src/test/resources/application.yml create mode 100644 templates/hw05-jdbc-hard/src/test/resources/data.sql diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/commands/AuthorCommands.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/commands/AuthorCommands.java new file mode 100644 index 000000000..c832e8ea0 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/commands/AuthorCommands.java @@ -0,0 +1,25 @@ +package ru.otus.hw.commands; + +import lombok.RequiredArgsConstructor; +import org.springframework.shell.standard.ShellComponent; +import org.springframework.shell.standard.ShellMethod; +import ru.otus.hw.converters.AuthorConverter; +import ru.otus.hw.services.AuthorService; + +import java.util.stream.Collectors; + +@RequiredArgsConstructor +@ShellComponent +public class AuthorCommands { + + private final AuthorService authorService; + + private final AuthorConverter authorConverter; + + @ShellMethod(value = "Find all authors", key = "aa") + public String findAllAuthors() { + return authorService.findAll().stream() + .map(authorConverter::authorToString) + .collect(Collectors.joining("," + System.lineSeparator())); + } +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/commands/BookCommands.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/commands/BookCommands.java new file mode 100644 index 000000000..9189a8c60 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/commands/BookCommands.java @@ -0,0 +1,53 @@ +package ru.otus.hw.commands; + +import lombok.RequiredArgsConstructor; +import org.springframework.shell.standard.ShellComponent; +import org.springframework.shell.standard.ShellMethod; +import ru.otus.hw.converters.BookConverter; +import ru.otus.hw.services.BookService; + +import java.util.Set; +import java.util.stream.Collectors; + +@SuppressWarnings({"SpellCheckingInspection", "unused"}) +@RequiredArgsConstructor +@ShellComponent +public class BookCommands { + + private final BookService bookService; + + private final BookConverter bookConverter; + + @ShellMethod(value = "Find all books", key = "ab") + public String findAllBooks() { + return bookService.findAll().stream() + .map(bookConverter::bookToString) + .collect(Collectors.joining("," + System.lineSeparator())); + } + + @ShellMethod(value = "Find book by id", key = "bbid") + public String findBookById(long id) { + return bookService.findById(id) + .map(bookConverter::bookToString) + .orElse("Book with id %d not found".formatted(id)); + } + + // bins newBook 1 1,6 + @ShellMethod(value = "Insert book", key = "bins") + public String insertBook(String title, long authorId, Set genresIds) { + var savedBook = bookService.insert(title, authorId, genresIds); + return bookConverter.bookToString(savedBook); + } + + // bupd 4 editedBook 3 2,5 + @ShellMethod(value = "Update book", key = "bupd") + public String updateBook(long id, String title, long authorId, Set genresIds) { + var savedBook = bookService.update(id, title, authorId, genresIds); + return bookConverter.bookToString(savedBook); + } + + @ShellMethod(value = "Delete book by id", key = "bdel") + public void updateBook(long id) { + bookService.deleteById(id); + } +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/commands/GenreCommands.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/commands/GenreCommands.java new file mode 100644 index 000000000..2b5705b8c --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/commands/GenreCommands.java @@ -0,0 +1,25 @@ +package ru.otus.hw.commands; + +import lombok.RequiredArgsConstructor; +import org.springframework.shell.standard.ShellComponent; +import org.springframework.shell.standard.ShellMethod; +import ru.otus.hw.converters.GenreConverter; +import ru.otus.hw.services.GenreService; + +import java.util.stream.Collectors; + +@RequiredArgsConstructor +@ShellComponent +public class GenreCommands { + + private final GenreService genreService; + + private final GenreConverter genreConverter; + + @ShellMethod(value = "Find all genres", key = "ag") + public String findAllGenres() { + return genreService.findAll().stream() + .map(genreConverter::genreToString) + .collect(Collectors.joining("," + System.lineSeparator())); + } +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/converters/AuthorConverter.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/converters/AuthorConverter.java new file mode 100644 index 000000000..7d4d6c9cb --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/converters/AuthorConverter.java @@ -0,0 +1,11 @@ +package ru.otus.hw.converters; + +import org.springframework.stereotype.Component; +import ru.otus.hw.models.Author; + +@Component +public class AuthorConverter { + public String authorToString(Author author) { + return "Id: %d, FullName: %s".formatted(author.getId(), author.getFullName()); + } +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/converters/BookConverter.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/converters/BookConverter.java new file mode 100644 index 000000000..c174791d3 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/converters/BookConverter.java @@ -0,0 +1,27 @@ +package ru.otus.hw.converters; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; +import ru.otus.hw.models.Book; + +import java.util.stream.Collectors; + +@RequiredArgsConstructor +@Component +public class BookConverter { + private final AuthorConverter authorConverter; + + private final GenreConverter genreConverter; + + public String bookToString(Book book) { + var genresString = book.getGenres().stream() + .map(genreConverter::genreToString) + .map("{%s}"::formatted) + .collect(Collectors.joining(", ")); + return "Id: %d, title: %s, author: {%s}, genres: [%s]".formatted( + book.getId(), + book.getTitle(), + authorConverter.authorToString(book.getAuthor()), + genresString); + } +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/converters/GenreConverter.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/converters/GenreConverter.java new file mode 100644 index 000000000..92d500ca9 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/converters/GenreConverter.java @@ -0,0 +1,11 @@ +package ru.otus.hw.converters; + +import org.springframework.stereotype.Component; +import ru.otus.hw.models.Genre; + +@Component +public class GenreConverter { + public String genreToString(Genre genre) { + return "Id: %d, Name: %s".formatted(genre.getId(), genre.getName()); + } +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/exceptions/EntityNotFoundException.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/exceptions/EntityNotFoundException.java new file mode 100644 index 000000000..eb27d4915 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/exceptions/EntityNotFoundException.java @@ -0,0 +1,7 @@ +package ru.otus.hw.exceptions; + +public class EntityNotFoundException extends RuntimeException { + public EntityNotFoundException(String message) { + super(message); + } +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/models/Author.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/models/Author.java new file mode 100644 index 000000000..7db6dbbe4 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/models/Author.java @@ -0,0 +1,14 @@ +package ru.otus.hw.models; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Author { + private long id; + + private String fullName; +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/models/Book.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/models/Book.java new file mode 100644 index 000000000..c71d15e16 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/models/Book.java @@ -0,0 +1,20 @@ +package ru.otus.hw.models; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Book { + private long id; + + private String title; + + private Author author; + + private List genres; +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/models/Genre.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/models/Genre.java new file mode 100644 index 000000000..e59ddab35 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/models/Genre.java @@ -0,0 +1,14 @@ +package ru.otus.hw.models; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Genre { + private long id; + + private String name; +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/AuthorRepository.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/AuthorRepository.java new file mode 100644 index 000000000..743de402a --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/AuthorRepository.java @@ -0,0 +1,12 @@ +package ru.otus.hw.repositories; + +import ru.otus.hw.models.Author; + +import java.util.List; +import java.util.Optional; + +public interface AuthorRepository { + List findAll(); + + Optional findById(long id); +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/BookRepository.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/BookRepository.java new file mode 100644 index 000000000..ab507c333 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/BookRepository.java @@ -0,0 +1,16 @@ +package ru.otus.hw.repositories; + +import ru.otus.hw.models.Book; + +import java.util.List; +import java.util.Optional; + +public interface BookRepository { + Optional findById(long id); + + List findAll(); + + Book save(Book book); + + void deleteById(long id); +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/GenreRepository.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/GenreRepository.java new file mode 100644 index 000000000..06d63458d --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/GenreRepository.java @@ -0,0 +1,12 @@ +package ru.otus.hw.repositories; + +import ru.otus.hw.models.Genre; + +import java.util.List; +import java.util.Set; + +public interface GenreRepository { + List findAll(); + + List findAllByIds(Set ids); +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/JdbcAuthorRepository.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/JdbcAuthorRepository.java new file mode 100644 index 000000000..892acbcc2 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/JdbcAuthorRepository.java @@ -0,0 +1,33 @@ +package ru.otus.hw.repositories; + +import org.springframework.jdbc.core.RowMapper; +import org.springframework.stereotype.Repository; +import ru.otus.hw.models.Author; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@Repository +public class JdbcAuthorRepository implements AuthorRepository { + + @Override + public List findAll() { + return new ArrayList<>(); + } + + @Override + public Optional findById(long id) { + return Optional.empty(); + } + + private static class AuthorRowMapper implements RowMapper { + + @Override + public Author mapRow(ResultSet rs, int i) throws SQLException { + return null; + } + } +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/JdbcBookRepository.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/JdbcBookRepository.java new file mode 100644 index 000000000..43e13c6d4 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/JdbcBookRepository.java @@ -0,0 +1,114 @@ +package ru.otus.hw.repositories; + +import lombok.RequiredArgsConstructor; +import org.springframework.dao.DataAccessException; +import org.springframework.jdbc.core.ResultSetExtractor; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.stereotype.Repository; +import ru.otus.hw.models.Book; +import ru.otus.hw.models.Genre; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@Repository +@RequiredArgsConstructor +public class JdbcBookRepository implements BookRepository { + + private final GenreRepository genreRepository; + + @Override + public Optional findById(long id) { + return Optional.empty(); + } + + @Override + public List findAll() { + var genres = genreRepository.findAll(); + var relations = getAllGenreRelations(); + var books = getAllBooksWithoutGenres(); + mergeBooksInfo(books, genres, relations); + return books; + } + + @Override + public Book save(Book book) { + if (book.getId() == 0) { + return insert(book); + } + return update(book); + } + + @Override + public void deleteById(long id) { + //... + } + + private List getAllBooksWithoutGenres() { + return new ArrayList<>(); + } + + private List getAllGenreRelations() { + return new ArrayList<>(); + } + + private void mergeBooksInfo(List booksWithoutGenres, List genres, + List relations) { + // Добавить книгам (booksWithoutGenres) жанры (genres) в соответствии со связями (relations) + } + + private Book insert(Book book) { + var keyHolder = new GeneratedKeyHolder(); + + //... + + //noinspection DataFlowIssue + book.setId(keyHolder.getKeyAs(Long.class)); + batchInsertGenresRelationsFor(book); + return book; + } + + private Book update(Book book) { + //... + + // Выбросить EntityNotFoundException если не обновлено ни одной записи в БД + removeGenresRelationsFor(book); + batchInsertGenresRelationsFor(book); + + return book; + } + + private void batchInsertGenresRelationsFor(Book book) { + // Использовать метод batchUpdate + } + + private void removeGenresRelationsFor(Book book) { + //... + } + + private static class BookRowMapper implements RowMapper { + + @Override + public Book mapRow(ResultSet rs, int rowNum) throws SQLException { + return null; + } + } + + // Использовать для findById + @SuppressWarnings("ClassCanBeRecord") + @RequiredArgsConstructor + private static class BookResultSetExtractor implements ResultSetExtractor { + + @Override + public Book extractData(ResultSet rs) throws SQLException, DataAccessException { + return null; + } + } + + private record BookGenreRelation(long bookId, long genreId) { + } +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/JdbcGenreRepository.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/JdbcGenreRepository.java new file mode 100644 index 000000000..2886574a5 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/repositories/JdbcGenreRepository.java @@ -0,0 +1,33 @@ +package ru.otus.hw.repositories; + +import org.springframework.jdbc.core.RowMapper; +import org.springframework.stereotype.Repository; +import ru.otus.hw.models.Genre; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +@Repository +public class JdbcGenreRepository implements GenreRepository { + + @Override + public List findAll() { + return new ArrayList<>(); + } + + @Override + public List findAllByIds(Set ids) { + return new ArrayList<>(); + } + + private static class GnreRowMapper implements RowMapper { + + @Override + public Genre mapRow(ResultSet rs, int i) throws SQLException { + return null; + } + } +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/AuthorService.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/AuthorService.java new file mode 100644 index 000000000..87e92f8c1 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/AuthorService.java @@ -0,0 +1,9 @@ +package ru.otus.hw.services; + +import ru.otus.hw.models.Author; + +import java.util.List; + +public interface AuthorService { + List findAll(); +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/AuthorServiceImpl.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/AuthorServiceImpl.java new file mode 100644 index 000000000..36740ca15 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/AuthorServiceImpl.java @@ -0,0 +1,19 @@ +package ru.otus.hw.services; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import ru.otus.hw.models.Author; +import ru.otus.hw.repositories.AuthorRepository; + +import java.util.List; + +@RequiredArgsConstructor +@Service +public class AuthorServiceImpl implements AuthorService { + private final AuthorRepository authorRepository; + + @Override + public List findAll() { + return authorRepository.findAll(); + } +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/BookService.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/BookService.java new file mode 100644 index 000000000..b7c1b3861 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/BookService.java @@ -0,0 +1,19 @@ +package ru.otus.hw.services; + +import ru.otus.hw.models.Book; + +import java.util.List; +import java.util.Optional; +import java.util.Set; + +public interface BookService { + Optional findById(long id); + + List findAll(); + + Book insert(String title, long authorId, Set genresIds); + + Book update(long id, String title, long authorId, Set genresIds); + + void deleteById(long id); +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/BookServiceImpl.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/BookServiceImpl.java new file mode 100644 index 000000000..d008d13df --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/BookServiceImpl.java @@ -0,0 +1,66 @@ +package ru.otus.hw.services; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import ru.otus.hw.exceptions.EntityNotFoundException; +import ru.otus.hw.models.Book; +import ru.otus.hw.repositories.AuthorRepository; +import ru.otus.hw.repositories.BookRepository; +import ru.otus.hw.repositories.GenreRepository; + +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import static org.springframework.util.CollectionUtils.isEmpty; + +@RequiredArgsConstructor +@Service +public class BookServiceImpl implements BookService { + private final AuthorRepository authorRepository; + + private final GenreRepository genreRepository; + + private final BookRepository bookRepository; + + @Override + public Optional findById(long id) { + return bookRepository.findById(id); + } + + @Override + public List findAll() { + return bookRepository.findAll(); + } + + @Override + public Book insert(String title, long authorId, Set genresIds) { + return save(0, title, authorId, genresIds); + } + + @Override + public Book update(long id, String title, long authorId, Set genresIds) { + return save(id, title, authorId, genresIds); + } + + @Override + public void deleteById(long id) { + bookRepository.deleteById(id); + } + + private Book save(long id, String title, long authorId, Set genresIds) { + if (isEmpty(genresIds)) { + throw new IllegalArgumentException("Genres ids must not be null"); + } + + var author = authorRepository.findById(authorId) + .orElseThrow(() -> new EntityNotFoundException("Author with id %d not found".formatted(authorId))); + var genres = genreRepository.findAllByIds(genresIds); + if (isEmpty(genres) || genresIds.size() != genres.size()) { + throw new EntityNotFoundException("One or all genres with ids %s not found".formatted(genresIds)); + } + + var book = new Book(id, title, author, genres); + return bookRepository.save(book); + } +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/GenreService.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/GenreService.java new file mode 100644 index 000000000..3fd4170b7 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/GenreService.java @@ -0,0 +1,9 @@ +package ru.otus.hw.services; + +import ru.otus.hw.models.Genre; + +import java.util.List; + +public interface GenreService { + List findAll(); +} diff --git a/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/GenreServiceImpl.java b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/GenreServiceImpl.java new file mode 100644 index 000000000..8d1f620a7 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/java/ru/otus/hw/services/GenreServiceImpl.java @@ -0,0 +1,19 @@ +package ru.otus.hw.services; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import ru.otus.hw.models.Genre; +import ru.otus.hw.repositories.GenreRepository; + +import java.util.List; + +@RequiredArgsConstructor +@Service +public class GenreServiceImpl implements GenreService { + private final GenreRepository genreRepository; + + @Override + public List findAll() { + return genreRepository.findAll(); + } +} diff --git a/templates/hw05-jdbc-hard/src/main/resources/application.yml b/templates/hw05-jdbc-hard/src/main/resources/application.yml new file mode 100644 index 000000000..c037758d3 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/resources/application.yml @@ -0,0 +1,8 @@ +spring: + datasource: + url: jdbc:h2:mem:maindb + sql: + init: + mode: always + data-locations: data.sql + schema-locations: schema.sql \ No newline at end of file diff --git a/templates/hw05-jdbc-hard/src/main/resources/data.sql b/templates/hw05-jdbc-hard/src/main/resources/data.sql new file mode 100644 index 000000000..3dd3a1664 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/resources/data.sql @@ -0,0 +1,14 @@ +insert into authors(full_name) +values ('Author_1'), ('Author_2'), ('Author_3'); + +insert into genres(name) +values ('Genre_1'), ('Genre_2'), ('Genre_3'), + ('Genre_4'), ('Genre_5'), ('Genre_6'); + +insert into books(title, author_id) +values ('BookTitle_1', 1), ('BookTitle_2', 2), ('BookTitle_3', 3); + +insert into books_genres(book_id, genre_id) +values (1, 1), (1, 2), + (2, 3), (2, 4), + (3, 5), (3, 6); diff --git a/templates/hw05-jdbc-hard/src/main/resources/schema.sql b/templates/hw05-jdbc-hard/src/main/resources/schema.sql new file mode 100644 index 000000000..b085e18a8 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/main/resources/schema.sql @@ -0,0 +1,24 @@ +create table authors ( + id bigserial, + full_name varchar(255), + primary key (id) +); + +create table genres ( + id bigserial, + name varchar(255), + primary key (id) +); + +create table books ( + id bigserial, + title varchar(255), + author_id bigint references authors (id) on delete cascade, + primary key (id) +); + +create table books_genres ( + book_id bigint references books(id) on delete cascade, + genre_id bigint references genres(id) on delete cascade, + primary key (book_id, genre_id) +); \ No newline at end of file diff --git a/templates/hw05-jdbc-hard/src/test/java/ru/otus/hw/repositories/JdbcBookRepositoryTest.java b/templates/hw05-jdbc-hard/src/test/java/ru/otus/hw/repositories/JdbcBookRepositoryTest.java new file mode 100644 index 000000000..8c6bcfdec --- /dev/null +++ b/templates/hw05-jdbc-hard/src/test/java/ru/otus/hw/repositories/JdbcBookRepositoryTest.java @@ -0,0 +1,134 @@ +package ru.otus.hw.repositories; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; +import org.springframework.context.annotation.Import; +import ru.otus.hw.models.Author; +import ru.otus.hw.models.Book; +import ru.otus.hw.models.Genre; + +import java.util.List; +import java.util.stream.IntStream; + +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayName("Репозиторий на основе Jdbc для работы с книгами ") +@JdbcTest +@Import({JdbcBookRepository.class, JdbcGenreRepository.class}) +class JdbcBookRepositoryTest { + + @Autowired + private JdbcBookRepository repositoryJdbc; + + private List dbAuthors; + + private List dbGenres; + + private List dbBooks; + + @BeforeEach + void setUp() { + dbAuthors = getDbAuthors(); + dbGenres = getDbGenres(); + dbBooks = getDbBooks(dbAuthors, dbGenres); + } + + @DisplayName("должен загружать книгу по id") + @ParameterizedTest + @MethodSource("getDbBooks") + void shouldReturnCorrectBookById(Book expectedBook) { + var actualBook = repositoryJdbc.findById(expectedBook.getId()); + assertThat(actualBook).isPresent() + .get() + .isEqualTo(expectedBook); + } + + @DisplayName("должен загружать список всех книг") + @Test + void shouldReturnCorrectBooksList() { + var actualBooks = repositoryJdbc.findAll(); + var expectedBooks = dbBooks; + + assertThat(actualBooks).containsExactlyElementsOf(expectedBooks); + actualBooks.forEach(System.out::println); + } + + @DisplayName("должен сохранять новую книгу") + @Test + void shouldSaveNewBook() { + var expectedBook = new Book(0, "BookTitle_10500", dbAuthors.get(0), + List.of(dbGenres.get(0), dbGenres.get(2))); + var returnedBook = repositoryJdbc.save(expectedBook); + assertThat(returnedBook).isNotNull() + .matches(book -> book.getId() > 0) + .usingRecursiveComparison().ignoringExpectedNullFields().isEqualTo(expectedBook); + + assertThat(repositoryJdbc.findById(returnedBook.getId())) + .isPresent() + .get() + .isEqualTo(returnedBook); + } + + @DisplayName("должен сохранять измененную книгу") + @Test + void shouldSaveUpdatedBook() { + var expectedBook = new Book(1L, "BookTitle_10500", dbAuthors.get(2), + List.of(dbGenres.get(4), dbGenres.get(5))); + + assertThat(repositoryJdbc.findById(expectedBook.getId())) + .isPresent() + .get() + .isNotEqualTo(expectedBook); + + var returnedBook = repositoryJdbc.save(expectedBook); + assertThat(returnedBook).isNotNull() + .matches(book -> book.getId() > 0) + .usingRecursiveComparison().ignoringExpectedNullFields().isEqualTo(expectedBook); + + assertThat(repositoryJdbc.findById(returnedBook.getId())) + .isPresent() + .get() + .isEqualTo(returnedBook); + } + + @DisplayName("должен удалять книгу по id ") + @Test + void shouldDeleteBook() { + assertThat(repositoryJdbc.findById(1L)).isPresent(); + repositoryJdbc.deleteById(1L); + assertThat(repositoryJdbc.findById(1L)).isEmpty(); + } + + private static List getDbAuthors() { + return IntStream.range(1, 4).boxed() + .map(id -> new Author(id, "Author_" + id)) + .toList(); + } + + private static List getDbGenres() { + return IntStream.range(1, 7).boxed() + .map(id -> new Genre(id, "Genre_" + id)) + .toList(); + } + + private static List getDbBooks(List dbAuthors, List dbGenres) { + return IntStream.range(1, 4).boxed() + .map(id -> new Book(id, + "BookTitle_" + id, + dbAuthors.get(id - 1), + dbGenres.subList((id - 1) * 2, (id - 1) * 2 + 2) + )) + .toList(); + } + + private static List getDbBooks() { + var dbAuthors = getDbAuthors(); + var dbGenres = getDbGenres(); + return getDbBooks(dbAuthors, dbGenres); + } +} \ No newline at end of file diff --git a/templates/hw05-jdbc-hard/src/test/resources/application.yml b/templates/hw05-jdbc-hard/src/test/resources/application.yml new file mode 100644 index 000000000..45c16b3d8 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/test/resources/application.yml @@ -0,0 +1,8 @@ +spring: + datasource: + url: jdbc:h2:mem:testdb + sql: + init: + mode: always + data-locations: data.sql + schema-locations: schema.sql \ No newline at end of file diff --git a/templates/hw05-jdbc-hard/src/test/resources/data.sql b/templates/hw05-jdbc-hard/src/test/resources/data.sql new file mode 100644 index 000000000..3dd3a1664 --- /dev/null +++ b/templates/hw05-jdbc-hard/src/test/resources/data.sql @@ -0,0 +1,14 @@ +insert into authors(full_name) +values ('Author_1'), ('Author_2'), ('Author_3'); + +insert into genres(name) +values ('Genre_1'), ('Genre_2'), ('Genre_3'), + ('Genre_4'), ('Genre_5'), ('Genre_6'); + +insert into books(title, author_id) +values ('BookTitle_1', 1), ('BookTitle_2', 2), ('BookTitle_3', 3); + +insert into books_genres(book_id, genre_id) +values (1, 1), (1, 2), + (2, 3), (2, 4), + (3, 5), (3, 6); From 6107d2afcefc8bd94dbecc6c3ca648df656eac43 Mon Sep 17 00:00:00 2001 From: stvort Date: Fri, 17 Nov 2023 01:45:37 +0400 Subject: [PATCH 3/3] templates update 3 --- templates/hw05-jdbc-hard/.gitignore | 2 ++ templates/hw05-jdbc-simple/.gitignore | 2 ++ 2 files changed, 4 insertions(+) diff --git a/templates/hw05-jdbc-hard/.gitignore b/templates/hw05-jdbc-hard/.gitignore index 153c9335e..5ae18aca6 100644 --- a/templates/hw05-jdbc-hard/.gitignore +++ b/templates/hw05-jdbc-hard/.gitignore @@ -27,3 +27,5 @@ HELP.md ### VS Code ### .vscode/ + +spring-shell.log \ No newline at end of file diff --git a/templates/hw05-jdbc-simple/.gitignore b/templates/hw05-jdbc-simple/.gitignore index 153c9335e..5ae18aca6 100644 --- a/templates/hw05-jdbc-simple/.gitignore +++ b/templates/hw05-jdbc-simple/.gitignore @@ -27,3 +27,5 @@ HELP.md ### VS Code ### .vscode/ + +spring-shell.log \ No newline at end of file