Skip to content

Commit 8de7a94

Browse files
authored
Merge pull request #1 from LevshaKS/controllers-films-users
ТЗ 10 исправлен, перенесен в другой проект
2 parents 95bf5e5 + 2b0e33f commit 8de7a94

16 files changed

Lines changed: 784 additions & 53 deletions

pom.xml

Lines changed: 91 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,95 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
5-
<parent>
6-
<groupId>org.springframework.boot</groupId>
7-
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>3.2.4</version>
9-
<relativePath/> <!-- lookup parent from repository -->
10-
</parent>
11-
<groupId>ru.yandex.practicum</groupId>
12-
<artifactId>filmorate</artifactId>
13-
<version>0.0.1-SNAPSHOT</version>
14-
<name>filmorate</name>
15-
<description>filmorate</description>
16-
<properties>
17-
<java.version>21</java.version>
18-
</properties>
19-
<dependencies>
20-
<dependency>
21-
<groupId>org.springframework.boot</groupId>
22-
<artifactId>spring-boot-starter-web</artifactId>
23-
</dependency>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>3.3.3</version>
10+
<relativePath/>
11+
</parent>
12+
<groupId>ru.yandex.practicum</groupId>
13+
<artifactId>filmorate</artifactId>
14+
<version>1.0-SNAPSHOT</version>
2415

25-
<dependency>
26-
<groupId>org.projectlombok</groupId>
27-
<artifactId>lombok</artifactId>
28-
<scope>provided</scope>
29-
</dependency>
30-
<dependency>
31-
<groupId>org.springframework.boot</groupId>
32-
<artifactId>spring-boot-starter-test</artifactId>
33-
<scope>test</scope>
34-
</dependency>
35-
</dependencies>
16+
<properties>
17+
<maven.compiler.source>21</maven.compiler.source>
18+
<maven.compiler.target>21</maven.compiler.target>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
</properties>
3621

37-
<build>
38-
<plugins>
39-
<plugin>
40-
<groupId>org.springframework.boot</groupId>
41-
<artifactId>spring-boot-maven-plugin</artifactId>
42-
</plugin>
43-
</plugins>
44-
</build>
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-web</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.projectlombok</groupId>
29+
<artifactId>lombok</artifactId>
30+
<version>1.18.34</version>
31+
<scope>provided</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.google.code.gson</groupId>
35+
<artifactId>gson</artifactId>
36+
<version>2.10.1</version>
37+
</dependency>
4538

46-
</project>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-validation</artifactId>
42+
<version>3.3.4</version>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>org.slf4j</groupId>
47+
<artifactId>slf4j-api</artifactId>
48+
<version>2.0.16</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.junit.jupiter</groupId>
52+
<artifactId>junit-jupiter</artifactId>
53+
<version>5.11.1</version>
54+
<scope>test</scope>
55+
</dependency>
56+
57+
<dependency>
58+
<groupId>org.assertj</groupId>
59+
<artifactId>assertj-core</artifactId>
60+
<version>3.26.3</version>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-test</artifactId>
66+
<version>3.3.3</version>
67+
<scope>test</scope>
68+
</dependency>
69+
</dependencies>
70+
71+
<build>
72+
<plugins>
73+
<plugin>
74+
<groupId>org.apache.maven.plugins</groupId>
75+
<artifactId>maven-resources-plugin</artifactId>
76+
<version>3.3.1</version>
77+
</plugin>
78+
<plugin>
79+
<groupId>org.apache.maven.plugins</groupId>
80+
<artifactId>maven-compiler-plugin</artifactId>
81+
<version>3.13.0</version>
82+
</plugin>
83+
<plugin>
84+
<groupId>org.apache.maven.plugins</groupId>
85+
<artifactId>maven-checkstyle-plugin</artifactId>
86+
<version>3.5.0</version>
87+
</plugin>
88+
<plugin>
89+
<groupId>org.springframework.boot</groupId>
90+
<artifactId>spring-boot-maven-plugin</artifactId>
91+
</plugin>
92+
</plugins>
93+
</build>
94+
95+
</project>
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package ru.yandex.practicum.filmorate;
22

3+
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
56

67
@SpringBootApplication
78
public class FilmorateApplication {
8-
public static void main(String[] args) {
9-
SpringApplication.run(FilmorateApplication.class, args);
10-
}
11-
9+
public static void main(String[] args) {
10+
SpringApplication.run(FilmorateApplication.class, args);
11+
}
1212
}
Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,72 @@
11
package ru.yandex.practicum.filmorate.controller;
22

3-
import org.springframework.web.bind.annotation.RestController;
3+
import jakarta.validation.Valid;
4+
import jakarta.validation.ValidationException;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.web.bind.annotation.*;
10+
import ru.yandex.practicum.filmorate.model.Film;
11+
12+
import java.util.Collection;
13+
import java.util.HashMap;
14+
import java.util.Map;
415

