generated from yandex-praktikum/java-filmorate
-
Notifications
You must be signed in to change notification settings - Fork 0
fix: убрать неиспользуемые библиотеки #5
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
Open
moratti72r
wants to merge
3
commits into
main
Choose a base branch
from
add-database
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/ru/yandex/practicum/filmorate/controller/GenreController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package ru.yandex.practicum.filmorate.controller; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import ru.yandex.practicum.filmorate.service.GenreService; | ||
| import ru.yandex.practicum.filmorate.model.Genres; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RequestMapping("/genres") | ||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class GenreController { | ||
|
|
||
| private final GenreService genreService; | ||
|
|
||
| @GetMapping() | ||
| public List<Genres> findAllGenres() { | ||
| return genreService.findAll(); | ||
| } | ||
|
|
||
| @GetMapping("/{id}") | ||
| public Genres getGenresById(@PathVariable Integer id) { | ||
| return genreService.findById(id); | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
src/main/java/ru/yandex/practicum/filmorate/controller/MPAController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package ru.yandex.practicum.filmorate.controller; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import ru.yandex.practicum.filmorate.model.MPA; | ||
| import ru.yandex.practicum.filmorate.service.MpaService; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RequestMapping("/mpa") | ||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class MPAController { | ||
|
|
||
| private final MpaService mpaService; | ||
|
|
||
| @GetMapping() | ||
| public List<MPA> findAllMpa() { | ||
| return mpaService.findAll(); | ||
| } | ||
|
|
||
| @GetMapping("/{id}") | ||
| public MPA getByIdMpa(@PathVariable Integer id) { | ||
| return mpaService.findById(id); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/ru/yandex/practicum/filmorate/dao/FriendsDao.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package ru.yandex.practicum.filmorate.dao; | ||
|
|
||
| public interface FriendsDao { | ||
|
|
||
| int addToFriends(int idUser, int idFriend); | ||
|
|
||
| int removeToFriends(int idUser, int idFriend); | ||
|
|
||
| } |
8 changes: 8 additions & 0 deletions
8
src/main/java/ru/yandex/practicum/filmorate/dao/LikesDao.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package ru.yandex.practicum.filmorate.dao; | ||
|
|
||
| public interface LikesDao { | ||
| int addLike(int idFilm, int idUser); | ||
|
|
||
| int removeLike(int idFilm, int idUser); | ||
|
|
||
| } |
30 changes: 30 additions & 0 deletions
30
src/main/java/ru/yandex/practicum/filmorate/dao/impl/FriendsDaoImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package ru.yandex.practicum.filmorate.dao.impl; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
| import org.springframework.stereotype.Component; | ||
| import ru.yandex.practicum.filmorate.dao.FriendsDao; | ||
|
|
||
|
|
||
| @Component | ||
| @AllArgsConstructor | ||
| public class FriendsDaoImpl implements FriendsDao { | ||
|
|
||
| private final JdbcTemplate jdbcTemplate; | ||
|
|
||
| @Override | ||
| public int addToFriends(int idUser, int idFriend) { | ||
|
|
||
| return jdbcTemplate.update("INSERT INTO friends (id_user,id_friend) VALUES (?,?)", idUser, idFriend); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public int removeToFriends(int idUser, int idFriend) { | ||
|
|
||
| return jdbcTemplate.update("DELETE FROM friends WHERE id_user = ? AND id_friend = ?", idUser, idFriend); | ||
|
|
||
|
|
||
| } | ||
|
|
||
| } |
35 changes: 35 additions & 0 deletions
35
src/main/java/ru/yandex/practicum/filmorate/dao/impl/LikesDaoImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package ru.yandex.practicum.filmorate.dao.impl; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
| import org.springframework.stereotype.Component; | ||
| import ru.yandex.practicum.filmorate.dao.LikesDao; | ||
|
|
||
| @Component | ||
| @AllArgsConstructor | ||
| public class LikesDaoImpl implements LikesDao { | ||
|
|
||
| private final JdbcTemplate jdbcTemplate; | ||
|
|
||
|
|
||
| @Override | ||
| public int addLike(int idFilm, int idUser) { | ||
|
|
||
| int result = jdbcTemplate.update("INSERT INTO likes (id_user,id_film) VALUES (?,?)", idUser, idFilm); | ||
| if (result != 0) upDateLikesFromFilm(idFilm); | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public int removeLike(int idFilm, int idUser) { | ||
|
|
||
| int result = jdbcTemplate.update("DELETE FROM likes WHERE id_user = ? AND id_film = ?", idUser, idFilm); | ||
| if (result != 0) upDateLikesFromFilm(idFilm); | ||
| return result; | ||
| } | ||
|
|
||
| private void upDateLikesFromFilm(int idFilm) { | ||
| String sqlQuery = "UPDATE films SET likes = (SELECT COUNT (id_user) FROM likes WHERE id_film = ?) where id = ?"; | ||
| jdbcTemplate.update(sqlQuery, idFilm, idFilm); | ||
| } | ||
| } |
170 changes: 170 additions & 0 deletions
170
src/main/java/ru/yandex/practicum/filmorate/dao/storageimpl/FilmDbStorage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| package ru.yandex.practicum.filmorate.dao.storageimpl; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
| import org.springframework.jdbc.core.ResultSetExtractor; | ||
| import org.springframework.jdbc.support.GeneratedKeyHolder; | ||
| import org.springframework.jdbc.support.rowset.SqlRowSet; | ||
| import org.springframework.stereotype.Component; | ||
| import ru.yandex.practicum.filmorate.model.Film; | ||
| import ru.yandex.practicum.filmorate.model.Genres; | ||
| import ru.yandex.practicum.filmorate.model.MPA; | ||
| import ru.yandex.practicum.filmorate.storage.FilmStorage; | ||
|
|
||
| import java.sql.Date; | ||
| import java.sql.PreparedStatement; | ||
| import java.util.*; | ||
|
|
||
| @Component | ||
| @AllArgsConstructor | ||
| public class FilmDbStorage implements FilmStorage { | ||
|
|
||
| private final JdbcTemplate jdbcTemplate; | ||
|
|
||
| private final String sq = "SELECT fgm.id,fgm.name,fgm.description,fgm.release_date,fgm.duration,fgm.likes," + | ||
| "fgm.mpa,m.name_mpa,fgm.id_genre,fgm.name_genre " + | ||
| "FROM mpa AS m RIGHT JOIN (SELECT * FROM films AS f LEFT JOIN " + | ||
| "(SELECT fg.id_film,fg.id_genre,g.name_genre " + | ||
| "FROM films_genres AS fg LEFT JOIN genres AS g " + | ||
| "ON g.id=fg.id_genre) " + | ||
| "ON f.id=id_film) AS fgm ON m.id=mpa "; | ||
|
|
||
| @Override | ||
| public List<Film> getAll() { | ||
| return new ArrayList<>(jdbcTemplate.query(sq, extractor()).values()); | ||
| } | ||
|
|
||
| @Override | ||
| public Film create(Film film) { | ||
|
|
||
| String sqlQuery = "INSERT INTO films (name," + | ||
| "description," + | ||
| "release_date," + | ||
| "duration," + | ||
| "likes," + | ||
| "mpa) " + | ||
| "VALUES (?,?,?,?,?,?)"; | ||
|
|
||
| GeneratedKeyHolder keyHolder = new GeneratedKeyHolder(); | ||
| jdbcTemplate.update(connection -> { | ||
| PreparedStatement psst = connection.prepareStatement(sqlQuery, new String[]{"id"}); | ||
| psst.setString(1, film.getName()); | ||
| psst.setString(2, film.getDescription()); | ||
| psst.setDate(3, Date.valueOf(film.getReleaseDate())); | ||
| psst.setInt(4, film.getDuration()); | ||
| psst.setInt(5, film.getLikes()); | ||
| psst.setInt(6, film.getMpa().getId()); | ||
| return psst; | ||
| }, keyHolder); | ||
|
|
||
| film.setId(keyHolder.getKey().intValue()); | ||
|
|
||
| if (!film.getGenres().isEmpty()) { | ||
| for (Genres gen : film.getGenres()) { | ||
| String sQuery = "INSERT INTO films_genres (id_film,id_genre) " + | ||
| "VALUES (?,?)"; | ||
|
|
||
| jdbcTemplate.update(sQuery, | ||
| film.getId(), | ||
| gen.getId()); | ||
| } | ||
| } | ||
| return film; | ||
| } | ||
|
|
||
| @Override | ||
| public int upDate(Film film) { | ||
| String sqlQuery = "UPDATE films SET name = ?," + | ||
| "description = ?," + | ||
| "release_date = ?," + | ||
| "duration = ?," + | ||
| "likes = ?," + | ||
| "mpa = ?" + | ||
| " WHERE id = ?"; | ||
|
|
||
| int result = jdbcTemplate.update(sqlQuery, | ||
| film.getName(), | ||
| film.getDescription(), | ||
| film.getReleaseDate(), | ||
| film.getDuration(), | ||
| film.getLikes(), | ||
| film.getMpa().getId(), | ||
| film.getId()); | ||
|
|
||
| if (result == 0) { | ||
| return result; | ||
| } | ||
|
|
||
| jdbcTemplate.update("DELETE FROM films_genres WHERE id_film = ?", film.getId()); | ||
|
|
||
| if (!film.getGenres().isEmpty()) { | ||
| for (Genres gen : film.getGenres()) { | ||
| String sQuery = "INSERT INTO films_genres (id_film,id_genre) " + | ||
| "VALUES (?,?)"; | ||
|
|
||
| jdbcTemplate.update(sQuery, | ||
| film.getId(), | ||
| gen.getId()); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean contains(int id) { | ||
| SqlRowSet filmRows = jdbcTemplate.queryForRowSet("select 1 from films where id = ?", id); | ||
| return filmRows.next(); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Film> getTopFilms(int limit) { | ||
| String sqlQuery = sq + "ORDER BY likes DESC LIMIT ?"; | ||
| return new ArrayList<>(jdbcTemplate.query(sqlQuery, extractor(), limit).values()); | ||
| } | ||
|
|
||
| @Override | ||
| public Film getById(int id) { | ||
| String sqq = "SELECT fgm.id,fgm.name,fgm.description,fgm.release_date,fgm.duration,fgm.likes," + | ||
| "fgm.mpa,m.name_mpa,fgm.id_genre,fgm.name_genre " + | ||
| "FROM mpa AS m RIGHT JOIN (SELECT * FROM films AS f LEFT JOIN " + | ||
| "(SELECT fg.id_film,fg.id_genre,g.name_genre " + | ||
| "FROM films_genres AS fg LEFT JOIN genres AS g " + | ||
| "ON g.id=fg.id_genre ) " + | ||
| "ON f.id=id_film where f.id = ?) AS fgm ON m.id=mpa "; | ||
| Film film = jdbcTemplate.query(sqq, extractor(), id).get(id); | ||
| return film; | ||
| } | ||
|
|
||
| private ResultSetExtractor<Map<Integer, Film>> extractor() { | ||
| return rs -> { | ||
|
|
||
| Map<Integer, Film> films = new HashMap<>(); | ||
| while (rs.next()) { | ||
| int id = rs.getInt("id"); | ||
| int idGenre = rs.getInt("id_genre"); | ||
| String nameGenre = rs.getString("name_genre"); | ||
| if (films.containsKey(id)) { | ||
| if (idGenre != 0) films.get(id).getGenres().add(new Genres(idGenre, nameGenre)); | ||
| } else { | ||
| Film film = new Film(); | ||
| films.put(id, film); | ||
| film.setId(id); | ||
| film.setName(rs.getString("name")); | ||
| film.setDescription(rs.getString("description")); | ||
| film.setReleaseDate(rs.getDate("release_date").toLocalDate()); | ||
| film.setDuration(rs.getInt("duration")); | ||
| film.setLikes(rs.getInt("likes")); | ||
|
|
||
| if (idGenre != 0) films.get(id).getGenres().add(new Genres(idGenre, nameGenre)); | ||
|
|
||
| int idMpa = rs.getInt("mpa"); | ||
| String nameMpa = rs.getString("name_mpa"); | ||
|
|
||
| film.setMpa(new MPA(idMpa, nameMpa)); | ||
|
|
||
| } | ||
| } | ||
| return films; | ||
| }; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@Min(value = 0)следует изменить на@Min(value = 1), так как нет смысла делать запрос на получение, если в любом случае вернется пустая коллекция) Допустимым лучше сделать значение от +1..