Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
3fa28d7
Финальное задание 10-го спринта
NadezhdaTA Feb 28, 2025
052a6fe
Финальное задание 10-го спринта (версия 1.2)
NadezhdaTA Feb 28, 2025
a685f62
Финальное задание 10-го спринта с доработками
NadezhdaTA Mar 2, 2025
9c90751
Финальное задание 10-го спринта с доработками
NadezhdaTA Mar 2, 2025
1bdd204
Финальное задание 10-го спринта с доработками (версия 1.3)
NadezhdaTA Mar 2, 2025
bf685be
Финальное задание 10-го спринта с доработками (версия 1.4)
NadezhdaTA Mar 2, 2025
c2b5ebe
Финальное задание 10-го спринта с доработками (версия 1.5)
NadezhdaTA Mar 2, 2025
af1c8b4
Финальное задание 11-го спринта
NadezhdaTA Mar 17, 2025
f6271be
Финальное задание 11-го спринта (версия 1.1)
NadezhdaTA Mar 17, 2025
097b853
Финальное задание 11-го спринта (версия 1.2)
NadezhdaTA Mar 17, 2025
a499214
Финальное задание 11-го спринта (версия 1.3)
NadezhdaTA Mar 17, 2025
5694a43
Финальное задание 11-го спринта (версия 1.4)
NadezhdaTA Mar 17, 2025
2288202
Финальное задание 11-го спринта (версия 1.5 переработанная)
NadezhdaTA Mar 22, 2025
ec47ef2
Финальное задание 11-го спринта (версия 1.6)
NadezhdaTA Mar 22, 2025
324dd64
Финальное задание 11-го спринта с исправлениями
NadezhdaTA Mar 23, 2025
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
8 changes: 2 additions & 6 deletions .github/workflows/api-tests.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#file: noinspection SpellCheckingInspection,SpellCheckingInspection
name: Films API Tests

on:
pull_request:

jobs:
build:
uses: yandex-praktikum/java-filmorate/.github/workflows/api-tests.yml@ci
on:
31 changes: 30 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand All @@ -32,6 +31,35 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>logbook-spring-boot-starter</artifactId>
<version>3.7.2</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>8.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.glassfish.expressly</groupId>
<artifactId>expressly</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -41,6 +69,7 @@
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>