516
@RestController
17+
@RequestMapping("/films")
618
public class FilmController {
19+
20+
private final ValidateController validateController = new ValidateController();
21+
private final Map<Long, Film> films = new HashMap<>();
22+
private final Logger log = LoggerFactory.getLogger(FilmController.class);
23+
24+
@GetMapping
25+
public ResponseEntity<Collection<Film>> filmAll() {
26+
log.info("вывод списка фильмов");
27+
return ResponseEntity.ok(films.values());
28+
}
29+
30+
@PostMapping
31+
public ResponseEntity<?> create(@Valid @RequestBody Film film) {
32+
validateController.validateFilm(film);
33+
film.setId(getNextId());
34+
films.put(film.getId(), film);
35+
log.info("фильм добавлен id: " + film.getId());
36+
return ResponseEntity.status(HttpStatus.CREATED).body(film);
37+
}
38+
39+
@PutMapping
40+
public ResponseEntity<?> update(@Valid @RequestBody Film newFilm) {
41+
if (newFilm.getId() == null) {
42+
log.warn("ID пустой");
43+
throw new ValidationException("пользователь с таким ID не найден");
44+
// изначально сделал чтоб в теле была ошибка, но не прошло тесты в Postman
45+
// return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("ID пустой");
46+
}
47+
if (!films.containsKey(newFilm.getId())) {
48+
log.warn("пользователь с таким ID не найден");
49+
// изначально сделал чтоб в теле была ошибка, но не прошло тесты в Postman
50+
// return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("пользователь с таким ID не найден");
51+
throw new ValidationException("пользователь с таким ID не найден");
52+
}
53+
validateController.validateFilm(newFilm);
54+
Film oldFilm = films.get(newFilm.getId());
55+
oldFilm.setName(newFilm.getName());
56+
oldFilm.setDescription(newFilm.getDescription());
57+
oldFilm.setReleaseDate(newFilm.getReleaseDate());
58+
oldFilm.setDuration(newFilm.getDuration());
59+
log.info("запись фильма обновлена");
60+
return ResponseEntity.ok(oldFilm);
61+
}
62+
63+
private long getNextId() {
64+
long currentMaxId = films.keySet()
65+
.stream()
66+
.mapToLong(id -> id)
67+
.max()
68+
.orElse(0);
69+
return ++currentMaxId;
70+
71+
}
772
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package ru.yandex.practicum.filmorate.controller;
2+
3+
import jakarta.validation.Valid;
4+
5+
import jakarta.validation.ValidationException;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.http.ResponseEntity;
10+
11+
import org.springframework.web.bind.annotation.*;
12+
import ru.yandex.practicum.filmorate.model.User;
13+
14+
import java.util.Collection;
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
18+
@RestController
19+
@RequestMapping("/users")
20+
public class UserController {
21+
22+
private final ValidateController validate = new ValidateController();
23+
private final Map<Long, User> users = new HashMap<>();
24+
25+
private final Logger log = LoggerFactory.getLogger(UserController.class);
26+
27+
@GetMapping //запрос всех пользователей
28+
public ResponseEntity<Collection<User>> usersAll() {
29+
30+
log.info("вывод списка пользователей");
31+
return ResponseEntity.ok(users.values());
32+
}
33+
34+
@PostMapping //добавление нового пользователя
35+
public ResponseEntity<?> create(@Valid @RequestBody User user) {
36+
validate.validateUser(user);
37+
38+
if (users.containsValue(user)) {
39+
log.warn("ошибка добавления пользователя, такой пользователь уже есть");
40+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("ошибка добавления пользователя, такой пользователь уже есть");
41+
}
42+
user.setId(getNextId());
43+
users.put(user.getId(), user);
44+
log.info("пользователь создан id: " + user.getId());
45+
return ResponseEntity.status(HttpStatus.CREATED).body(user); //отрпавляем ответ с статусом и телом
46+
}
47+
48+
@PutMapping //обновление пользователя
49+
public ResponseEntity<?> update(@Valid @RequestBody User newUser) {
50+
if (newUser.getId() == null) {
51+
log.warn("ID пустой");
52+
throw new ValidationException(" ID пустой");
53+
// изначально сделал чтоб в теле была ошибка, но не прошло тесты в Postman
54+
// return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("ID пустой");
55+
}
56+
if (!users.containsKey(newUser.getId())) {
57+
log.warn("пользователь с таким ID не найден");
58+
throw new ValidationException("пользователь с таким ID не найден");
59+
// изначально сделал чтоб в теле была ошибка, но не прошло тесты в Postman
60+
// return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("пользователь с таким ID не найден");
61+
}
62+
validate.validateUser(newUser);
63+
User oldUser = users.get(newUser.getId());
64+
oldUser.setEmail(newUser.getEmail());
65+
oldUser.setLogin(newUser.getLogin());
66+
oldUser.setBirthday(newUser.getBirthday());
67+
oldUser.setName(newUser.getName());
68+
log.info("пользователь изменен");
69+
return ResponseEntity.ok(oldUser);
70+
}
71+
72+
private long getNextId() {
73+
long currentMaxId = users.keySet()
74+
.stream()
75+
.mapToLong(id -> id)
76+
.max()
77+
.orElse(0);
78+
return ++currentMaxId;
79+
80+
}
81+
}

0 commit comments

Comments
 (0)