Skip to content
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

int #98

Closed
wants to merge 1 commit into from
Closed

int #98

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<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 @@ -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<Integer, Film> films = new HashMap<>();
private int filmId = 1;

private int generatorId() {
return filmId++;
}

@GetMapping
public List<Film> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Integer, User> users = new HashMap<>();
private int userId = 1;

private int generatorId() {
return userId++;
}

@GetMapping
public List<User> 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;
}
}
57 changes: 57 additions & 0 deletions src/main/java/ru/yandex/practicum/filmorate/model/Film.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
66 changes: 66 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,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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.yandex.practicum.filmorate.validation;

public class ValidationException extends RuntimeException {
public ValidationException(String message) {
super(message);
}
}
Loading