Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

templates updated #216

Merged
merged 3 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions templates/hw01-xml-config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>6.0.12</spring.version>
<spring.version>6.0.13</spring.version>
<opencsv.version>5.8</opencsv.version>
<junit.version>5.10.0</junit.version>
<mockito.version>5.4.0</mockito.version>
<assertj.version>3.24.2</assertj.version>
<lombok.version>1.18.28</lombok.version>
<lombok.version>1.18.30</lombok.version>
<checkstyle-plugin.version>3.2.2</checkstyle-plugin.version>
<checkstyle.version>10.11.0</checkstyle.version>
<checkstyle.config.url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@AllArgsConstructor
@Data
public class AppConfig implements TestFileNameProvider {
public class AppProperties implements TestFileNameProvider {

private String testFileName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public List<Question> 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<>();
}
Expand Down
4 changes: 2 additions & 2 deletions templates/hw02-annotation-config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>6.0.12</spring.version>
<spring.version>6.0.13</spring.version>
<opencsv.version>5.8</opencsv.version>
<junit.version>5.10.0</junit.version>
<mockito.version>5.4.0</mockito.version>
<assertj.version>3.24.2</assertj.version>
<lombok.version>1.18.28</lombok.version>
<lombok.version>1.18.30</lombok.version>
<checkstyle-plugin.version>3.2.2</checkstyle-plugin.version>
<checkstyle.version>10.11.0</checkstyle.version>
<checkstyle.config.url>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public List<Question> 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<>();
}
Expand Down
2 changes: 1 addition & 1 deletion templates/hw03-spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<maven.compiler.source>17</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<opencsv.version>5.8</opencsv.version>
<lombok.version>1.18.28</lombok.version>
<lombok.version>1.18.30</lombok.version>
<checkstyle-plugin.version>3.2.2</checkstyle-plugin.version>
<checkstyle.version>10.11.0</checkstyle.version>
<checkstyle.config.url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public List<Question> 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<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ HELP.md
*.iws
*.iml
*.ipr
spring-shell.log

### NetBeans ###
/nbproject/private/
Expand All @@ -28,3 +27,5 @@ spring-shell.log

### VS Code ###
.vscode/

