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
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
<artifactId>logbook-spring-boot-starter</artifactId>
<version>3.7.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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.Genre;
import ru.yandex.practicum.filmorate.service.GenreService;

import java.util.List;

@RestController
@RequestMapping(value = "/genres")
@RequiredArgsConstructor
public class GenreController {

Choose a reason for hiding this comment

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

добавь логи в методы

private final GenreService service;

@GetMapping
public List<Genre> getAll() {
return service.getAll();
}

@GetMapping("/{id}")
public Genre getById(@PathVariable Long id) {
return service.getById(id);
}
}
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.RatingMPA;
import ru.yandex.practicum.filmorate.service.RatingMPAService;

import java.util.List;

@RestController
@RequestMapping(value = "/mpa")
@RequiredArgsConstructor
public class RatingMPAController {

Choose a reason for hiding this comment

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

и сюда тоже


private final RatingMPAService service;

@GetMapping
public List<RatingMPA> getAll() {
return service.getAll();
}

@GetMapping("/{id}")
public RatingMPA getById(@PathVariable Long id) {
return service.getById(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.yandex.practicum.filmorate.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

Expand All @@ -17,6 +18,15 @@ public ErrorResponse validateHandler(ValidationException e) {
);
}

@org.springframework.web.bind.annotation.ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse validateHandler(MethodArgumentNotValidException e) {
return new ErrorResponse(
"Ошибка валидации",
e.getMessage()
);
}

@org.springframework.web.bind.annotation.ExceptionHandler
@ResponseStatus(HttpStatus.NOT_FOUND)
public ErrorResponse notFoundHandler(NotFoundException e) {
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/ru/yandex/practicum/filmorate/model/Film.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.*;

import java.time.LocalDate;
import java.util.ArrayList;
Expand Down Expand Up @@ -37,6 +34,12 @@ public class Film {

private List<Long> likes = new ArrayList<>();

private List<Genre> genres;

@NonNull
private RatingMPA mpa;


public void addLike(Long userId) {
likes.add(userId);
}
Expand Down
14 changes: 14 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,14 @@
package ru.yandex.practicum.filmorate.model;

import lombok.*;

@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class Genre {

private Long id;
private String name;

}
14 changes: 14 additions & 0 deletions src/main/java/ru/yandex/practicum/filmorate/model/RatingMPA.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.yandex.practicum.filmorate.model;

import lombok.*;

@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class RatingMPA {

private Long id;
private String name;

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public class FilmService {

final FilmStorage filmStorage;

private final GenreService genreService;

private final RatingMPAService ratingMPAService;

public List<Film> getFilms() {
log.info("Запрос всех фильмов");
return filmStorage.getFilms();
Expand All @@ -35,7 +39,15 @@ public Film create(Film film) {

film.setId(getNextId());

filmStorage.addOrUpdateFilm(film);
if (film.getMpa() != null) {
ratingMPAService.getById(film.getMpa().getId());
}
if (film.getGenres() != null) {
film.getGenres().forEach(genre -> {
genreService.getById(genre.getId());
});
}
filmStorage.createFilm(film);
log.info("Фильм добавлен");
return film;
}
Expand All @@ -54,7 +66,7 @@ public Film update(Film newFilm) {
oldFilm.setDescription(newFilm.getDescription());
oldFilm.setReleaseDate(newFilm.getReleaseDate());
oldFilm.setDuration(newFilm.getDuration());
filmStorage.addOrUpdateFilm(oldFilm);
filmStorage.updateFilm(oldFilm);
log.info("Фильм с id " + newFilm.getId() + " обновлен");
return oldFilm;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ru.yandex.practicum.filmorate.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import ru.yandex.practicum.filmorate.model.Genre;
import ru.yandex.practicum.filmorate.storage.interfaces.GenreStorage;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class GenreService {

private final GenreStorage storage;

public List<Genre> getAll() {
log.info("Запрос всех жанров");
return storage.getAll();
}

public Genre getById(Long id) {
log.info("Запрос жанра с id = " + id);
return storage.getById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ru.yandex.practicum.filmorate.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import ru.yandex.practicum.filmorate.model.RatingMPA;
import ru.yandex.practicum.filmorate.storage.interfaces.RatingMPAStorage;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class RatingMPAService {

private final RatingMPAStorage storage;

public List<RatingMPA> getAll() {
log.info("Запрос всех рейтингов MPA");
return storage.getAll();
}

public RatingMPA getById(Long id) {
log.info("Запрос рейтинга MPA с id = " + id);
return storage.getById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public User createUser(User user) {
user.setId(getNextId());
user.setNameWithCheck(user);

userStorage.addOrUpdateUser(user);
userStorage.createUser(user);
log.info("Пользователь добавлен");
return user;
}
Expand All @@ -58,7 +58,7 @@ public User updateUser(User newUser) {
oldUser.setNameWithCheck(newUser);
oldUser.setLogin(newUser.getLogin());
oldUser.setBirthday(newUser.getBirthday());
userStorage.addOrUpdateUser(oldUser);
userStorage.updateUser(oldUser);
log.info("Пользователь с id " + newUser.getId() + " обновлен");
return oldUser;
}
Expand All @@ -78,17 +78,15 @@ public List<User> getCommonFriends(Long userId, Long otherUserId) {

public void addFriend(Long userId, Long friendId) {
User user = getUserByIdWithCheck(userId);
User friend = getUserByIdWithCheck(friendId);
getUserByIdWithCheck(friendId);
userStorage.addFriends(user, friendId);
userStorage.addFriends(friend, userId);
log.info("Пользователи с id = " + userId + " и " + friendId + " теперь друзья");
}

public void removeFriends(Long userId, Long friendId) {
User user = getUserByIdWithCheck(userId);
User friend = getUserByIdWithCheck(friendId);
getUserByIdWithCheck(friendId);
userStorage.removeFriends(user, friendId);
userStorage.removeFriends(friend, userId);
log.info("Пользователи с id = " + userId + " и " + friendId + " перестали быть друзьями");
}

Expand Down
Loading