Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public void deleteLike(@PathVariable @Positive long id,
filmService.removeLike(id, userId);
}

@GetMapping("/common")
public Collection<FilmDto> findCommonFilms(@RequestParam(name = "userId") @Positive long userId,
@RequestParam(name = "friendId") @Positive long friendId) {
log.trace("Find common films requested for user {} and friend {}", userId, friendId);
return filmService.findCommonFilms(userId, friendId);
}

@GetMapping("/popular")
public Collection<FilmDto> findPopular(@RequestParam(defaultValue = "10") @Positive int count) {
log.trace("Find popular film requested with count: {}", count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public interface FilmRepository {
void deleteById(long id);

Collection<Film> findTopPopularFilms(int count);

Collection<Film> findCommonFilms(long userId, long friendId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,40 @@ ORDER BY COUNT(fl.user_id) DESC, f.film_id
return jdbc.query(selectTopFilmsSql, params, filmResultSetExtractor);
}

@Override
public Collection<Film> findCommonFilms(long userId, long friendId) {
String selectCommonFilmsSql = """
SELECT f.film_id,
f.name,
f.description,
f.release_date,
f.duration_in_minutes,
f.mpa_id,
mr.name AS mpa_name,
g.genre_id,
g.name AS genre_name
FROM films f
JOIN mpa_ratings mr ON f.mpa_id = mr.mpa_id
LEFT JOIN film_genres fg ON f.film_id = fg.film_id
LEFT JOIN genres g ON g.genre_id = fg.genre_id
LEFT JOIN likes fl ON f.film_id = fl.film_id
WHERE f.film_id in (SELECT l1.film_id
FROM likes l1
WHERE l1.user_id = :user_id
INTERSECT
SELECT l2.film_id
FROM likes l2
WHERE l2.user_id = :friend_id)
GROUP BY f.film_id, f.name, g.genre_id
ORDER BY COUNT(fl.user_id) DESC, f.film_id
""";
MapSqlParameterSource params = new MapSqlParameterSource()
.addValue("user_id", userId)
.addValue("friend_id", friendId);

return jdbc.query(selectCommonFilmsSql, params, filmResultSetExtractor);
}

private void saveGenres(Set<Genre> genres, long filmId) {
String insertGenresSql = """
INSERT INTO film_genres (film_id, genre_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ public interface FriendshipsRepository {
void addFriendship(long userId, long friendId);

void removeFriendship(long userId, long friendId);

boolean isFriends(long userId, long friendId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,23 @@ public void removeFriendship(long userId, long friendId) {
jdbc.update(deleteFriendshipSql, params);
}

@Override
public boolean isFriends(long userId, long friendId) {
String checkFriendshipSql = """
SELECT EXISTS (
SELECT 1
FROM friendships
WHERE user_id1 = :user_id AND user_id2 = :friend_id)""";
MapSqlParameterSource params = new MapSqlParameterSource()
.addValue("user_id", userId)
.addValue("friend_id", friendId);

return Boolean.TRUE.equals(jdbc.queryForObject(checkFriendshipSql, params, Boolean.class));
}

private MapSqlParameterSource getParameterMap(long userId, long friendId) {
return new MapSqlParameterSource()
.addValue("user_id1", userId)
.addValue("user_id2", friendId);
.addValue("user_id1", userId)
.addValue("user_id2", friendId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ public interface FilmService {
void removeLike(long filmId, long userId);

Collection<FilmDto> findFilmsWithTopLikes(int count);

Collection<FilmDto> findCommonFilms(long userId, long friendId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ public Collection<FilmDto> findFilmsWithTopLikes(int count) {
.toList();
}

@Override
public Collection<FilmDto> findCommonFilms(long userId, long friendId) {
throwIfUserNotFound(userId);
throwIfUserNotFound(friendId);

return filmRepository.findCommonFilms(userId, friendId)
.stream()
.map(FilmMapper::toFilmDto)
.toList();
}

private void throwIfUserNotFound(long userId) {
userRepository.findById(userId)
.orElseThrow(NotFoundException.supplier("User with id %d not found", userId));
Expand Down