Skip to content

Commit

Permalink
Создал Filmorate по ТЗ, проверил через postman, ошибок нет.
Browse files Browse the repository at this point in the history
  • Loading branch information
DumplingM2 committed Jan 11, 2025
1 parent c9016a0 commit 4a8b8f8
Show file tree
Hide file tree
Showing 10 changed files with 408 additions and 56 deletions.
120 changes: 77 additions & 43 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,46 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ru.yandex.practicum</groupId>
<artifactId>filmorate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>filmorate</name>
<description>filmorate</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.2</version>
<relativePath/>
</parent>

<groupId>ru.yandex.practicum</groupId>
<artifactId>filmorate</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<java.version>21</java.version>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-resources-plugin.version>3.3.1</maven-resources-plugin.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven-resources-plugin.version}</version>
</plugin>
</plugins>
</build>
</project>
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,75 @@
package ru.yandex.practicum.filmorate.controller;

import org.springframework.web.bind.annotation.RestController;
import ru.yandex.practicum.filmorate.exceptions.ValidationException;
import ru.yandex.practicum.filmorate.model.Film;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/films")
public class FilmController {
}

private final Map<Long, Film> films = new HashMap<>();

@GetMapping
public Collection<Film> findAll() {
return films.values();
}

@PostMapping
public Film create(@RequestBody Film film) {
validateFilm(film);
film.setId(getNextId());
films.put(film.getId(), film);
log.info("Film created: {}", film);
return film;
}

@PutMapping
public Film update(@RequestBody Film film) {
if (film.getId() == null) {
throw new ValidationException("Id должен быть указан");
}
if (films.containsKey(film.getId())) {
validateFilm(film);
films.put(film.getId(), film);
log.info("Film updated: {}", film);
return film;
}
throw new ValidationException("Фильм с id = " + film.getId() + " не найден");
}

private long getNextId() {
long currentMaxId = films.keySet()
.stream()
.mapToLong(id -> id)
.max()
.orElse(0);
return ++currentMaxId;
}

void validateFilm(Film film) {
if (film.getName() == null || film.getName().isBlank()) {
log.error("Validation failed: Film name is null or blank");
throw new ValidationException("Название не может быть пустым");
}
if (film.getDescription() != null && film.getDescription().length() > 200) {
log.error("Validation failed: Film description is too long");
throw new ValidationException("Максимальная длина описания — 200 символов");
}
if (film.getReleaseDate() != null && film.getReleaseDate().isBefore(LocalDate.of(1895, 12, 28))) {
log.error("Validation failed: Film release date is too early");
throw new ValidationException("Дата релиза — не раньше 28 декабря 1895 года");
}
if (film.getDuration() <= 0) {
log.error("Validation failed: Film duration is not positive");
throw new ValidationException("Продолжительность фильма должна быть положительным числом");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ru.yandex.practicum.filmorate.controller;

import ru.yandex.practicum.filmorate.exceptions.ValidationException;
import ru.yandex.practicum.filmorate.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {

private final Map<Long, User> users = new HashMap<>();

@GetMapping
public Collection<User> findAll() {
return users.values();
}

@PostMapping
public User create(@RequestBody User user) {
validateUser(user);
user.setId(getNextId());
users.put(user.getId(), user);
log.info("User created: {}", user);
return user;
}

@PutMapping
public User update(@RequestBody User user) {
if (user.getId() == null) {
throw new ValidationException("Id должен быть указан");
}
if (users.containsKey(user.getId())) {
validateUser(user);
users.put(user.getId(), user);
log.info("User updated: {}", user);
return user;
}
throw new ValidationException("Пользователь с id = " + user.getId() + " не найден");
}

private long getNextId() {
long currentMaxId = users.keySet()
.stream()
.mapToLong(id -> id)
.max()
.orElse(0);
return ++currentMaxId;
}

void validateUser(User user) {
if (user.getEmail() == null || !user.getEmail().contains("@")) {
throw new ValidationException("Электронная почта не может быть пустой и должна содержать символ @");
}
if (user.getLogin() == null || user.getLogin().isBlank() || user.getLogin().contains(" ")) {
throw new ValidationException("Логин не может быть пустым и содержать пробелы");
}
if (user.getName() == null || user.getName().isBlank()) {
user.setName(user.getLogin());
}
if (user.getBirthday() != null && user.getBirthday().isAfter(LocalDate.now())) {
throw new ValidationException("Дата рождения не может быть в будущем");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.yandex.practicum.filmorate.exceptions;

public class ValidationException extends RuntimeException {
public ValidationException(String message) {
super(message);
}
}
20 changes: 12 additions & 8 deletions src/main/java/ru/yandex/practicum/filmorate/model/Film.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package ru.yandex.practicum.filmorate.model;

import lombok.Getter;
import lombok.Setter;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
* Film.
*/
@Getter
@Setter
import java.time.LocalDate;

@Data
@EqualsAndHashCode(of = "id")
public class Film {
}
private Long id;
private String name;
private String description;
private LocalDate releaseDate;
private int duration;
}
16 changes: 16 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,16 @@
package ru.yandex.practicum.filmorate.model;

import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDate;

@Data
@EqualsAndHashCode(of = "id")
public class User {
private Long id;
private String email;
private String login;
private String name;
private LocalDate birthday;
}
5 changes: 5 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
server.port=8080

logging.level.root=INFO
logging.level.com.example.filmorate=DEBUG

spring.application.name=Filmorate
Loading

0 comments on commit 4a8b8f8

Please sign in to comment.