-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nikita
committed
Feb 21, 2024
1 parent
fe5308a
commit 4b30e9c
Showing
19 changed files
with
768 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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
12 changes: 12 additions & 0 deletions
12
src/main/java/ru/yandex/practicum/filmorate/constant/Constant.java
This file contains 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,12 @@ | ||
package ru.yandex.practicum.filmorate.constant; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class Constant { | ||
public static final String SIZE_OF_FILMS = "10"; | ||
public static final String REGEX_LOGIN = "^\\S*$"; | ||
public static final String REGEX_EMAIL = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"; | ||
public static final LocalDate REGEX_DATE = LocalDate.of(1895, 12, 28); | ||
public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd"); | ||
} |
49 changes: 48 additions & 1 deletion
49
src/main/java/ru/yandex/practicum/filmorate/controller/FilmController.java
This file contains 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,54 @@ | ||
package ru.yandex.practicum.filmorate.controller; | ||
|
||
import org.springframework.web.bind.annotation.RestController; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
import ru.yandex.practicum.filmorate.model.Film; | ||
import ru.yandex.practicum.filmorate.service.FilmService; | ||
|
||
import javax.validation.Valid; | ||
import java.util.List; | ||
|
||
import static ru.yandex.practicum.filmorate.constant.Constant.SIZE_OF_FILMS; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/films") | ||
public class FilmController { | ||
|
||
private final FilmService filmService; | ||
|
||
@PutMapping("/{id}/like/{userId}") | ||
public Film addLike(@PathVariable int id, @PathVariable int userId) { | ||
return filmService.addLike(id, userId); | ||
} | ||
|
||
@DeleteMapping("/{id}/like/{userId}") | ||
public Film deleteLike(@PathVariable int id, @PathVariable int userId) { | ||
return filmService.deleteLike(id, userId); | ||
} | ||
|
||
@GetMapping("/popular") | ||
public List<Film> getTopTenOfFilms(@RequestParam(defaultValue = SIZE_OF_FILMS) String count) { | ||
return filmService.getTopTenOfFilms(count); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public Film getFilm(@PathVariable int id) { | ||
return filmService.getFilm(id); | ||
} | ||
|
||
@PostMapping | ||
public Film createFilm(@Valid @RequestBody Film film) { | ||
return filmService.createFilm(film); | ||
} | ||
|
||
@PutMapping | ||
public Film updateFilm(@Valid @RequestBody Film film) { | ||
return filmService.updateFilm(film); | ||
} | ||
|
||
@GetMapping | ||
public List<Film> getALlFilms() { | ||
return filmService.getALlFilms(); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/ru/yandex/practicum/filmorate/controller/UserController.java
This file contains 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,57 @@ | ||
package ru.yandex.practicum.filmorate.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
import ru.yandex.practicum.filmorate.model.User; | ||
import ru.yandex.practicum.filmorate.service.UserService; | ||
|
||
import javax.validation.Valid; | ||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/users") | ||
public class UserController { | ||
|
||
private final UserService userService; | ||
|
||
@PutMapping("/{id}/friends/{friendId}") | ||
public User addFriend(@PathVariable int id, @PathVariable int friendId) { | ||
return userService.addFriend(id, friendId); | ||
} | ||
|
||
@DeleteMapping("/{id}/friends/{friendId}") | ||
public User deleteFriend(@PathVariable int id, @PathVariable int friendId) { | ||
return userService.deleteFriend(id, friendId); | ||
} | ||
|
||
@GetMapping("/{id}/friends/common/{otherId}") | ||
public List<User> getCommonFriends(@PathVariable int id, @PathVariable int otherId) { | ||
return userService.getCommonFriends(id, otherId); | ||
} | ||
|
||
@GetMapping("/{id}/friends") | ||
public List<User> getFriends(@PathVariable int id) { | ||
return userService.getFriends(id); | ||
} | ||
|
||
@PostMapping | ||
public User createUser(@Valid @RequestBody User user) { | ||
return userService.createUser(user); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public User getUser(@PathVariable int id) { | ||
return userService.getUser(id); | ||
} | ||
|
||
@PutMapping | ||
public User updateUser(@Valid @RequestBody User user) { | ||
return userService.updateUser(user); | ||
} | ||
|
||
@GetMapping | ||
public List<User> getALlUsers() { | ||
return userService.getALlUsers(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/ru/yandex/practicum/filmorate/exception/HandlerException.java
This file contains 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; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@RestControllerAdvice | ||
public class HandlerException { | ||
@ExceptionHandler(ValidException.class) | ||
public ResponseEntity<ResponseError> notFound(ValidException e) { | ||
return new ResponseEntity<>(ResponseError.builder() | ||
.message(e.getMessage()) | ||
.build(), e.getStatus()); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/ru/yandex/practicum/filmorate/exception/ResponseError.java
This file contains 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,10 @@ | ||
package ru.yandex.practicum.filmorate.exception; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Builder | ||
@Data | ||
public class ResponseError { | ||
private String message; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/ru/yandex/practicum/filmorate/exception/ValidException.java
This file contains 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,14 @@ | ||
package ru.yandex.practicum.filmorate.exception; | ||
|
||
import lombok.*; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
@ToString | ||
@Setter | ||
@Builder | ||
public class ValidException extends RuntimeException { | ||
private String message; | ||
private HttpStatus status; | ||
} |
27 changes: 20 additions & 7 deletions
27
src/main/java/ru/yandex/practicum/filmorate/model/Film.java
This file contains 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,25 @@ | ||
package ru.yandex.practicum.filmorate.model; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
/** | ||
* Film. | ||
*/ | ||
@Getter | ||
@Setter | ||
import javax.validation.constraints.NotEmpty; | ||
import javax.validation.constraints.Positive; | ||
import javax.validation.constraints.Size; | ||
import java.time.LocalDate; | ||
import java.util.Set; | ||
|
||
@Data | ||
@Builder | ||
public class Film { | ||
private Integer id; | ||
@NotEmpty(message = "Имя не может быть пустым") | ||
@Size(max = 255, min = 1, message = "Максимальная длина имени - 255 символов") | ||
private String name; | ||
@Size(max = 200, min = 1, message = "Максимальная длина описания - 200 символов") | ||
private String description; | ||
private LocalDate releaseDate; | ||
@Positive(message = "Продолжительность не может быть отрицательной") | ||
private Integer duration; | ||
private Set<Integer> likes; | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/ru/yandex/practicum/filmorate/model/User.java
This file contains 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.model; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
import javax.validation.constraints.Email; | ||
import javax.validation.constraints.NotEmpty; | ||
import javax.validation.constraints.Past; | ||
import javax.validation.constraints.Pattern; | ||
import java.time.LocalDate; | ||
import java.util.Set; | ||
|
||
import static ru.yandex.practicum.filmorate.constant.Constant.REGEX_EMAIL; | ||
import static ru.yandex.practicum.filmorate.constant.Constant.REGEX_LOGIN; | ||
|
||
@Data | ||
@Builder | ||
public class User { | ||
private Integer id; | ||
@NotEmpty(message = "Почта не может быть пустая") | ||
@Email(regexp = REGEX_EMAIL, message = "В 'email' использованы запрещённые символы") | ||
private String email; | ||
@NotEmpty(message = "Логин не может быть пустым") | ||
@Pattern(regexp = REGEX_LOGIN, message = "В 'login' использованы запрещённые символы") | ||
private String login; | ||
private String name; | ||
@Past(message = "День рождения не может быть указан в будущем") | ||
private LocalDate birthday; | ||
private Set<Integer> friends; | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/ru/yandex/practicum/filmorate/service/FilmService.java
This file contains 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,47 @@ | ||
package ru.yandex.practicum.filmorate.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import ru.yandex.practicum.filmorate.model.Film; | ||
import ru.yandex.practicum.filmorate.storage.film.FilmStorage; | ||
|
||
import java.util.List; | ||
|
||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class FilmService { | ||
|
||
private final FilmStorage filmStorage; | ||
|
||
public Film getFilm(int id) { | ||
return filmStorage.getFilm(id); | ||
} | ||
|
||
public Film addLike(int id, int userId) { | ||
return filmStorage.addLike(id, userId); | ||
} | ||
|
||
public Film deleteLike(int id, int userId) { | ||
return filmStorage.deleteLike(id, userId); | ||
} | ||
|
||
public List<Film> getTopTenOfFilms(String count) { | ||
int count1 = Integer.parseInt(count); | ||
return filmStorage.getTopTenOfFilms(count1); | ||
} | ||
|
||
public Film createFilm(Film film) { | ||
return filmStorage.createFilm(film); | ||
} | ||
|
||
|
||
public Film updateFilm(Film film) { | ||
return filmStorage.updateFilm(film); | ||
} | ||
|
||
|
||
public List<Film> getALlFilms() { | ||
return filmStorage.getALlFilms(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/ru/yandex/practicum/filmorate/service/UserService.java
This file contains 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,47 @@ | ||
package ru.yandex.practicum.filmorate.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import ru.yandex.practicum.filmorate.model.User; | ||
import ru.yandex.practicum.filmorate.storage.user.UserStorage; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class UserService { | ||
|
||
private final UserStorage userStorage; | ||
|
||
public User addFriend(int id, int friendId) { | ||
return userStorage.addFriend(id, friendId); | ||
} | ||
|
||
public User deleteFriend(int id, int friendId) { | ||
return userStorage.deleteFriend(id, friendId); | ||
} | ||
|
||
public List<User> getCommonFriends(int id, int otherId) { | ||
return userStorage.getCommonFriends(id, otherId); | ||
} | ||
|
||
public List<User> getFriends(int id) { | ||
return userStorage.getFriends(id); | ||
} | ||
|
||
public User createUser(User user) { | ||
return userStorage.createUser(user); | ||
} | ||
|
||
public User updateUser(User user) { | ||
return userStorage.updateUser(user); | ||
} | ||
|
||
public User getUser(int id) { | ||
return userStorage.getUser(id); | ||
} | ||
|
||
public List<User> getALlUsers() { | ||
return userStorage.getALlUsers(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/ru/yandex/practicum/filmorate/storage/film/FilmStorage.java
This file contains 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,22 @@ | ||
package ru.yandex.practicum.filmorate.storage.film; | ||
|
||
import ru.yandex.practicum.filmorate.model.Film; | ||
|
||
import java.util.List; | ||
|
||
public interface FilmStorage { | ||
|
||
Film getFilm(int id); | ||
|
||
List<Film> getTopTenOfFilms(int count); | ||
|
||
Film addLike(int id, int userId); | ||
|
||
Film deleteLike(int id, int userId); | ||
|
||
Film createFilm(Film film); | ||
|
||
Film updateFilm(Film film); | ||
|
||
List<Film> getALlFilms(); | ||
} |
Oops, something went wrong.