3
3
import lombok .RequiredArgsConstructor ;
4
4
import lombok .extern .slf4j .Slf4j ;
5
5
import org .springframework .stereotype .Service ;
6
+ import org .springframework .transaction .annotation .Transactional ;
6
7
import ru .yandex .practicum .filmorate .dao .*;
7
8
import ru .yandex .practicum .filmorate .dto .FeedDto ;
8
9
import ru .yandex .practicum .filmorate .dto .FilmDto ;
@@ -36,6 +37,7 @@ public class UserServiceImpl implements UserService {
36
37
* @return пользователь с присвоенным идентификатором
37
38
*/
38
39
@ Override
40
+ @ Transactional
39
41
public UserDto addUser (final UserDto userDto ) {
40
42
final User user = UserMapper .toModel (validateUserName (userDto ));
41
43
final User addedUser = userStorage .add (user );
@@ -50,6 +52,7 @@ public UserDto addUser(final UserDto userDto) {
50
52
* @return пользователь с обновленными данными.
51
53
*/
52
54
@ Override
55
+ @ Transactional
53
56
public UserDto updateUser (final UserDto updatedUserDto ) {
54
57
final User updatedUser = UserMapper .toModel (validateUserName (updatedUserDto ));
55
58
final long userId = updatedUser .getId ();
@@ -91,6 +94,7 @@ public UserDto getUserById(final long userId) {
91
94
* @return пользователь с обновленным списком друзей.
92
95
*/
93
96
@ Override
97
+ @ Transactional
94
98
public UserDto addFriend (final long userId , final long friendId ) {
95
99
final User user = userStorage .findById (userId );
96
100
final User friend = userStorage .findById (friendId );
@@ -147,6 +151,7 @@ public Collection<UserDto> findCommonFriends(final long userId, final long other
147
151
* @param friendId идентификатор друга, которого требуется исключить из списка друзей.
148
152
*/
149
153
@ Override
154
+ @ Transactional
150
155
public void removeFriend (final long userId , final long friendId ) {
151
156
userStorage .findById (userId );
152
157
userStorage .findById (friendId );
@@ -179,11 +184,12 @@ public void removeUser(long userId) {
179
184
public Collection <FilmDto > showRecommendations (long id ) {
180
185
log .info ("Получение списка рекомендаций фильмов для пользователя с id {}." , id );
181
186
int positiveRating = 6 ;
182
- Map <Long , Set <Long >> usersLikes = filmStorage .getUsersAndFilmLikes ();
187
+ Map <Long , Map <Long , Integer >> usersLikes = filmStorage .getUsersAndFilmLikes ();
183
188
Map <Long , Set <Film >> usersLikedFilms = filmStorage .findAllFilmsLikedByUsers ();
184
189
int maxLikes = 0 ;
185
190
Set <Film > recommendations = new HashSet <>();
186
191
Set <Film > userLikedFilms = usersLikedFilms .get (id );
192
+ Map <Long , Integer > userFilmIdRating = usersLikes .get (id );
187
193
for (Long userId : usersLikes .keySet ()) {
188
194
if (userId != id ) {
189
195
Set <Film > sameFilms = new HashSet <>();
@@ -196,10 +202,11 @@ public Collection<FilmDto> showRecommendations(long id) {
196
202
} else {
197
203
sameFilm = optionalFilm .get ();
198
204
}
199
- if ((film .getRating () >= positiveRating && sameFilm .getRating () >= positiveRating ) ||
200
- (film .getRating () < positiveRating && sameFilm .getRating () < positiveRating )) {
205
+ long filmId = sameFilm .getId ();
206
+ Map <Long , Integer > anotherUserFilmIdRating = usersLikes .get (userId );
207
+ if ((userFilmIdRating .get (filmId ) >= positiveRating && anotherUserFilmIdRating .get (filmId ) >= positiveRating ) ||
208
+ (userFilmIdRating .get (filmId ) < positiveRating && anotherUserFilmIdRating .get (filmId ) < positiveRating ))
201
209
sameFilms .add (film );
202
- }
203
210
}
204
211
if (sameFilms .size () > maxLikes && sameFilms .size () < anotherUserLikedFilms .size ()) {
205
212
recommendations .clear ();
@@ -225,12 +232,12 @@ public Collection<FilmDto> showRecommendations(long id) {
225
232
}
226
233
227
234
/**
228
- * Выгразка ленты пользователя. Запрос выгружает историй действий пользователя:
235
+ * Выгрузка ленты пользователя. Запрос выгружает историй действий пользователя:
229
236
* кого он добавлял в друзья и удалял из друзей
230
237
* что лайкал
231
238
* какие писал и удалял отзывы
232
239
*
233
- * @param id идентификатор пользоваетеля
240
+ * @param id идентификатор пользователя
234
241
* @return коллекция FeedDto
235
242
*/
236
243
0 commit comments