Skip to content

Commit e607e05

Browse files
committed
add-database. Добавить базу данных для сохранения сущностей.
1 parent 7c971db commit e607e05

32 files changed

+993
-16
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@
4141
<artifactId>logbook-spring-boot-starter</artifactId>
4242
<version>3.7.2</version>
4343
</dependency>
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-jdbc</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>com.h2database</groupId>
50+
<artifactId>h2</artifactId>
51+
</dependency>
4452
</dependencies>
4553

4654
<build>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ru.yandex.practicum.filmorate.controller;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
import ru.yandex.practicum.filmorate.model.Genre;
9+
import ru.yandex.practicum.filmorate.service.GenreService;
10+
11+
import java.util.List;
12+
13+
@RestController
14+
@RequestMapping(value = "/genres")
15+
@RequiredArgsConstructor
16+
public class GenreController {
17+
private final GenreService service;
18+
19+
@GetMapping
20+
public List<Genre> getAll() {
21+
return service.getAll();
22+
}
23+
24+
@GetMapping("/{id}")
25+
public Genre getById(@PathVariable Long id) {
26+
return service.getById(id);
27+
}
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ru.yandex.practicum.filmorate.controller;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
import ru.yandex.practicum.filmorate.model.RatingMPA;
9+
import ru.yandex.practicum.filmorate.service.RatingMPAService;
10+
11+
import java.util.List;
12+
13+
@RestController
14+
@RequestMapping(value = "/mpa")
15+
@RequiredArgsConstructor
16+
public class RatingMPAController {
17+
18+
private final RatingMPAService service;
19+
20+
@GetMapping
21+
public List<RatingMPA> getAll() {
22+
return service.getAll();
23+
}
24+
25+
@GetMapping("/{id}")
26+
public RatingMPA getById(@PathVariable Long id) {
27+
return service.getById(id);
28+
}
29+
}

src/main/java/ru/yandex/practicum/filmorate/exception/ExceptionHandler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ru.yandex.practicum.filmorate.exception;
22

33
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.MethodArgumentNotValidException;
45
import org.springframework.web.bind.annotation.ResponseStatus;
56
import org.springframework.web.bind.annotation.RestControllerAdvice;
67

@@ -17,6 +18,15 @@ public ErrorResponse validateHandler(ValidationException e) {
1718
);
1819
}
1920

21+
@org.springframework.web.bind.annotation.ExceptionHandler
22+
@ResponseStatus(HttpStatus.BAD_REQUEST)
23+
public ErrorResponse validateHandler(MethodArgumentNotValidException e) {
24+
return new ErrorResponse(
25+
"Ошибка валидации",
26+
e.getMessage()
27+
);
28+
}
29+
2030
@org.springframework.web.bind.annotation.ExceptionHandler
2131
@ResponseStatus(HttpStatus.NOT_FOUND)
2232
public ErrorResponse notFoundHandler(NotFoundException e) {

src/main/java/ru/yandex/practicum/filmorate/model/Film.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
import jakarta.validation.constraints.NotNull;
55
import jakarta.validation.constraints.Positive;
66
import jakarta.validation.constraints.Size;
7-
import lombok.AllArgsConstructor;
8-
import lombok.Builder;
9-
import lombok.Data;
10-
import lombok.NoArgsConstructor;
7+
import lombok.*;
118

129
import java.time.LocalDate;
1310
import java.util.ArrayList;
@@ -37,6 +34,12 @@ public class Film {
3734

3835
private List<Long> likes = new ArrayList<>();
3936

37+
private List<Genre> genres;
38+
39+
@NonNull
40+
private RatingMPA mpa;
41+
42+
4043
public void addLike(Long userId) {
4144
likes.add(userId);
4245
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ru.yandex.practicum.filmorate.model;
2+
3+
import lombok.*;
4+
5+
@Data
6+
@Builder(toBuilder = true)
7+
@NoArgsConstructor
8+
@AllArgsConstructor
9+
public class Genre {
10+
11+
private Long id;
12+
private String name;
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ru.yandex.practicum.filmorate.model;
2+
3+
import lombok.*;
4+
5+
@Data
6+
@Builder(toBuilder = true)
7+
@NoArgsConstructor
8+
@AllArgsConstructor
9+
public class RatingMPA {
10+
11+
private Long id;
12+
private String name;
13+
14+
}

src/main/java/ru/yandex/practicum/filmorate/service/FilmService.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public class FilmService {
2020

2121
final FilmStorage filmStorage;
2222

23+
private final GenreService genreService;
24+
25+
private final RatingMPAService ratingMPAService;
26+
2327
public List<Film> getFilms() {
2428
log.info("Запрос всех фильмов");
2529
return filmStorage.getFilms();
@@ -35,7 +39,15 @@ public Film create(Film film) {
3539

3640
film.setId(getNextId());
3741

38-
filmStorage.addOrUpdateFilm(film);
42+
if (film.getMpa() != null) {
43+
ratingMPAService.getById(film.getMpa().getId());
44+
}
45+
if (film.getGenres() != null) {
46+
film.getGenres().forEach(genre -> {
47+
genreService.getById(genre.getId());
48+
});
49+
}
50+
filmStorage.createFilm(film);
3951
log.info("Фильм добавлен");
4052
return film;
4153
}
@@ -54,7 +66,7 @@ public Film update(Film newFilm) {
5466
oldFilm.setDescription(newFilm.getDescription());
5567
oldFilm.setReleaseDate(newFilm.getReleaseDate());
5668
oldFilm.setDuration(newFilm.getDuration());
57-
filmStorage.addOrUpdateFilm(oldFilm);
69+
filmStorage.updateFilm(oldFilm);
5870
log.info("Фильм с id " + newFilm.getId() + " обновлен");
5971
return oldFilm;
6072
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ru.yandex.practicum.filmorate.service;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.springframework.stereotype.Service;
5+
import ru.yandex.practicum.filmorate.exception.NotFoundException;
6+
import ru.yandex.practicum.filmorate.model.Genre;
7+
import ru.yandex.practicum.filmorate.storage.interfaces.GenreStorage;
8+
9+
import java.util.List;
10+
11+
@Service
12+
@RequiredArgsConstructor
13+
public class GenreService {
14+
15+
private final GenreStorage storage;
16+
17+
public List<Genre> getAll() {
18+
return storage.getAll();
19+
}
20+
21+
public Genre getById(Long id) {
22+
if (storage.getById(id) == null) {
23+
throw new NotFoundException("Жанр с id = " + id + " не найден");
24+
} else {
25+
return storage.getById(id);
26+
}
27+
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ru.yandex.practicum.filmorate.service;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.springframework.stereotype.Service;
5+
import ru.yandex.practicum.filmorate.exception.NotFoundException;
6+
import ru.yandex.practicum.filmorate.model.RatingMPA;
7+
import ru.yandex.practicum.filmorate.storage.interfaces.RatingMPAStorage;
8+
9+
import java.util.List;
10+
11+
@Service
12+
@RequiredArgsConstructor
13+
public class RatingMPAService {
14+
15+
private final RatingMPAStorage storage;
16+
17+
public List<RatingMPA> getAll() {
18+
return storage.getAll();
19+
}
20+
21+
public RatingMPA getById(Long id) {
22+
if (storage.getById(id) == null) {
23+
throw new NotFoundException("Рейтинг с id = " + id + " не найден");
24+
} else {
25+
return storage.getById(id);
26+
}
27+
28+
}
29+
}

0 commit comments

Comments
 (0)