Skip to content
Merged
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
105 changes: 103 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,103 @@
# java-filmorate
Template repository for Filmorate project.
# Filmorate
Это проект кинотеки, в который можно добавлять комментарии, оценивать фильмы находить самые популярные а так добавлять
в друзья пользователей кинотеки.

# Схема БД
<img alt="Схема БД" src="filmorate.png">

# Примеры запросов

### Для пользователей

* Создание пользователя
```SQL
INSERT INTO users(email,
login,
name,
birthday)
VALUES (?,?,?,?);
```

* Обновление пользователя
```SQL
UPDATE users
SET email=?,
login=?,
name=?,
birthday=?
WHERE id=?;
```
* Получение пользоывтеля по ID
```SQL
SELECT * FROM users WHERE id=?;
```
* Получение всех пользователей
```SQL
SELECT * FROM users;
```
* Удаление пользователя
```SQL
DELETE FROM users WHERE id=?;
```
* Добавление в список друзей
```SQL
INSERT INTO friends (user_id, to_user_id) VALUES (?,?);
```
* Получение списка друзей
```SQL
"SELECT to_user_id FROM friends WHERE user_id=?;
```
* Обновление списка друзей
```SQL
DELETE FROM users WHERE id=?;
```
### Для фильмов

* Добавление фильма
```SQL
INSERT INTO films(name,
description,
release_date,
duration,
mpa_rating_id)
VALUES(?,?,?,?,?);
```
* Обновление фильма
```SQL
UPDATE films SET name=?,
description=?,
release_date=?,
duration=?,
mpa_rating_id=?
WHERE id=?;
```
* Удаление фильма
```SQL
DELETE FROM films WHERE id=?;
```
* Получение фильма по ID
```SQL
SELECT * FROM films WHERE id=?;
```
* Получение всех фильмов
```SQL
SELECT * FROM films;
```
* Получение списка лайков фильма
```SQL
SELECT user_id FROM film_likes WHERE film_id=?;
```
* Добавление лайка фильма
```SQL
INSERT INTO film_likes(film_id, user_id) VALUES (?,?);
```
* Удвление лайка фильма
```SQL
DELETE FROM film_likes WHERE film_id=? AND user_id=?;
```
* Получение списка жанров фильма
```SQL
SELECT g.genre_id AS id, gs.name AS name
FROM genre AS g LEFT OUTER JOIN genres AS gs ON g.genre_id = gs.id
WHERE g.film_id =? ORDER BY id";
```
Binary file added filmorate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>

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

import jakarta.validation.constraints.Positive;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;

import org.springframework.web.bind.annotation.*;
import ru.yandex.practicum.filmorate.model.Genre;
import ru.yandex.practicum.filmorate.service.GenreService;

import java.util.Collection;

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

private final GenreService genreService;
private final Logger logger = LoggerFactory.getLogger(GenreService.class);

@Autowired
public GenreController(GenreService genreService) {
this.genreService = genreService;
}

@GetMapping({"/{id}"})
@ResponseStatus(HttpStatus.OK)
public Genre getGenreId(@Positive(message = "неверное значение") @PathVariable long id) {
logger.info("возращаем жанр по id: " + id);
return genreService.getGenreId(id);
}

@GetMapping
@ResponseStatus(HttpStatus.OK)
public Collection<Genre> getGenreAll() {
logger.info("возращаем список жанров");
return genreService.getGenreAll();
}

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


import jakarta.validation.constraints.Positive;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ru.yandex.practicum.filmorate.model.Mpa;
import ru.yandex.practicum.filmorate.service.MpaService;

import java.util.Collection;

@Validated
@RestController
@RequestMapping("/mpa")
public class MpaController {
private final Logger logger = LoggerFactory.getLogger(MpaController.class);

private final MpaService mpaService;

@Autowired
public MpaController(MpaService mpaService) {
this.mpaService = mpaService;
}


@GetMapping({"/{id}"})
@ResponseStatus(HttpStatus.OK)
public Mpa getMpaId(@Positive(message = "неверное значение") @PathVariable long id) {
logger.info("возращаем жанр по id: " + id);
return mpaService.getMpaId(id);
}

@GetMapping
@ResponseStatus(HttpStatus.OK)
public Collection<Mpa> getMpaAll() {
logger.info("возращаем список жанров");
return mpaService.getMpaAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public Collection<User> friendsGetList(@Positive(message = "неверное з
@ResponseStatus(HttpStatus.OK)
public Collection<User> friendsGetCommonList(@Positive(message = "неверное значение") @PathVariable long id,
@Positive(message = "неверное значение") @PathVariable long otherId) {
logger.info("показывает список друзей");
logger.info("показывает обший список друзей");
return userService.friendsGetCommonList(id, otherId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.yandex.practicum.filmorate.exception;

public class NotFoundException extends RuntimeException {
public NotFoundException(String message) {
super(message);
}
}
17 changes: 9 additions & 8 deletions src/main/java/ru/yandex/practicum/filmorate/model/Film.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
import lombok.*;

import java.time.LocalDate;
import java.util.HashSet;
import java.util.Set;

/**
* Film.
*/
@Getter
@Setter
@ToString
@AllArgsConstructor
@Data
@NoArgsConstructor
public class Film extends DataModel implements Comparable<Film> {

private Long id;
Expand All @@ -34,7 +30,12 @@ public class Film extends DataModel implements Comparable<Film> {
@Min(1)
private int duration;

private Set<Long> likesId;
private Set<Long> likesId = new HashSet<>();

private Set<Genre> genres = new HashSet<>();

// @NotNull (message ="рейтинг не может быть пустым")
private Mpa mpa;

@Override
public int compareTo(Film o) {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/ru/yandex/practicum/filmorate/model/Friends.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.NoArgsConstructor;
import lombok.NonNull;

@Data
@NoArgsConstructor
public class Friends {

@NonNull
private long userId;

@NonNull
private long toUserId;
}
18 changes: 18 additions & 0 deletions src/main/java/ru/yandex/practicum/filmorate/model/Genre.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.yandex.practicum.filmorate.model;


import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;

@Data
@NoArgsConstructor
public class Genre extends DataModel {

@NonNull
private Long id;

@NonNull
private String name;

}
17 changes: 17 additions & 0 deletions src/main/java/ru/yandex/practicum/filmorate/model/Like.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ru.yandex.practicum.filmorate.model;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;

@Data
@NoArgsConstructor
public class Like {

@NonNull
private long userId;

@NonNull
private long filmId;

}
18 changes: 18 additions & 0 deletions src/main/java/ru/yandex/practicum/filmorate/model/Mpa.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.yandex.practicum.filmorate.model;

import jakarta.validation.constraints.Positive;
import lombok.*;

@Data
@NoArgsConstructor
public class Mpa extends DataModel {

@NonNull
@Positive
private long id;


private String name;


}
6 changes: 1 addition & 5 deletions src/main/java/ru/yandex/practicum/filmorate/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import lombok.*;

import java.time.LocalDate;
import java.util.Set;

@Setter
@Getter
@ToString
@AllArgsConstructor
@RequiredArgsConstructor
@EqualsAndHashCode(exclude = {"id", "name", "birthday"}, callSuper = false)
public class User extends DataModel {

Expand All @@ -30,7 +29,4 @@ public class User extends DataModel {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate birthday;

private Set<Long> friendsId;


}
Loading