Skip to content
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# java-filmorate
Template repository for Filmorate project.
![Схема базы данных](./docs/diagram.pdf)
Binary file added docs/diagram.pdf
Binary file not shown.
106 changes: 106 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?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>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</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>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>logbook-spring-boot-starter</artifactId>
<version>3.7.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>runtime</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<failOnViolation>true</failOnViolation>
<logViolationCountToConsole>true</logViolationCountToConsole>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
<phase>compile</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.yandex.practicum.filmorate;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@Slf4j
@SpringBootApplication
public class FilmorateApplication {
public static void main(String[] args) {
SpringApplication.run(FilmorateApplication.class, args);
log.info("Приложение запущено");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ru.yandex.practicum.filmorate.controller;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import ru.yandex.practicum.filmorate.ecxeption.DatabaseException;
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.ErrorResponse;

@RestControllerAdvice
public class ErrorHandler {
@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse validationException(final ValidationException e) {
return new ErrorResponse(e.getMessage());
}

@ExceptionHandler
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorResponse occurredException(final OccurredException e) {
return new ErrorResponse(e.getMessage());
}

@ExceptionHandler
@ResponseStatus(HttpStatus.NOT_FOUND)
public ErrorResponse notFoundException(final NotFoundException e) {
return new ErrorResponse(e.getMessage());
}

@ExceptionHandler
@ResponseStatus(HttpStatus.NOT_FOUND)
public ErrorResponse databaseException(final DatabaseException e) {
return new ErrorResponse(e.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ru.yandex.practicum.filmorate.controller;

import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import ru.yandex.practicum.filmorate.dto.FilmDto;
import ru.yandex.practicum.filmorate.dao.mappers.FilmMapper;
import ru.yandex.practicum.filmorate.model.Film;
import org.springframework.web.bind.annotation.*;
import ru.yandex.practicum.filmorate.service.FilmService;

import java.util.Collection;

@RestController
@AllArgsConstructor
@RequestMapping("/films")
public class FilmController {
private final FilmService filmService;

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

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

@PostMapping
public Film createFilm(@Valid @RequestBody FilmDto filmDto) {
Film film = FilmMapper.mapToFilm(filmDto);
return filmService.createFilm(film);
}

@PutMapping
public Film updateFilm(@Valid @RequestBody FilmDto filmDto) {
Film film = FilmMapper.mapToFilm(filmDto);
return filmService.updateFilm(film);
}

@PutMapping("/{id}/like/{userId}")
public Film userLikesFilm(@PathVariable("id") Long id, @PathVariable("userId") Long userId) {
return filmService.userLikesFilm(id, userId);
}

@DeleteMapping("/{id}/like/{userId}")
public Film deleteLikesFilm(@PathVariable("id") Long id, @PathVariable("userId") Long userId) {
return filmService.deleteLikesFilm(id, userId);
}

@GetMapping("/popular")
public Collection<Film> listFirstCountFilm(@RequestParam(defaultValue = "10") int count) {
return filmService.listFirstCountFilm(count);
}

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

import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import ru.yandex.practicum.filmorate.model.Genre;
import ru.yandex.practicum.filmorate.service.GenreService;

import java.util.Collection;

@RestController
@RequestMapping("/genres")
@AllArgsConstructor
public class GenreController {

private final GenreService genreService;

@GetMapping
public Collection<Genre> getAllGenres() {
return genreService.getAllGenres();
}

@GetMapping("/{id}")
public Genre getGenreById(@PathVariable("id") long id) {
return genreService.getGenreById(id);
}

@PostMapping
public Genre createGenre(@Valid @RequestBody Genre genre) {
return genreService.createGenre(genre);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ru.yandex.practicum.filmorate.controller;

import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import ru.yandex.practicum.filmorate.model.Rating;
import ru.yandex.practicum.filmorate.service.RatingService;

import java.util.Collection;

@RestController
@AllArgsConstructor
@RequestMapping("/mpa")
public class RatingController {
private final RatingService ratingService;

@GetMapping
public Collection<Rating> getAllRatings() {
return ratingService.getAllRatings();
}

@GetMapping("/{id}")
public Rating getRatingById(@PathVariable("id") long id) {
return ratingService.getRatingById(id);
}

@PostMapping
public Rating createRating(@Valid @RequestBody Rating rating) {
return ratingService.createRating(rating);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ru.yandex.practicum.filmorate.controller;

import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import ru.yandex.practicum.filmorate.dao.mappers.UserMapper;
import ru.yandex.practicum.filmorate.dto.UserDto;
import ru.yandex.practicum.filmorate.ecxeption.NotFoundException;
import ru.yandex.practicum.filmorate.model.User;
import org.springframework.web.bind.annotation.*;
import ru.yandex.practicum.filmorate.service.UserService;

import java.util.Collection;

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

private final UserService userService;
private final UserMapper userMapper;

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

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

@PostMapping
public User createUser(@Valid @RequestBody UserDto userDto) {
User user = UserMapper.mapToUser(userDto);
return userService.createUser(user);
}

@PutMapping
public User updateUser(@Valid @RequestBody UserDto userDto) {
User user = UserMapper.mapToUser(userDto);
return userService.updateUser(user);
}

@PutMapping("/{id}/friends/{friendId}")
public User putFriends(@PathVariable("id") long id, @PathVariable("friendId") long friendId) {
return userService.makeFriendship(id, friendId);
}

@DeleteMapping("/{id}/friends/{friendId}")
public User deleteFriends(@PathVariable("id") long id, @PathVariable("friendId") long friendId) {
return userService.deleteFriendship(id, friendId);
}

@GetMapping("/{id}/friends")
public Collection<User> listFriends(@PathVariable("id") long id) {
if (userService.listOfFriends(id) == null) {
throw new NotFoundException("Такого юзера нет в списке!");
}
return userService.listOfFriends(id);
}

@GetMapping("/{id}/friends/common/{otherId}")
public Collection<User> commonFriends(@PathVariable("id") long id, @PathVariable("otherId") long otherId) {
return userService.listOfCommonFriends(id, otherId);
}

}
Loading