Skip to content
Open
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
21 changes: 18 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
<artifactId>filmorate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>filmorate</name>
<description>бэкенд для сервиса, который будет работать с фильмами и оценками пользователей, а также возвращать топ-5 фильмов, рекомендованных к просмотру.</description>
<description>бэкенд для сервиса, который будет работать с фильмами и оценками пользователей, а также возвращать
топ-5 фильмов, рекомендованных к просмотру.
</description>
<properties>
<java.version>11</java.version>
</properties>
Expand All @@ -24,9 +26,7 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand All @@ -37,6 +37,21 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.5.4</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
import ru.yandex.practicum.filmorate.service.FilmService;

import javax.validation.Valid;
import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("/films")
@Slf4j
@RequiredArgsConstructor
public class FilmController {

private final FilmService filmService;

@GetMapping
public List<Film> getFilmsList() {
public List<Film> getFilmsList() throws SQLException {
return filmService.getFilmsList();
}

Expand All @@ -29,7 +31,7 @@ public Film getFilmById(@PathVariable Integer id) {
}

@GetMapping("/popular")
public List<Film> getRatedFilmsList(@RequestParam(required = false, defaultValue = "10") Integer count) {
public List<Film> getRatedFilmsList(@RequestParam(required = false, defaultValue = "10") Integer count) throws SQLException {
return filmService.getMostPopularFilms(count);
}

Expand All @@ -44,7 +46,7 @@ public Film updFilm(@Valid @RequestBody Film film) {
}

@PutMapping("/{id}/like/{userId}")
public Film likeFilm(@PathVariable Integer id, @PathVariable Integer userId) {
public Film likeFilm(@PathVariable Integer id, @PathVariable Integer userId) throws DuplicateException {
return filmService.likeFilm(id, userId);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.yandex.practicum.filmorate.controllers;

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.Genre;
import ru.yandex.practicum.filmorate.storage.dao.FilmDao;

import java.util.List;

@RestController
@RequestMapping("/genres")
@Slf4j
@RequiredArgsConstructor
public class GenreController {
private final FilmDao filmDao;

@GetMapping
public List<Genre> getGenres() {
return filmDao.getGenres();
}

@GetMapping("/{id}")
public Genre getGenreByID(@PathVariable Integer id) {
return filmDao.getGenreById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.yandex.practicum.filmorate.controllers;

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.storage.dao.FilmDao;

import java.util.List;

@RestController
@RequestMapping("/mpa")
@Slf4j
@RequiredArgsConstructor
public class MpaController {
private final FilmDao filmDao;

@GetMapping
public List<MPARating> getRatings() {
return filmDao.getMPAS();
}

@GetMapping("/{id}")
public MPARating getGenreByID(@PathVariable Integer id) {
return filmDao.getMPAbyId(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,38 @@ public List<User> getUserList() {
}

@GetMapping("/{id}")
public User getUserById(@PathVariable Integer id) {
public User getUserById(@PathVariable Integer id) throws DuplicateException {
return userService.getUserById(id);
}

@GetMapping("/{id}/friends")
public List<User> getUserFriendList(@PathVariable Integer id) {
public List<User> getUserFriendList(@PathVariable Integer id) throws DuplicateException {
return userService.getUsersFrendsList(id);
}

@GetMapping("/{id}/friends/common/{otherId}")
public Set<User> getCommonFriends(@PathVariable Integer id
, @PathVariable Integer otherId) throws RuntimeException {
public Set<User> getCommonFriends(@PathVariable Integer id,
@PathVariable Integer otherId) throws DuplicateException {
return userService.getUsersCommonFriends(id, otherId);
}

@PostMapping
public User addUser(@Valid @RequestBody User user) {
public User addUser(@Valid @RequestBody User user) throws DuplicateException {
return userService.addUser(user);
}

@PutMapping
public User updUser(@Valid @RequestBody User user) {
public User updUser(@Valid @RequestBody User user) throws DuplicateException {
return userService.updateUser(user);
}

@PutMapping("/{id}/friends/{friendId}")
public User addFriend(@PathVariable Integer id, @PathVariable Integer friendId) throws DuplicateException {
public int addFriend(@PathVariable Integer id, @PathVariable Integer friendId) throws DuplicateException {
return userService.addFriend(id, friendId);
}

@DeleteMapping("/{id}/friends/{friendId}")
public User deleteFriend(@PathVariable Integer id, @PathVariable Integer friendId) throws DuplicateException {
public int deleteFriend(@PathVariable Integer id, @PathVariable Integer friendId) throws DuplicateException {
return userService.deleteFriend(id, friendId);
}
}
7 changes: 5 additions & 2 deletions src/main/java/ru/yandex/practicum/filmorate/model/Film.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
import javax.validation.executable.ValidateOnExecution;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Data
@Builder
@ValidateOnExecution
@FieldDefaults(level= AccessLevel.PRIVATE)
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Film {

Integer id;
Expand All @@ -32,6 +33,8 @@ public class Film {
LocalDate releaseDate;
@Positive
Long duration;
List<Genre> genres;
MPARating mpa;
final Set<Integer> userLikes = new HashSet<>();

private final Set<Integer> userLikes = new HashSet<>();
}
17 changes: 17 additions & 0 deletions src/main/java/ru/yandex/practicum/filmorate/model/Genre.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ru.yandex.practicum.filmorate.model;


import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldDefaults;

@Data
@AllArgsConstructor
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Genre {
Integer id;
String name;
}
16 changes: 16 additions & 0 deletions src/main/java/ru/yandex/practicum/filmorate/model/MPARating.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ru.yandex.practicum.filmorate.model;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldDefaults;

@Data
@AllArgsConstructor
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class MPARating {
Integer id;
String name;
}
4 changes: 3 additions & 1 deletion src/main/java/ru/yandex/practicum/filmorate/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@Builder
@AllArgsConstructor
@ValidateOnExecution

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Валидировать лучше в слое контроллеров

@FieldDefaults(level= AccessLevel.PRIVATE)
@FieldDefaults(level = AccessLevel.PRIVATE)
public class User {
Integer id;
@Email(message = "Incorrect Email!")
Expand All @@ -27,8 +27,10 @@ public class User {

String name;

@NotNull
@PastOrPresent(message = "Incorrect date!")
LocalDate birthday;

final Set<Integer> friends = new HashSet<>();
final Set<Integer> unacceptedFriends = new HashSet<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,61 @@
import ru.yandex.practicum.filmorate.model.Film;
import ru.yandex.practicum.filmorate.storage.FilmStorage;
import ru.yandex.practicum.filmorate.storage.UserStorage;
import ru.yandex.practicum.filmorate.util.LikesComparator;

import java.util.Comparator;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class FilmService {

private final FilmStorage filmStorage;
private final UserStorage userStorage;
private final Comparator<Film> likesComparator = new LikesComparator();
protected final Set<Film> filmsRating = new TreeSet<>(likesComparator);

public List<Film> getFilmsList() {
return filmStorage.getAll();
public List<Film> getFilmsList() throws SQLException {
return filmStorage.getFilmsList();
}

public Film getFilmById(Integer id) {
return filmStorage.get(id);
return filmStorage.getFilmById(id);
}

public Film addFilm(Film film) throws DuplicateException {
return filmStorage.create(film);
if (film.getGenres() == null) {
film.setGenres(new ArrayList<>());
}
return filmStorage.addFilm(film);
}

public Film updateFilm(Film film) {
return filmStorage.update(film);
if (film.getGenres() == null) {
film.setGenres(new ArrayList<>());
}
return filmStorage.updateFilm(film);
}

public Film likeFilm(Integer id, Integer userId) {
userStorage.get(userId); // немного костыль, но как мне кажется, самы простой способ проверить, что юзер есть - если его нет, то будет исключние
Film film = filmStorage.get(id);
film.getUserLikes().add(userId);
filmStorage.update(film);
public Film likeFilm(Integer id, Integer userId) throws DuplicateException {
userStorage.getUserById(userId);
Film film = filmStorage.getFilmById(id);
if (film.getUserLikes().contains(userId)) {
throw new DuplicateException("You already liked it!");
}
filmStorage.addFilmLike(id, userId);
return film;
}

public Film unlikeFilm(Integer id, Integer userId) {
Film film = filmStorage.get(id);
Film film = filmStorage.getFilmById(id);
if (!film.getUserLikes().contains(userId)) {
throw new NotFoundException("Film not found!");
throw new NotFoundException("User: " + userId + " not found!");
}
film.getUserLikes().remove(userId);
filmStorage.update(film);
filmStorage.deleteFilmLike(id, userId);
return film;
}

public List<Film> getMostPopularFilms(int count) {
filmsRating.addAll(filmStorage.filmsList.values());
return filmsRating.stream().limit(count).collect(Collectors.toList());
public List<Film> getMostPopularFilms(Integer count) throws SQLException {
return filmStorage.getMostPopularFilms(count);
}
}
Loading