From 35b326837fa22dc3834ec76de9840016be0ae0d0 Mon Sep 17 00:00:00 2001 From: viktoriakoroleva Date: Tue, 19 Mar 2024 20:17:56 +0300 Subject: [PATCH] int --- pom.xml | 5 ++ .../filmorate/controller/FilmController.java | 62 ++++++++++++++++- .../filmorate/controller/UserController.java | 63 ++++++++++++++++++ .../practicum/filmorate/model/Film.java | 57 ++++++++++++++++ .../practicum/filmorate/model/User.java | 66 +++++++++++++++++++ .../validation/ValidationException.java | 7 ++ 6 files changed, 259 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ru/yandex/practicum/filmorate/controller/UserController.java create mode 100644 src/main/java/ru/yandex/practicum/filmorate/model/User.java create mode 100644 src/main/java/ru/yandex/practicum/filmorate/validation/ValidationException.java diff --git a/pom.xml b/pom.xml index 371c29c8..183ffd57 100644 --- a/pom.xml +++ b/pom.xml @@ -32,6 +32,11 @@ spring-boot-starter-test test + + + org.springframework.boot + spring-boot-starter-validation + diff --git a/src/main/java/ru/yandex/practicum/filmorate/controller/FilmController.java b/src/main/java/ru/yandex/practicum/filmorate/controller/FilmController.java index 08cf0a18..1f40d4fb 100644 --- a/src/main/java/ru/yandex/practicum/filmorate/controller/FilmController.java +++ b/src/main/java/ru/yandex/practicum/filmorate/controller/FilmController.java @@ -2,6 +2,66 @@ import org.springframework.web.bind.annotation.RestController; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; +import ru.yandex.practicum.filmorate.model.Film; +import ru.yandex.practicum.filmorate.validation.ValidationException; + +import javax.validation.Valid; +import java.time.LocalDate; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Slf4j @RestController +@RequestMapping("/films") public class FilmController { -} + private final Map films = new HashMap<>(); + private int filmId = 1; + + private int generatorId() { + return filmId++; + } + + @GetMapping + public List findAll() { + log.info("Фильмов в коллекции: {}", films.size()); + return List.copyOf(films.values()); + } + + @PostMapping + public Film create(@Valid @RequestBody Film film) throws ValidationException { + log.debug("Попытка добавить фильм: {}", film); + + validateFilms(film); + film.setId(generatorId()); + films.put(film.getId(), film); + log.debug("Фильм успешно добавлен: {}", film); + + return film; + } + + @PutMapping + public Film put(@Valid @RequestBody Film film) throws ValidationException { + log.debug("Попытка обновить фильм: {}", film); + validateFilms(film); + + if (films.get(film.getId()) != null) { + films.replace(film.getId(), film); + log.debug("Фильм успешно обновлен: {}", film); + } else { + log.debug("Такого фильма не существует"); + throw new ValidationException("Такого фильма не существует"); + } + return film; + } + + public Film validateFilms(Film film) throws ValidationException { + if (film.getReleaseDate().isBefore(LocalDate.of(1895, 12, 28))) { + log.warn("Валидация не пройдена. Причина - дата релиза фильма"); + throw new ValidationException("Дата релиза не может быть раньше чем 28.12.1895"); + } + return film; + } +} \ No newline at end of file diff --git a/src/main/java/ru/yandex/practicum/filmorate/controller/UserController.java b/src/main/java/ru/yandex/practicum/filmorate/controller/UserController.java new file mode 100644 index 00000000..c48a3554 --- /dev/null +++ b/src/main/java/ru/yandex/practicum/filmorate/controller/UserController.java @@ -0,0 +1,63 @@ +package ru.yandex.practicum.filmorate.controller; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; +import ru.yandex.practicum.filmorate.model.User; +import ru.yandex.practicum.filmorate.validation.ValidationException; + +import javax.validation.Valid; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Slf4j +@RestController +@RequestMapping("/users") +public class UserController { + private final Map users = new HashMap<>(); + private int userId = 1; + + private int generatorId() { + return userId++; + } + + @GetMapping + public List findAll() { + log.info("Количество пользователей: {}", users.size()); + return List.copyOf(users.values()); + } + + @PostMapping + public User create(@Valid @RequestBody User user) throws ValidationException { + log.debug("Попытка добавить пользователя: {}", user); + + validateUsers(user); + user.setId(generatorId()); + users.put(user.getId(), user); + log.debug("Пользователь успешно добавлен: {}", user); + return user; + } + + @PutMapping + public User put(@Valid @RequestBody User user) throws ValidationException { + log.debug("Попытка обновить пользователя: {}", user); + + validateUsers(user); + if (users.get(user.getId()) != null) { + users.replace(user.getId(), user); + log.debug("Пользователь успешно обновлен: {}", user); + } else { + log.debug("Такого пользователя не существует"); + throw new ValidationException("Такого пользователя не существует"); + } + return user; + } + + public User validateUsers(User user) throws ValidationException { + if (user.getName() == null || user.getName().isBlank()) { + log.info("Установлен логин вместо имени пользователя"); + user.setName(user.getLogin()); + } + return user; + } +} \ No newline at end of file diff --git a/src/main/java/ru/yandex/practicum/filmorate/model/Film.java b/src/main/java/ru/yandex/practicum/filmorate/model/Film.java index 3614a44b..1fe83f29 100644 --- a/src/main/java/ru/yandex/practicum/filmorate/model/Film.java +++ b/src/main/java/ru/yandex/practicum/filmorate/model/Film.java @@ -1,12 +1,69 @@ package ru.yandex.practicum.filmorate.model; +import lombok.Data; import lombok.Getter; import lombok.Setter; +import java.time.LocalDate; + /** * Film. */ @Getter @Setter +@Data public class Film { + private int id; + private String name; + private String description; + private LocalDate releaseDate; + private int duration; + + public Film(int id, String name, String description, LocalDate releaseDate, int duration) { + this.id = id; + this.name = name; + this.description = description; + this.releaseDate = releaseDate; + this.duration = duration; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public LocalDate getReleaseDate() { + return releaseDate; + } + + public void setReleaseDate(LocalDate releaseDate) { + this.releaseDate = releaseDate; + } + + public int getDuration() { + return duration; + } + + public void setDuration(int duration) { + this.duration = duration; + } } diff --git a/src/main/java/ru/yandex/practicum/filmorate/model/User.java b/src/main/java/ru/yandex/practicum/filmorate/model/User.java new file mode 100644 index 00000000..b479c461 --- /dev/null +++ b/src/main/java/ru/yandex/practicum/filmorate/model/User.java @@ -0,0 +1,66 @@ +package ru.yandex.practicum.filmorate.model; + +import lombok.Data; +import lombok.Getter; +import lombok.Setter; + +import java.time.LocalTime; + +@Getter +@Setter +@Data +public class User { + private int id; + private String email; + private String login; + private String name; + private LocalTime birthday; + + public User(int id, String email, String login, String name, LocalTime birthday) { + this.id = id; + this.email = email; + this.login = login; + this.name = name; + this.birthday = birthday; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getLogin() { + return login; + } + + public void setLogin(String login) { + this.login = login; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public LocalTime getBirthday() { + return birthday; + } + + public void setBirthday(LocalTime birthday) { + this.birthday = birthday; + } +} diff --git a/src/main/java/ru/yandex/practicum/filmorate/validation/ValidationException.java b/src/main/java/ru/yandex/practicum/filmorate/validation/ValidationException.java new file mode 100644 index 00000000..18b30921 --- /dev/null +++ b/src/main/java/ru/yandex/practicum/filmorate/validation/ValidationException.java @@ -0,0 +1,7 @@ +package ru.yandex.practicum.filmorate.validation; + +public class ValidationException extends RuntimeException { + public ValidationException(String message) { + super(message); + } +}