Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita committed Feb 21, 2024
1 parent fe5308a commit 4b30e9c
Show file tree
Hide file tree
Showing 19 changed files with 768 additions and 15 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

@SpringBootApplication
public class FilmorateApplication {
public static void main(String[] args) {
SpringApplication.run(FilmorateApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(FilmorateApplication.class, args);
}

}
12 changes: 12 additions & 0 deletions src/main/java/ru/yandex/practicum/filmorate/constant/Constant.java
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");
}
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();
}
}
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();
}
}
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());
}
}
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;
}
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 src/main/java/ru/yandex/practicum/filmorate/model/Film.java
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 src/main/java/ru/yandex/practicum/filmorate/model/User.java
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;
}
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();
}
}
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();
}
}
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();
}
Loading

0 comments on commit 4b30e9c

Please sign in to comment.