spring-shell.log
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.2</version>
<version>3.1.5</version>
<relativePath/>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import ru.otus.hw.converters.BookConverter;
import ru.otus.hw.services.BookService;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@SuppressWarnings({"SpellCheckingInspection", "unused"})
@RequiredArgsConstructor
@ShellComponent
public class BookCommands {
Expand All @@ -31,16 +32,16 @@ public String findBookById(long id) {
.orElse("Book with id %d not found".formatted(id));
}

//bins aaaaaaaaaaaaa 1 1,6//bins aaaaaaaaaaaaa 1 1,6
// bins newBook 1 1,6
@ShellMethod(value = "Insert book", key = "bins")
public String insertBook(String title, long authorId, List<Long> genresIds) {
public String insertBook(String title, long authorId, Set<Long> genresIds) {
var savedBook = bookService.insert(title, authorId, genresIds);
return bookConverter.bookToString(savedBook);
}

//bupd 4 dfasdfasdfasd 3 2,5
// bupd 4 editedBook 3 2,5
@ShellMethod(value = "Update book", key = "bupd")
public String updateBook(long id, String title, long authorId, List<Long> genresIds) {
public String updateBook(long id, String title, long authorId, Set<Long> genresIds) {
var savedBook = bookService.update(id, title, authorId, genresIds);
return bookConverter.bookToString(savedBook);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import ru.otus.hw.models.Genre;

import java.util.List;
import java.util.Set;

public interface GenreRepository {
List<Genre> findAll();

List<Genre> findAllByIds(List<Long> ids);
List<Genre> findAllByIds(Set<Long> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.Optional;

@Repository
public class AuthorRepositoryJdbc implements AuthorRepository {
public class JdbcAuthorRepository implements AuthorRepository {

@Override
public List<Author> findAll() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

@Repository
@RequiredArgsConstructor
public class BookRepositoryJdbc implements BookRepository {
public class JdbcBookRepository implements BookRepository {

private final GenreRepository genreRepository;

Expand Down Expand Up @@ -75,14 +75,15 @@ private Book insert(Book book) {
private Book update(Book book) {
//...

// Выбросить EntityNotFoundException если не обновлено ни одной записи в БД
removeGenresRelationsFor(book);
batchInsertGenresRelationsFor(book);

return book;
}

private void batchInsertGenresRelationsFor(Book book) {
// batchUpdate
// Использовать метод batchUpdate
}

private void removeGenresRelationsFor(Book book) {
Expand All @@ -97,13 +98,14 @@ public Book mapRow(ResultSet rs, int rowNum) throws SQLException {
}
}

// Использовать для findById
@SuppressWarnings("ClassCanBeRecord")
@RequiredArgsConstructor
private static class BookResultSetExtractor implements ResultSetExtractor<List<Book>> {
private static class BookResultSetExtractor implements ResultSetExtractor<Book> {

@Override
public List<Book> extractData(ResultSet rs) throws SQLException, DataAccessException {
return new ArrayList<>();
public Book extractData(ResultSet rs) throws SQLException, DataAccessException {
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Repository
public class GenreRepositoryJdbc implements GenreRepository {
public class JdbcGenreRepository implements GenreRepository {

@Override
public List<Genre> findAll() {
return new ArrayList<>();
}

@Override
public List<Genre> findAllByIds(List<Long> ids) {
public List<Genre> findAllByIds(Set<Long> ids) {
return new ArrayList<>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

import java.util.List;
import java.util.Optional;
import java.util.Set;

public interface BookService {
Optional<Book> findById(long id);

List<Book> findAll();

Book insert(String title, long authorId, List<Long> genresIds);
Book insert(String title, long authorId, Set<Long> genresIds);

Book update(long id, String title, long authorId, List<Long> genresIds);
Book update(long id, String title, long authorId, Set<Long> genresIds);

void deleteById(long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.List;
import java.util.Optional;
import java.util.Set;

import static org.springframework.util.CollectionUtils.isEmpty;

Expand All @@ -33,12 +34,12 @@ public List<Book> findAll() {
}

@Override
public Book insert(String title, long authorId, List<Long> genresIds) {
public Book insert(String title, long authorId, Set<Long> genresIds) {
return save(0, title, authorId, genresIds);
}

@Override
public Book update(long id, String title, long authorId, List<Long> genresIds) {
public Book update(long id, String title, long authorId, Set<Long> genresIds) {
return save(id, title, authorId, genresIds);
}

Expand All @@ -47,13 +48,18 @@ public void deleteById(long id) {
bookRepository.deleteById(id);
}

private Book save(long id, String title, long authorId, List<Long> genresIds) {
private Book save(long id, String title, long authorId, Set<Long> 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)) {
throw new EntityNotFoundException("Genres with ids %s not found".formatted(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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Author> dbAuthors;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ HELP.md
*.iws
*.iml
*.ipr
spring-shell.log

### NetBeans ###
/nbproject/private/
Expand All @@ -28,3 +27,5 @@ spring-shell.log

### VS Code ###
.vscode/

spring-shell.log
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.2</version>
<version>3.1.5</version>
<relativePath/>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.stream.Collectors;

@SuppressWarnings({"SpellCheckingInspection", "unused"})
@RequiredArgsConstructor
@ShellComponent
public class BookCommands {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.Optional;

@Repository
public class AuthorRepositoryJdbc implements AuthorRepository {
public class JdbcAuthorRepository implements AuthorRepository {

@Override
public List<Author> findAll() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.Optional;

@Repository
public class BookRepositoryJdbc implements BookRepository {
public class JdbcBookRepository implements BookRepository {

@Override
public Optional<Book> findById(long id) {
Expand Down Expand Up @@ -49,6 +49,7 @@ private Book insert(Book book) {

private Book update(Book book) {
//...
// Выбросить EntityNotFoundException если не обновлено ни одной записи в БД
return book;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.Optional;

@Repository
public class GenreRepositoryJdbc implements GenreRepository {
public class JdbcGenreRepository implements GenreRepository {

@Override
public List<Genre> findAll() {
Expand Down
Loading
Loading