</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@SpringBootApplication
public class FilmorateApplication {
public static void main(String[] args) {

SpringApplication.run(FilmorateApplication.class, args);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ru.yandex.practicum.filmorate.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import ru.yandex.practicum.filmorate.exception.DuplicatedDataException;
import ru.yandex.practicum.filmorate.exception.NotFoundException;
import ru.yandex.practicum.filmorate.exception.ValidationException;

@RestController
@ControllerAdvice
@Slf4j
public class ErrorHandler {

@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleDuplicatedDataException(final DuplicatedDataException e) {
log.error("Duplicated: {}", e.getMessage());
return new ErrorResponse(e.getMessage());
}

@ExceptionHandler
@ResponseStatus(HttpStatus.NOT_FOUND)
public ErrorResponse handleNotFoundException(final NotFoundException e) {
log.error("NotFound: {}", e.getMessage());
return new ErrorResponse(e.getMessage());
}

@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleValidationException(final ValidationException e) {
log.error("Validation: {}", e.getMessage());
return new ErrorResponse(e.getMessage());
}

@ExceptionHandler
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorResponse handleException(final Exception e) {
log.error("ServerError: {}", e.getMessage());
return new ErrorResponse(e.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.yandex.practicum.filmorate.controller;

import lombok.Getter;

@Getter
public class ErrorResponse {
String error;

public ErrorResponse(String error) {
this.error = error;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,66 @@
package ru.yandex.practicum.filmorate.controller;

import org.springframework.web.bind.annotation.RestController;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ru.yandex.practicum.filmorate.exception.DuplicatedDataException;
import ru.yandex.practicum.filmorate.exception.NotFoundException;
import ru.yandex.practicum.filmorate.model.Film;
import ru.yandex.practicum.filmorate.service.FilmService;

import java.util.Collection;

@RestController
@RequestMapping("/films")
@Slf4j
@RequiredArgsConstructor
@Validated
public class FilmController {
}
private final FilmService filmService;

@PostMapping
public Film createFilm(@Valid @RequestBody Film film) {
Film created = filmService.createFilm(film);
log.debug("Фильм успешно добавлен - {} \n", created);
return created;
}

@PutMapping
public Film updateFilm(@Valid @RequestBody Film newFilm) throws DuplicatedDataException, NotFoundException {
Film updated = filmService.updateFilm(newFilm);
log.debug("Фильм успешно обновлен - {} \n", updated);
return updated;
}

@GetMapping
public Collection<Film> getFilmsList() {
return filmService.getFilmsList();
}

@GetMapping("/{id}")
public Film getFilmById(@PathVariable int id) throws NotFoundException {
return filmService.getFilmById(id);
}

@PutMapping("{id}/like/{userId}")
public void addLikes(@PathVariable int id,
@PathVariable int userId) {
filmService.addLikes(id, userId);
log.debug("Like успешно добавлен.");
}

@DeleteMapping("{id}/like/{userId}")
public void deleteFilmLikes(@PathVariable int id,
@PathVariable int userId) {
filmService.deleteLikes(id, userId);
log.debug("Like успешно удален.");
}

@GetMapping("popular")
public Collection<String> getPopularFilmList(@RequestParam(defaultValue = "10") int count) {
return filmService.getPopularFilms(count);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ru.yandex.practicum.filmorate.controller;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ru.yandex.practicum.filmorate.exception.DuplicatedDataException;
import ru.yandex.practicum.filmorate.exception.NotFoundException;
import ru.yandex.practicum.filmorate.model.User;
import ru.yandex.practicum.filmorate.service.UserService;

import java.util.Collection;

@RestController
@RequestMapping("/users")
@Slf4j
@RequiredArgsConstructor
@Validated
public class UserController {
private final UserService userService;

@PostMapping
public User createUser(@Valid @RequestBody User user) {
User created = userService.createUser(user);
log.debug("Пользователь успешно добавлен - {} \n.", created);
return created;
}

@PutMapping
public User updateUser(@Valid @RequestBody User newUser) throws DuplicatedDataException, NotFoundException {
User updated = userService.updateUser(newUser);
log.debug("Пользователь успешно обновлен - {} \n.", updated);
return updated;
}

@GetMapping
public Collection<User> getUsersList() {
return userService.getUsersList();
}

@GetMapping("/{id}")
public User getUserById(@PathVariable int id) throws NotFoundException {
return userService.getUserById(id);
}

@GetMapping("/{id}/friends")
public Collection<String> getUsersFriends(@PathVariable int id) {
return userService.getFriendsList(id);
}

@GetMapping("/{id}/friends/common/{friendId}")
public Collection<String> getCommonFriends(@PathVariable int id,
@PathVariable int friendId) {
return userService.getCommonFriends(id, friendId);
}

@PutMapping("/{id}/friends/{friendId}")
public void addFriend(@PathVariable int id,
@PathVariable int friendId) throws NotFoundException {
userService.addFriend(id, friendId);
log.debug("Друг успешно добавлен.");
}

@DeleteMapping("/{id}/friends/{friendId}")
public void deleteFriend(@PathVariable int id,
@PathVariable int friendId) throws NotFoundException {
userService.deleteFriend(id, friendId);
log.debug("Друг успешно удален.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.yandex.practicum.filmorate.exception;

public class DuplicatedDataException extends RuntimeException {

public DuplicatedDataException() {
super();
}

public DuplicatedDataException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.yandex.practicum.filmorate.exception;

public class NotFoundException extends RuntimeException {

public NotFoundException() {
super();
}

public NotFoundException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.yandex.practicum.filmorate.exception;

public class ValidationException extends RuntimeException {

public ValidationException() {
super();
}

public ValidationException(String message) {
super(message);
}
}
48 changes: 40 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,44 @@
package ru.yandex.practicum.filmorate.model;

import lombok.Getter;
import lombok.Setter;

/**
* Film.
*/
@Getter
@Setter
import javax.validation.constraints.Past;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Length;

import java.time.LocalDate;

@Data
@Slf4j
public class Film {

@NotBlank(message = "Название не должно быть пустым или иметь пробелы.")
private String name;

@NotNull(message = "Описание фильма не может быть пустым.")
@Length(max = 200, message = "Описание фильма не может превышать {max} знаков.")
private String description;

@Past(message = "Дата выхода фильма не может быть в будущем.")
@NotNull(message = "Дата выхода фильма не может быть пустой.")
@FilmStartDate
private LocalDate releaseDate;

@NotNull(message = "Продолжительность фильма не может быть пустой.")
@Positive(message = "Продолжительность фильма не может быть меньше 0.")
Integer duration;

private Integer id;

public Film() {

}

public Film(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.yandex.practicum.filmorate.model;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Target({ FIELD })
@Retention(RUNTIME)
@Constraint(validatedBy = FilmStartDateValidator.class)
@Documented
@interface FilmStartDate {
String message() default "{CapitalLetter.invalid}";

Class<?>[] groups() default { };

Class<? extends Payload>[] payload() default { };
}
Loading