generated from yandex-praktikum/java-filmorate
-
Notifications
You must be signed in to change notification settings - Fork 0
Add database #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
Merged
Merged
Add database #3
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d21a3c1
Sprint 12 add work H2 for Film
26511c8
Sprint 12 add work H2 for User & friends
ae6e814
Sprint 12 add work H2 for MPA
643c265
Sprint 12 add all
c83e45d
Sprint 12 add all
200bd14
Sprint 12 add all
5990cec
Sprint 12 add all
3cbfef6
Sprint 12 add all
91065f7
Sprint 12 Reviewer's comments
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
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
| package ru.yandex.practicum.filmorate.controller; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import ru.yandex.practicum.filmorate.model.Genre; | ||
| import ru.yandex.practicum.filmorate.service.GenreService; | ||
|
|
||
| import java.util.Collection; | ||
|
|
||
| @Slf4j | ||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class GenreController { | ||
|
|
||
| private final GenreService genreService; | ||
|
|
||
| @GetMapping("/genres/{id}") | ||
| public Genre getById(@PathVariable Integer id) { | ||
| return genreService.getById(id); | ||
| } | ||
|
|
||
| @GetMapping("/genres") | ||
| public Collection<Genre> getAll() { | ||
| return genreService.getAll(); | ||
| } | ||
|
|
||
| } |
31 changes: 31 additions & 0 deletions
31
src/main/java/ru/yandex/practicum/filmorate/controller/MpaRatingController.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,31 @@ | ||
| package ru.yandex.practicum.filmorate.controller; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| 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.MpaRating; | ||
| import ru.yandex.practicum.filmorate.service.MpaRatingService; | ||
|
|
||
| import java.util.Collection; | ||
|
|
||
| @Slf4j | ||
| @RestController | ||
| @RequestMapping("/mpa") | ||
| @RequiredArgsConstructor | ||
| public class MpaRatingController { | ||
| private final MpaRatingService mpaRatingService; | ||
|
|
||
| @GetMapping("/{id}") | ||
| public MpaRating getById(@PathVariable Integer id) { | ||
| return mpaRatingService.getById(id); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public Collection<MpaRating> getAll() { | ||
| return mpaRatingService.getAll(); | ||
| } | ||
|
|
||
| } |
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
49 changes: 49 additions & 0 deletions
49
src/main/java/ru/yandex/practicum/filmorate/mappers/FilmRowMapper.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,49 @@ | ||
| package ru.yandex.practicum.filmorate.mappers; | ||
|
|
||
| import org.springframework.jdbc.core.RowMapper; | ||
| import org.springframework.stereotype.Component; | ||
| import ru.yandex.practicum.filmorate.model.Film; | ||
| import ru.yandex.practicum.filmorate.model.Genre; | ||
| import ru.yandex.practicum.filmorate.storage.genre.GenreDbStorage; | ||
| import ru.yandex.practicum.filmorate.storage.mpa.MpaRatingDbStorage; | ||
|
|
||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.sql.Timestamp; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| @Component | ||
| public class FilmRowMapper implements RowMapper<Film> { | ||
| private final GenreDbStorage genreDbStorage; | ||
| private final MpaRatingDbStorage mpa; | ||
|
|
||
| public FilmRowMapper(GenreDbStorage genreDbStorage, MpaRatingDbStorage mpa) { | ||
| this.genreDbStorage = genreDbStorage; | ||
| this.mpa = mpa; | ||
| } | ||
|
|
||
| @Override | ||
| public Film mapRow(ResultSet resultSet, int rowNum) throws SQLException { | ||
| Timestamp releaseDate = resultSet.getTimestamp("release_date"); | ||
| String genreIds = resultSet.getString("genre_ids"); | ||
| List<Genre> genres = new ArrayList<>(); | ||
| if (!genreIds.isEmpty()) { | ||
| String[] genreIdsArray = genreIds.split(",\\s*"); | ||
| for (String id : genreIdsArray) { | ||
| genres.add(genreDbStorage.getById(Integer.parseInt(id))); | ||
| } | ||
| } | ||
|
|
||
| return Film.builder() | ||
| .id(resultSet.getInt("film_id")) | ||
| .name(resultSet.getString("film_name")) | ||
| .description(resultSet.getString("description")) | ||
| .releaseDate(releaseDate.toLocalDateTime().toLocalDate()) | ||
| .duration(resultSet.getInt("duration_minutes")) | ||
| .likesCount(resultSet.getInt("likes_count")) | ||
| .mpa(mpa.geById(resultSet.getInt("mpa_id"))) | ||
| .genres(genres) | ||
| .build(); | ||
| } | ||
| } |
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
16 changes: 16 additions & 0 deletions
16
src/main/java/ru/yandex/practicum/filmorate/model/Friendship.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,16 @@ | ||
| package ru.yandex.practicum.filmorate.model; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
|
|
||
| @Builder | ||
| @Data | ||
| public class Friendship { | ||
| @NotNull | ||
| private Integer firstUserId; | ||
| @NotNull | ||
| private Integer secondUserId; | ||
| @NotNull | ||
| private Boolean status; | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/ru/yandex/practicum/filmorate/model/Genre.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,15 @@ | ||
| package ru.yandex.practicum.filmorate.model; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
|
|
||
| @Builder | ||
| @Data | ||
| public class Genre { | ||
| private Integer id; | ||
| @NotNull | ||
| @NotBlank | ||
| private String name; | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/ru/yandex/practicum/filmorate/model/MpaRating.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,15 @@ | ||
| package ru.yandex.practicum.filmorate.model; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
|
|
||
| @Builder | ||
| @Data | ||
| public class MpaRating { | ||
| private Integer id; | ||
| @NotNull | ||
| @NotBlank | ||
| private String name; | ||
| } |
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
31 changes: 18 additions & 13 deletions
31
src/main/java/ru/yandex/practicum/filmorate/service/FilmService.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 |
|---|---|---|
| @@ -1,42 +1,47 @@ | ||
| package ru.yandex.practicum.filmorate.service; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.beans.factory.annotation.Qualifier; | ||
| import org.springframework.stereotype.Service; | ||
| import ru.yandex.practicum.filmorate.model.Film; | ||
| import ru.yandex.practicum.filmorate.storage.film.InMemoryFilmStorage; | ||
| import ru.yandex.practicum.filmorate.storage.film.FilmStorage; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Service | ||
| public class FilmService { | ||
|
|
||
| private final InMemoryFilmStorage inMemoryFilmStorage; | ||
| private final FilmStorage filmStorage; | ||
|
|
||
| public FilmService(@Qualifier("filmDbStorage") FilmStorage filmStorage) { | ||
| this.filmStorage = filmStorage; | ||
| } | ||
|
|
||
| public Collection<Film> getAll() { | ||
| return inMemoryFilmStorage.getAll(); | ||
| return filmStorage.getAll(); | ||
| } | ||
|
|
||
| public Film getFilmById(int id) { | ||
| return filmStorage.getFilmById(id); | ||
| } | ||
|
|
||
| public Film create(Film film) { | ||
| return inMemoryFilmStorage.create(film); | ||
| return filmStorage.create(film); | ||
| } | ||
|
|
||
| public Film update(Film filmUpdate) { | ||
| return inMemoryFilmStorage.update(filmUpdate); | ||
| return filmStorage.update(filmUpdate); | ||
| } | ||
|
|
||
| public Map<Film, Set<Integer>> addLike(int filmId, int userId) { | ||
| return inMemoryFilmStorage.addLike(filmId, userId); | ||
| public boolean addLike(int filmId, int userId) { | ||
| return filmStorage.addLike(filmId, userId); | ||
| } | ||
|
|
||
| public void removeLike(int filmId, int userId) { | ||
| inMemoryFilmStorage.removeLike(filmId, userId); | ||
| filmStorage.removeLike(filmId, userId); | ||
| } | ||
|
|
||
| public Collection<Film> getFilmsByLike(Integer sizeFilms) { | ||
| return inMemoryFilmStorage.getFilmsByLike(sizeFilms); | ||
| return filmStorage.getFilmsByLike(sizeFilms); | ||
| } | ||
|
|
||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/java/ru/yandex/practicum/filmorate/service/GenreService.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,23 @@ | ||
| package ru.yandex.practicum.filmorate.service; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import ru.yandex.practicum.filmorate.model.Genre; | ||
| import ru.yandex.practicum.filmorate.storage.genre.GenreDbStorage; | ||
|
|
||
| import java.util.Collection; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class GenreService { | ||
| private final GenreDbStorage genreDbStorage; | ||
|
|
||
| public Genre getById(Integer id) { | ||
| return genreDbStorage.getById(id); | ||
| } | ||
|
|
||
| public Collection<Genre> getAll() { | ||
| return genreDbStorage.getAll(); | ||
| } | ||
|
|
||
| } | ||
23 changes: 23 additions & 0 deletions
23
src/main/java/ru/yandex/practicum/filmorate/service/MpaRatingService.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,23 @@ | ||
| package ru.yandex.practicum.filmorate.service; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import ru.yandex.practicum.filmorate.model.MpaRating; | ||
| import ru.yandex.practicum.filmorate.storage.mpa.MpaRatingDbStorage; | ||
|
|
||
| import java.util.Collection; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class MpaRatingService { | ||
| private final MpaRatingDbStorage mpaRatingDbStorage; | ||
|
|
||
| public MpaRating getById(Integer id) { | ||
| return mpaRatingDbStorage.geById(id); | ||
| } | ||
|
|
||
| public Collection<MpaRating> getAll() { | ||
| return mpaRatingDbStorage.getAll(); | ||
| } | ||
|
|
||
| } |
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.
Слишком много пустых строк