Skip to content

Conversation

@Just-Roman
Copy link
Owner

No description provided.

private final Map<Integer, Film> films = new HashMap<>();

@Autowired
private FilmService filmService;

Choose a reason for hiding this comment

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

Лучше инжектить зависимости через конструктор

Choose a reason for hiding this comment

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

Замечание для всего кода

}

public Map<Film, Set<Integer>> addLike(int filmId, int userId) {
inMemoryUserStorage.validateUserId(userId);

Choose a reason for hiding this comment

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

Вызов метода validateUserId можно вынести в метод addLike

}

public void removeLike(int filmId, int userId) {
inMemoryUserStorage.validateUserId(userId);

Choose a reason for hiding this comment

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

Аналогичный комментарий


Collection<Film> getFilmsByLike(Integer sizeFilms);

void validateFilmId(Integer id);

Choose a reason for hiding this comment

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

Кажется 4 последних метода лучше сделать приватными


@Override
public Collection<Film> getAll() {
log.debug("GET, all films");

Choose a reason for hiding this comment

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

Уровень info будет достаточно для всех информационных логов

String newEmail = newUser.getEmail();
String newLogin = newUser.getLogin();
oldUser.setName(newUser.getName());
if (newUser.getBirthday() != null) oldUser.setBirthday(newUser.getBirthday());

Choose a reason for hiding this comment

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

Лучше if указывать в фигурных скобках, так код становится более читаемым

validateUserId(userId);
validateUserId(friendsId);

if (checkUserFriends(userId)) {

Choose a reason for hiding this comment

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

Блок можно переписать в одну строчку:

        if (checkUserFriends(userId)) {
            userFriends.get(userId).add(friendsId);
        }

}
}

if (checkUserFriends(friendsId)) {

Choose a reason for hiding this comment

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

Кажется лишняя проверка, если у первого есть коллекция друзей, то и у второго тоже они будут, так как процесс добавления синхронный

Set<Integer> user2FriendsId = userFriends.get(friendsId);
List<User> mutualFriends = new ArrayList<>();

for (int id1 : user1FriendsId) {

Choose a reason for hiding this comment

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

Попробуй вместо двойного цикла использовать метод intersect у коллекции set


Collection<User> getMutualFriends(int userId, int friendsId);

void validateUserId(int id);

Choose a reason for hiding this comment

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

Последние 5 методов можно сделать приватными


private final Map<Integer, Film> films = new HashMap<>();
private final Map<Integer, Set<Integer>> filmsLikes = new HashMap<>();
LocalDate birthdayFilm = LocalDate.of(1895, 12, 28);

Choose a reason for hiding this comment

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

Так как это констатнта, то следует указать private final

}

@Override
public Collection<Film> getFilmsByLike(Integer sizeFilms) {

Choose a reason for hiding this comment

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

Смысл этого метода в том, чтобы вернуть определенный размер популярных фильмы по лайкам, он реализуется путем сортировки массива films по лайкам и получения размера массива в зависимости от параметра sizeFilms, то есть реализацию можно записать таким образом:

    public Collection<Film> getFilmsByLike(Integer sizeFilms) {
        return films.values()
                .stream()
                .sorted(Comparator.comparing(Film::getLikes).reversed())
                .limit(sizeFilms)
                .collect(Collectors.toList());
    }

Film film = films.get(filmId);

film.setLikes(film.getLikes() + 1);
films.put(filmId, film);

Choose a reason for hiding this comment

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

put в данном случае лишний, так как объект film выбирается по ссылке


if (like != 0) {
film.setLikes(like - 1);
films.put(filmId, film);

Choose a reason for hiding this comment

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

Аналогичный комментарий

if (friendsIds1.isEmpty()) {
userFriends.remove(userId);
} else {
userFriends.put(userId, friendsIds1);

Choose a reason for hiding this comment

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

put лишний, так как объект friendsIds1 выбирается по ссылке

if (friendsIds2.isEmpty()) {
userFriends.remove(friendsId);
} else {
userFriends.put(friendsId, friendsIds2);

Choose a reason for hiding this comment

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

Аналогичный комментарий

@Just-Roman Just-Roman merged commit a4d82dd into main Jan 26, 2025
2 checks passed
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