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
Binary file added ER Diagramm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 42 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
# java-filmorate
Template repository for Filmorate project.
# Java filmorate

*Как работает база данных:*

**1. Таблица movie**
PK: movie_id (уникальный идентификатор фильма)
Содержит основные поля с названием, описанием фильма, id жанра (one to one с таблицей genre), mpa_id(one to one с таблицей mpa)
**2. Таблица movie_advanced**
PK: movie_id (one to one с таблицей movie)
Содержит дополнительную информацию о фильме. Создана для опциональных характеристик картины с запасом на расширение, что бы не перегружать остальное.
**3. Таблицы genre, mpa** - содержат информацию о жанре и рейтинге фильма. Вынесены в отдельную таблицу для упрощения корректировок значений.
**4. Таблица user**
PK: user_id (уникальный идентификатор пользователя)
Содержит основные поля, характеризующие пользователя
**5. Таблица user_friendlist**
PK: user_id+friend_id - ключ составной, связка id юзера+ id юзера-друга уникальна.
Предполагается выгрузка списка друзей по user_id и фильтрация по friendship_status, который отражает статус взаимоотношений:
0 - запрос отправлен от "друга"
1 - запрос согласован.
**6. Таблица movie_rating**
PK: movie_id + user_id - ключ составной, связка фильма и id лайкнувшего юзера уникальна.
Из неё можно будет каунтить рейтинг фильма по id, а также искать пересечения по лайкнувшим юзерам для составления списка обших фильмов.

**Примеры запросов**
1. Получение фильма по id:
SELECT*
FROM movie
WHERE movie_id = {id фильма};
2. Получение TOP-10 фильмов
SELECT *
FROM movie
WHERE movie_id IN (SELECT movie_id
FROM movie_rating
GROUP BY movie_id
ORDER BY COUNT(user_id) DESC
LIMIT 10);



![Диаграмма базы данных](src/main/resources/Java-filmorate%20SQL%20db.png)

SP12 start
18 changes: 18 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.210</version>
<scope>runtime</scope>
</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>
</dependencies>

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

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ru.yandex.practicum.filmorate.model.Film;
Expand Down Expand Up @@ -50,4 +49,9 @@ public Film removeLike(@PathVariable Integer filmId, @PathVariable Integer id) {
public Collection<Film> getTop(@RequestParam(defaultValue = "10") Integer count) {
return filmService.top(count);
}

@GetMapping("/{id}")
public Film getFilm(@PathVariable Integer id) {
return filmService.getFilm(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ru.yandex.practicum.filmorate.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ru.yandex.practicum.filmorate.model.Genre;
import ru.yandex.practicum.filmorate.service.FilmService;

import java.util.Collection;

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

private final FilmService filmService;

@Autowired
public GenreController(FilmService filmService) {
this.filmService = filmService;
}

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

@GetMapping("/{id}")
public Genre getGenre(@PathVariable Integer id) {
return filmService.getGenre(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ru.yandex.practicum.filmorate.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ru.yandex.practicum.filmorate.model.Mpa;
import ru.yandex.practicum.filmorate.service.FilmService;

import java.util.Collection;

@Slf4j
@RestController
@RequestMapping("/mpa")
public class MPAController {

private final FilmService filmService;

@Autowired
public MPAController(FilmService filmService) {
this.filmService = filmService;
}

@GetMapping
public Collection<Mpa> getAllGenres() {
return filmService.getAllMpa();
}

@GetMapping("/{id}")
public Mpa getGenre(@PathVariable Integer id) {
return filmService.getMpa(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ public Collection<User> getFriends(@PathVariable Integer id) {
return userService.getFriends(id);
}

@GetMapping("/test")
public Collection<User> testSequense(@PathVariable Integer id) {
return userService.getFriends(id);
}

}
7 changes: 6 additions & 1 deletion src/main/java/ru/yandex/practicum/filmorate/model/Film.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import ru.yandex.practicum.filmorate.exceptions.UnknownDataException;

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


@AllArgsConstructor
@NoArgsConstructor
@Data
public class Film {
private int id;
Expand All @@ -19,6 +21,8 @@ public class Film {
private LocalDate releaseDate;
private long duration;
private int rating = 0;
private Collection<Genre> genres;
private Mpa mpa;
@JsonIgnore
private Set<Integer> whoLikes;

Expand Down Expand Up @@ -48,3 +52,4 @@ public Film removeLike(Integer id) {
}



24 changes: 24 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,24 @@
package ru.yandex.practicum.filmorate.model;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode
public class Genre {
private int id;
private String name;

public Genre(int id, String name) {
this.id = id;
this.name = name;
}

public int getId() {
return id;
}

public String getName() {
return name;
}
}
22 changes: 22 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,22 @@
package ru.yandex.practicum.filmorate.model;

import lombok.Data;

@Data
public class Mpa {
private int id;
private String name;

public Mpa(int id, String name) {
this.id = id;
this.name = name;
}

public int getId() {
return id;
}

public String getName() {
return name;
}
}
23 changes: 4 additions & 19 deletions src/main/java/ru/yandex/practicum/filmorate/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
Expand All @@ -11,33 +12,17 @@

@Slf4j
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
private int id;
private String email;
private String login;
private String name;
private LocalDate birthday;
private Set<Integer> friends;
private Set<User> friends;

public User addFriend(Integer friendId) {
if (friends == null) {
friends = new HashSet<>();
}
friends.add(friendId);
log.info("Друзья пользователя " + id + ": " + friends);
return this;
}

public User deleteFriend(Integer friendId) {
if (friends == null) {
friends = new HashSet<>();
}
friends.remove(friendId);
return this;
}

public Collection<Integer> getFriends() {
public Collection<User> getFriends() {
if (friends == null) {
friends = new HashSet<>();
}
Expand Down
Loading