-
Notifications
You must be signed in to change notification settings - Fork 0
Промежуточное задание 12-го спринта #3
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,39 @@ | ||
| # java-filmorate | ||
| Template repository for Filmorate project. | ||
|
|
||
| ## BD-structure | ||
|  | ||
|
|
||
| Получение списка фильмов по рейтингу: | ||
| ``` | ||
| SELECT f.film_id, | ||
| COUNT(l.user_id) | ||
| FROM film AS f | ||
| INNER JOIN likes AS l ON f.film_id = l.film_id | ||
| GROUP BY f.film_id | ||
| ORDER BY COUNT(l.user_id) DESC; | ||
| ``` | ||
|
|
||
| Получение списка друзей пользователя: | ||
| ``` | ||
| SELECT u.user_id, | ||
| fr.friend_id | ||
| FROM user AS u | ||
| INNER JOIN friends AS fr ON u.user_id = fr.user_id | ||
| GROUP BY u.user_id; | ||
| ``` | ||
|
|
||
| Получение списка названий фильмов в жанре COMEDY по продолжительности: | ||
| ``` | ||
| SELECT f.name, | ||
| f.duration | ||
| FROM film AS f | ||
| JOIN film_genre AS fg ON f.film_id = fg.film_id | ||
| JION genre AS g ON fg.genre_id = g.genre_id | ||
| WHERE g.name = 'COMEDY' | ||
| GROUP BY g.name | ||
| ORDER BY f.duration; | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package ru.yandex.practicum.filmorate.model; | ||
|
|
||
| public enum AgeRating { | ||
| G, | ||
| PG, | ||
| PG_13, | ||
| R, | ||
| NC_17 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package ru.yandex.practicum.filmorate.model; | ||
|
|
||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.EnumType; | ||
| import jakarta.persistence.Enumerated; | ||
| import lombok.Data; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| @Data | ||
| @Entity | ||
| public class Friends { | ||
| private int userId; | ||
| private int friendId; | ||
| @Enumerated(EnumType.STRING) | ||
| private Friendship friendship; | ||
|
|
||
| public Friends(int userId, int friendId, Friendship friendship) { | ||
| this.userId = userId; | ||
| this.friendId = friendId; | ||
| this.friendship = friendship; | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| Friends that = (Friends) o; | ||
| return userId == that.userId && friendId == that.friendId; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(userId, friendId); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package ru.yandex.practicum.filmorate.model; | ||
|
|
||
| public enum Friendship { | ||
| CONFIRMED, | ||
| NOT_CONFIRMED, | ||
| DENIED | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package ru.yandex.practicum.filmorate.model; | ||
|
|
||
| public enum Genre { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Использование enum для жанров ограничивает гибкость приложения. Например, если в будущем потребуется добавить новый жанр, это придётся делать через изменение кода enum, что приведёт к необходимости перекомпиляции.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. решила пока не переделывать enum, тоже почитала про него и его совместимость с sql и думаю попробовать, хотя очень вероятно, что при создании таблиц и переделаю в объект с полями (как ты говоришь) |
||
| ACTION_MOVIE, | ||
| COMEDY, | ||
| DRAMA, | ||
| CARTOON, | ||
| THRILLER, | ||
| DOCUMENTARY | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ | |
| import org.springframework.validation.annotation.Validated; | ||
| import ru.yandex.practicum.filmorate.exception.DuplicatedDataException; | ||
| import ru.yandex.practicum.filmorate.exception.NotFoundException; | ||
| import ru.yandex.practicum.filmorate.model.Friends; | ||
| import ru.yandex.practicum.filmorate.model.Friendship; | ||
| import ru.yandex.practicum.filmorate.model.User; | ||
| import ru.yandex.practicum.filmorate.storage.InMemoryUserStorage; | ||
| import ru.yandex.practicum.filmorate.storage.UserStorage; | ||
|
|
@@ -44,22 +46,28 @@ public Collection<User> getUsersList() { | |
| } | ||
|
|
||
|
|
||
| public void addFriend(int userId, int friendId) throws NotFoundException { | ||
| public void addFriend(int userId, int friendId, Friendship friendStatus) throws NotFoundException { | ||
| User user = getUserById(userId); | ||
| User friend = getUserById(friendId); | ||
| userStorage.addFriend(user, friend); | ||
| Friends userFriend = new Friends(userId, friendId, friendStatus); | ||
| Friends friendFriend = new Friends(friendId, userId, Friendship.CONFIRMED); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. при добавлении в друзья сразу создаются две связи, одна из которых со статусом CONFIRMED, даже если вторая сторона ещё не принимала заявку |
||
| userStorage.addFriend(user, friend, userFriend, friendFriend); | ||
| } | ||
|
|
||
| public Collection<String> getFriendsList(int id) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. метод возвращает список имен друзей, а не самих пользователей(или их идентификаторы), что затрудняет идентификацию. Может возникнуть путаница, т.к имена могут повторяться. Но если ранее замечаний не было, то ок)
|
||
| return userStorage.getFriendsList(id).stream() | ||
| .map(Friends::getFriendId) | ||
| .map(this::getUserById) | ||
| .map(User::getName) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public Collection<String> getCommonFriends(int userId, int friendId) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. метод также возвращает список имен. если ранее не было замечаний, то ок |
||
| Collection<User> friendsList = userStorage.getFriendsList(friendId); | ||
| var friendsList = userStorage.getFriendsList(friendId); | ||
| return userStorage.getFriendsList(userId).stream() | ||
| .filter(friendsList::contains) | ||
| .map(Friends::getFriendId) | ||
| .map(this::getUserById) | ||
| .map(User::getName) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
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.
GROUP BY вернет одну строку, соответсвенно и одного друга