Repository navigation
Add search #19
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
Merged
Merged
Add search #19
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dc328de
add film directors
6a6f5ba
trigger ci
36620a7
Fixed mistakes
3947d82
Fixed small mistakes
bb65ce4
Fixed mistakes and added info in README
2a9cfdf
Resolve minor bugs after merge
d834a57
Added import
4137b7b
Finished add-search
98fbab4
merge, resolve conflicts
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
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 |
|---|---|---|
|
|
@@ -104,6 +104,14 @@ public class FilmDbStorage implements FilmStorage { | |
| "WHERE fd.director_id = ? " + | ||
| "GROUP BY f.id, f.name, f.description, f.release_date, " + | ||
| "f.duration, f.mpa_id, m.name "; | ||
| private static final String FIND_FILMS_BY_SEARCH_QUERY = | ||
| "SELECT f.id, f.name, f.description, f.release_date, f.duration, " + | ||
| "f.mpa_id, m.name AS mpa_name " + | ||
| "FROM films f " + | ||
| "LEFT JOIN mpa m ON f.mpa_id = m.id " + | ||
| "LEFT JOIN film_director fd ON f.id = fd.film_id " + | ||
| "LEFT JOIN directors d ON fd.director_id = d.id " + | ||
| "LEFT JOIN film_likes fl ON f.id = fl.film_id "; | ||
|
|
||
| private final JdbcTemplate jdbc; | ||
| private final FilmRowMapper mapper; | ||
|
|
@@ -238,6 +246,40 @@ public List<Film> findFilmByDirector(long directorId, String sortBy) { | |
| return films; | ||
| } | ||
|
|
||
| @Override | ||
| public List<Film> searchFilms(String query, List<String> by) { | ||
| StringBuilder sql = new StringBuilder(FIND_FILMS_BY_SEARCH_QUERY); | ||
| List<String> conditions = new ArrayList<>(); | ||
| List<Object> params = new ArrayList<>(); | ||
|
|
||
| if (by.contains("director")) { | ||
| conditions.add("LOWER(d.name) LIKE ?"); | ||
| params.add("%" + query.toLowerCase() + "%"); | ||
| } | ||
|
|
||
| if (by.contains("title")) { | ||
| conditions.add("LOWER(f.name) LIKE ?"); | ||
| params.add("%" + query.toLowerCase() + "%"); | ||
| } | ||
|
|
||
| if (conditions.isEmpty()) { | ||
| throw new ValidationException("Необходимо указать поиск: director или title"); | ||
| } | ||
|
|
||
| sql.append("WHERE ") | ||
| .append(String.join(" OR ", conditions)) | ||
| .append(" "); | ||
|
|
||
| sql.append("GROUP BY f.id, f.name, f.description, f.release_date, ") | ||
| .append("f.duration, f.mpa_id, m.name ") | ||
| .append("ORDER BY COUNT(DISTINCT fl.user_id) DESC, f.id"); | ||
|
Owner
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. Классно, что |
||
| List<Film> films = jdbc.query(sql.toString(), mapper, params.toArray()); | ||
|
|
||
| loadDirectors(films); | ||
| loadGenresForFilms(films); | ||
| return films; | ||
| } | ||
|
|
||
| //Собирает запрос: чтение + WHERE по условиям + сортировка по числу лайков, без LIMIT | ||
| private String buildPopularQuery(List<String> conditions) { | ||
| StringBuilder sql = new StringBuilder(FIND_POPULAR_BASE_QUERY); | ||
|
|
||
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
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.
Здесь
by.contains(...)учитывает регистр, а вvalidateSearchParametersпроверка черезequalsIgnoreCase. Из-за этогоby=Titleпроходит валидацию, но тут не находит ни одного условия - и в ответ 400 «Необходимо указать поиск». Не блокирует, но правило лучше сделать одним в обоих местах.