-
Notifications
You must be signed in to change notification settings - Fork 0
Controllers films users #1
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
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d40202e
Add models
Karat120 e398275
Add FilmNotFoundException
Karat120 fd6977e
Add FilmController
Karat120 4aa5093
Add UserNotFoundException
Karat120 4e8db36
Add UserController
Karat120 ec76a4e
Add FilmControllerTest
Karat120 b076f51
Add UserControllerTest
Karat120 d7657e3
Add ValidationException
Karat120 c69bbdb
Add validation in User and Film
Karat120 890af88
Add logging
Karat120 a5409f6
Add tests for models
Karat120 9232c71
Add maven-checkstyle-plugin
Karat120 3cfee42
Add GlobalExceptionHandler
Karat120 93d658d
Edit Film model
Karat120 9ade40c
Fix tests
Karat120 9922975
Add NotBeforeCinemaBirthday and NotBeforeCinemaBirthdayValidator
Karat120 fbd57c5
Fix FilmController
Karat120 536e963
Refactor FilmTest
Karat120 c636610
Fix FilmControllerTest
Karat120 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
23 changes: 23 additions & 0 deletions
23
src/main/java/ru/yandex/practicum/filmorate/annotation/NotBeforeCinemaBirthday.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.annotation; | ||
|
|
||
| import jakarta.validation.Constraint; | ||
| import jakarta.validation.Payload; | ||
| import ru.yandex.practicum.filmorate.util.NotBeforeCinemaBirthdayValidator; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Documented | ||
| @Constraint(validatedBy = NotBeforeCinemaBirthdayValidator.class) | ||
| @Target({ElementType.FIELD, ElementType.PARAMETER}) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface NotBeforeCinemaBirthday { | ||
| String message() default "The date cannot be earlier than the cinema's birthday — December 28, 1895"; | ||
|
|
||
| Class<?>[] groups() default {}; | ||
|
|
||
| Class<? extends Payload>[] payload() default {}; | ||
| } |
44 changes: 44 additions & 0 deletions
44
src/main/java/ru/yandex/practicum/filmorate/controller/FilmController.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,7 +1,51 @@ | ||
| package ru.yandex.practicum.filmorate.controller; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.PutMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import ru.yandex.practicum.filmorate.exception.FilmNotFoundException; | ||
| import ru.yandex.practicum.filmorate.model.Film; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| @Slf4j | ||
| @RestController | ||
| @RequestMapping("/films") | ||
| public class FilmController { | ||
| private final Map<Long, Film> films = new HashMap<>(); | ||
| private long lastGeneratedID = 0; | ||
|
|
||
| @PostMapping | ||
| public Film createFilm(@Valid @RequestBody Film film) { | ||
| film.setId(++lastGeneratedID); | ||
| films.put(film.getId(), film); | ||
|
|
||
| log.info("Film created: id={}, name={}", film.getId(), film.getName()); | ||
| return film; | ||
| } | ||
|
|
||
| @GetMapping | ||
| public List<Film> getAllFilms() { | ||
| log.debug("Retrieving all films (total={})", films.size()); | ||
| return new ArrayList<>(films.values()); | ||
| } | ||
|
|
||
| @PutMapping | ||
| public Film updateFilm(@Valid @RequestBody Film film) { | ||
| if (!films.containsKey(film.getId())) { | ||
| log.warn("Attempt to update non-existent film id={}", film.getId()); | ||
| throw new FilmNotFoundException(); | ||
| } | ||
| films.put(film.getId(), film); | ||
| log.info("Film updated: id={}, name={}", film.getId(), film.getName()); | ||
| return film; | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
src/main/java/ru/yandex/practicum/filmorate/controller/UserController.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,51 @@ | ||
| package ru.yandex.practicum.filmorate.controller; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.PutMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import ru.yandex.practicum.filmorate.exception.UserNotFoundException; | ||
| import ru.yandex.practicum.filmorate.model.User; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| @Slf4j | ||
| @RestController | ||
| @RequestMapping("/users") | ||
| public class UserController { | ||
| private final Map<Long, User> users = new HashMap<>(); | ||
| private long lastGeneratedID = 0; | ||
|
|
||
| @PostMapping | ||
| public User createUser(@Valid @RequestBody User user) { | ||
| user.setId(++lastGeneratedID); | ||
| users.put(user.getId(), user); | ||
|
|
||
| log.info("User created: id={}, email={}, login={}", user.getId(), user.getEmail(), user.getLogin()); | ||
| return user; | ||
| } | ||
|
|
||
| @GetMapping | ||
| public List<User> getAllUsers() { | ||
| log.debug("Retrieving all users (total={})", users.size()); | ||
| return new ArrayList<>(users.values()); | ||
| } | ||
|
|
||
| @PutMapping | ||
| public User updateUser(@Valid @RequestBody User user) { | ||
| if (!users.containsKey(user.getId())) { | ||
| log.warn("Attempt to update non-existent user id={}", user.getId()); | ||
| throw new UserNotFoundException(); | ||
| } | ||
| users.put(user.getId(), user); | ||
| log.info("User updated: id={}, email={}, login={}", user.getId(), user.getEmail(), user.getLogin()); | ||
| return user; | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/ru/yandex/practicum/filmorate/exception/FilmNotFoundException.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.exception; | ||
|
|
||
| public class FilmNotFoundException extends RuntimeException { | ||
| public FilmNotFoundException() { | ||
| super("Film not found"); | ||
| } | ||
|
|
||
| public FilmNotFoundException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public FilmNotFoundException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
src/main/java/ru/yandex/practicum/filmorate/exception/GlobalExceptionHandler.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,48 @@ | ||
| package ru.yandex.practicum.filmorate.exception; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.ControllerAdvice; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| @Slf4j | ||
| @ControllerAdvice | ||
| public class GlobalExceptionHandler { | ||
|
|
||
| @ExceptionHandler(ValidationException.class) | ||
| public ResponseEntity<Map<String, String>> handleValidationException(ValidationException ex) { | ||
| log.error("Validation error: {}", ex.getMessage()); | ||
| Map<String, String> body = new HashMap<>(); | ||
| body.put("error", ex.getMessage()); | ||
| return new ResponseEntity<>(body, HttpStatus.BAD_REQUEST); | ||
| } | ||
|
|
||
| @ExceptionHandler(UserNotFoundException.class) | ||
| public ResponseEntity<Map<String, String>> handleUserNotFoundException(UserNotFoundException ex) { | ||
| log.warn("User not found: {}", ex.getMessage()); | ||
| Map<String, String> body = new HashMap<>(); | ||
| body.put("error", ex.getMessage() != null ? ex.getMessage() : "User not found"); | ||
| return new ResponseEntity<>(body, HttpStatus.NOT_FOUND); | ||
| } | ||
|
|
||
| @ExceptionHandler(FilmNotFoundException.class) | ||
| public ResponseEntity<Map<String, String>> handleFilmNotFoundException(FilmNotFoundException ex) { | ||
| log.warn("Film not found: {}", ex.getMessage()); | ||
| Map<String, String> body = new HashMap<>(); | ||
| body.put("error", ex.getMessage() != null ? ex.getMessage() : "Film not found"); | ||
| return new ResponseEntity<>(body, HttpStatus.NOT_FOUND); | ||
| } | ||
|
|
||
| // Для любых неожиданных ошибок | ||
| @ExceptionHandler(Exception.class) | ||
| public ResponseEntity<Map<String, String>> handleOtherExceptions(Exception ex) { | ||
| log.error("Unexpected error", ex); | ||
| Map<String, String> body = new HashMap<>(); | ||
| body.put("error", "Unexpected error: " + ex.getMessage()); | ||
| return new ResponseEntity<>(body, HttpStatus.INTERNAL_SERVER_ERROR); | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/ru/yandex/practicum/filmorate/exception/UserNotFoundException.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.exception; | ||
|
|
||
| public class UserNotFoundException extends RuntimeException { | ||
| public UserNotFoundException() { | ||
| super("User not found"); | ||
| } | ||
|
|
||
| public UserNotFoundException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public UserNotFoundException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/ru/yandex/practicum/filmorate/exception/ValidationException.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,11 @@ | ||
| package ru.yandex.practicum.filmorate.exception; | ||
|
|
||
| public class ValidationException extends RuntimeException { | ||
| public ValidationException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public ValidationException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
44 changes: 36 additions & 8 deletions
44
src/main/java/ru/yandex/practicum/filmorate/model/Film.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,12 +1,40 @@ | ||
| package ru.yandex.practicum.filmorate.model; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| /** | ||
| * Film. | ||
| */ | ||
| @Getter | ||
| @Setter | ||
| import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
| import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
| import jakarta.validation.ValidationException; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.Size; | ||
| import lombok.Data; | ||
| import ru.yandex.practicum.filmorate.annotation.NotBeforeCinemaBirthday; | ||
| import ru.yandex.practicum.filmorate.util.DurationMinutesDeserializer; | ||
| import ru.yandex.practicum.filmorate.util.DurationMinutesSerializer; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.LocalDate; | ||
|
|
||
| @Data | ||
| public class Film { | ||
|
|
||
| private Long id; | ||
|
|
||
| @NotBlank(message = "The film name cannot be empty") | ||
| private String name; | ||
|
|
||
| @Size(max = 200, message = "The film description must not exceed 200 characters") | ||
| private String description; | ||
|
|
||
| @NotBeforeCinemaBirthday | ||
| private LocalDate releaseDate; | ||
|
|
||
| @JsonSerialize(using = DurationMinutesSerializer.class) | ||
| @JsonDeserialize(using = DurationMinutesDeserializer.class) | ||
| private Duration duration; | ||
|
|
||
| public void setDuration(Duration duration) { | ||
| if (duration == null || duration.isNegative() || duration.isZero()) { | ||
| throw new ValidationException("Film duration must be positive"); | ||
| } | ||
| this.duration = duration; | ||
| } | ||
| } | ||
35 changes: 35 additions & 0 deletions
35
src/main/java/ru/yandex/practicum/filmorate/model/User.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,35 @@ | ||
| package ru.yandex.practicum.filmorate.model; | ||
|
|
||
| import jakarta.validation.constraints.Email; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.PastOrPresent; | ||
| import jakarta.validation.constraints.Pattern; | ||
| import lombok.Data; | ||
|
|
||
| import java.time.LocalDate; | ||
|
|
||
| @Data | ||
| public class User { | ||
|
|
||
| private Long id; | ||
|
|
||
| @NotBlank(message = "Email cannot be empty") | ||
| @Email(message = "Email must contain '@' and be valid") | ||
| private String email; | ||
|
|
||
| @NotBlank(message = "Login cannot be empty") | ||
| @Pattern(regexp = "\\S+", message = "Login must not contain spaces") | ||
| private String login; | ||
|
|
||
| private String name; | ||
|
|
||
| @PastOrPresent(message = "Birthday cannot be in the future") | ||
| private LocalDate birthday; | ||
|
|
||
| public void setLogin(String login) { | ||
| this.login = login; | ||
| if (this.name == null || this.name.isBlank()) { | ||
| this.name = login; | ||
| } | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
src/main/java/ru/yandex/practicum/filmorate/util/DurationMinutesDeserializer.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,21 @@ | ||
| package ru.yandex.practicum.filmorate.util; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonParser; | ||
| import com.fasterxml.jackson.databind.DeserializationContext; | ||
| import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
|
|
||
| import java.io.IOException; | ||
| import java.time.Duration; | ||
|
|
||
| public class DurationMinutesDeserializer extends StdDeserializer<Duration> { | ||
|
|
||
| public DurationMinutesDeserializer() { | ||
| super(Duration.class); | ||
| } | ||
|
|
||
| @Override | ||
| public Duration deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
| long minutes = p.getLongValue(); | ||
| return Duration.ofMinutes(minutes); | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/main/java/ru/yandex/practicum/filmorate/util/DurationMinutesSerializer.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,19 @@ | ||
| package ru.yandex.practicum.filmorate.util; | ||
| import com.fasterxml.jackson.core.JsonGenerator; | ||
| import com.fasterxml.jackson.databind.SerializerProvider; | ||
| import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
|
|
||
| import java.io.IOException; | ||
| import java.time.Duration; | ||
|
|
||
| public class DurationMinutesSerializer extends StdSerializer<Duration> { | ||
|
|
||
| public DurationMinutesSerializer() { | ||
| super(Duration.class); | ||
| } | ||
|
|
||
| @Override | ||
| public void serialize(Duration duration, JsonGenerator gen, SerializerProvider provider) throws IOException { | ||
| gen.writeNumber(duration.toMinutes()); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Лучше создать свою собственную аннотацию для условия, что дата релиза должна быть не раньше 28 декабря 1895 года. Если не получится, то ничего страшного приму так, если получится, то будет супер)