-
Notifications
You must be signed in to change notification settings - Fork 0
Add friends likes #2
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
Conversation
| private final Map<Integer, Film> films = new HashMap<>(); | ||
|
|
||
| @Autowired | ||
| private FilmService filmService; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лучше инжектить зависимости через конструктор
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Аналогичный комментарий
No description provided.