Skip to content

Conversation

@Max-Browckin
Copy link
Owner

No description provided.

Copy link

@avfyodorov avfyodorov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Добрый день, Максим!

В целом хорошо, правда, есть ряд уточнений.

public class FilmDbStorage implements FilmStorage {
private final JdbcTemplate jdbc;
private final MpaDbStorage mpaStorage;
private final GenreDbStorage genreStorage;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private final MpaDbStorage mpaStorage;
private final GenreDbStorage genreStorage;

Вообще говоря, нет, это нехорошо. Репозитории (DAO) ничего друг про друга знать не должны. Репозиторий выполняет crud команды для своей таблицы (набора данных) и, если надо, возвращает результаты в сервис.
Если же репозиторий вызывает другие репозитории, то есть фактически зависит от них, то это серьёзный повод пересмотреть архитектуру программы.
Вся бизнес логика находится в сервисах. Сервис может обращаться к другим сервисам, а также получать данные из нескольких репозиториев. Репозиторий же настроен на свою таблицу.

List<Film> films = jdbc.query(sql, this::makeFilm);
for (Film f : films) {
f.setMpa(mpaStorage.findById(f.getMpa().getId()));
List<Genre> genres = genreStorage.getGenresForFilm(f.getId());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В этом месте, в цикле, идут запросы к базе данных. Было бы хорошо избавиться от этих многочисленных запросов, которые сильно нагружают сервер. Хорошо бы переделать данный метод, чтобы уменьшить число обращений к БД .

List<Film> films = jdbc.query(sql, this::makeFilm, count);
for (Film f : films) {
f.setMpa(mpaStorage.findById(f.getMpa().getId()));
List<Genre> genres = genreStorage.getGenresForFilm(f.getId());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

        f.setMpa(mpaStorage.findById(f.getMpa().getId()));
        List<Genre> genres = genreStorage.getGenresForFilm(f.getId());

Здесь та же история-запросы в цикле. Нужно постараться избавиться..Сократить количество запросов.

if (film.getGenres() != null && !film.getGenres().isEmpty()) {
String sql = "INSERT INTO film_genres (film_id, genre_id) VALUES (?, ?)";
for (Genre genre : film.getGenres()) {
jdbc.update(sql, film.getId(), genre.getId());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Хорошо было бы сохранить данные одним запросом, например, с помощью конструкции:
jdbcTemplate.batchUpdate(............

if (film.getGenres() != null) {
Set<Genre> genres = film.getGenres().stream()
.filter(Objects::nonNull)
.map(g -> genreStorage.findById(g.getId()))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

и здесь запросы к БД в цикле.

Copy link

@avfyodorov avfyodorov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Добрый день, Максим!

Замечаний нет.
Работа принята.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants