Skip to content

Commit

Permalink
Финальный проект спринта 11 (вар2)
Browse files Browse the repository at this point in the history
  • Loading branch information
AidarJava committed Sep 17, 2024
1 parent 7f6c640 commit 33b4eb2
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.Collection;

@RestController
//@RequiredArgsConstructor
@RequestMapping("/films")
public class FilmController {
private final FilmService filmService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@RequestMapping("/users")
public class UserController {

private UserService userService;
private final UserService userService;

UserController(UserService userService) {
this.userService = userService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.yandex.practicum.filmorate.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import ru.yandex.practicum.filmorate.ecxeption.NotFoundException;
import ru.yandex.practicum.filmorate.model.Film;
Expand All @@ -9,14 +10,10 @@
import java.util.*;

@Service
@RequiredArgsConstructor
public class FilmService {
FilmStorage filmStorage;
UserStorage userStorage;

public FilmService(FilmStorage filmStorage, UserStorage userStorage) {
this.filmStorage = filmStorage;
this.userStorage = userStorage;
}
private final FilmStorage filmStorage;
private final UserStorage userStorage;

public Film getFilmById(Long id) {
if (filmStorage.getFilmById(id) == null) {
Expand Down Expand Up @@ -74,6 +71,6 @@ public Film createFilm(Film film) {
}

public Film updateFilm(Film film) {
return filmStorage.ubdateFilm(film);
return filmStorage.updateFilm(film);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.yandex.practicum.filmorate.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import ru.yandex.practicum.filmorate.ecxeption.NotFoundException;
import ru.yandex.practicum.filmorate.model.User;
Expand All @@ -9,13 +10,10 @@
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class UserService {

UserStorage userStorage;

public UserService(UserStorage userStorage) {
this.userStorage = userStorage;
}
private final UserStorage userStorage;

public User getUserById(Long id) {
if (userStorage.getUserById(id) == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ public interface FilmStorage {

Film createFilm(Film film);

Film ubdateFilm(Film film);
Film updateFilm(Film film);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import ru.yandex.practicum.filmorate.ecxeption.NotFoundException;
import ru.yandex.practicum.filmorate.ecxeption.OccurredException;
import ru.yandex.practicum.filmorate.ecxeption.ValidationException;
import ru.yandex.practicum.filmorate.model.Film;

Expand All @@ -30,7 +29,6 @@ public Collection<Film> getAllFilms() {

@Override
public Film createFilm(Film film) {
validateFilm(film);
if (film.getReleaseDate().isBefore(LocalDate.of(1895, 12, 28))) {
throw new ValidationException("Дата релиза — не раньше 28 декабря 1895 года!");
}
Expand All @@ -42,20 +40,16 @@ public Film createFilm(Film film) {
}

@Override
public Film ubdateFilm(Film film) {
validateFilm(film);
public Film updateFilm(Film film) {
// validateFilm(film);
if (film.getId() == null) {
throw new ValidationException("Id должен быть указан!");
}
validateFilm(film);
if (films.containsKey(film.getId())) {
if (film.getReleaseDate().isBefore(LocalDate.of(1895, 12, 28))) {
throw new ValidationException("Дата релиза — не раньше 28 декабря 1895 года!");
}
film.setDescription(film.getDescription());
film.setName(film.getName());
film.setReleaseDate(film.getReleaseDate());
film.setDuration(film.getDuration());
films.put(film.getId(), film);
return film;
} else throw new NotFoundException("Такого фильма нет в списке!");
}
Expand All @@ -68,16 +62,4 @@ private Long getNextId() {
.orElse(0);
return ++currentMaxId;
}

private void validateFilm(Film film) {
if (film.getName() == null || film.getName().isBlank()) {
throw new OccurredException("Название не может быть пустым!");
}
if (film.getDescription().length() > 200) {
throw new OccurredException("Максимальная длина описания — 200 символов!");
}
if (film.getDuration() <= 0) {
throw new OccurredException("Продолжительность фильма должна быть положительным числом!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import ru.yandex.practicum.filmorate.ecxeption.NotFoundException;
import ru.yandex.practicum.filmorate.ecxeption.OccurredException;
import ru.yandex.practicum.filmorate.ecxeption.ValidationException;
import ru.yandex.practicum.filmorate.model.User;

import java.time.LocalDate;

import java.util.*;

@Slf4j
Expand All @@ -29,7 +26,6 @@ public User getUserById(Long id) {

@Override
public User createUser(User user) {
validateUser(user);
user.setId(getNextId());
log.debug("Валидация пройдена.");
if (user.getName() == null) {
Expand All @@ -44,7 +40,6 @@ public User updateUser(User user) {
if (user.getId() == null) {
throw new ValidationException("Id должен быть указан!");
}
validateUser(user);
if (users.containsKey(user.getId())) {
user.setBirthday(user.getBirthday());
user.setLogin(user.getLogin());
Expand All @@ -67,16 +62,4 @@ private Long getNextId() {
.orElse(0);
return ++currentMaxId;
}

private void validateUser(User user) {
if (user.getEmail() == null || user.getEmail().isBlank() || !user.getEmail().contains(String.valueOf('@'))) {
throw new OccurredException("Электронная почта не может быть пустой и должна содержать символ @!");
}
if (user.getLogin().isBlank() || user.getLogin().contains(String.valueOf(' '))) {
throw new OccurredException("Логин не может быть пустым и содержать пробелы!");
}
if (user.getBirthday().isAfter(LocalDate.now())) {
throw new OccurredException("Дата рождения не может быть в будущем!");
}
}
}

0 comments on commit 33b4eb2

Please sign in to comment.