Skip to content

Commit

Permalink
commit 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergeev-Alexander committed Dec 6, 2023
1 parent 9abe17c commit 9f595be
Show file tree
Hide file tree
Showing 26 changed files with 1,202 additions and 102 deletions.
19 changes: 18 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,24 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ru.yandex.practicum.filmorate.model.Film;
import ru.yandex.practicum.filmorate.model.Genre;
import ru.yandex.practicum.filmorate.model.Mpa;
import ru.yandex.practicum.filmorate.service.FilmService;

import javax.validation.Valid;
import javax.validation.constraints.Positive;
import java.util.List;
import java.util.Set;

@Slf4j
@Validated
Expand All @@ -28,9 +31,9 @@ public List<Film> getAllFilms() {
return filmService.getAllFilms();
}

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

@GetMapping("/films/popular")
Expand All @@ -41,6 +44,26 @@ public List<Film> getPopularFilms(
return filmService.getPopularFilms(count);
}

@GetMapping("/genres")
public Set<Genre> getAllGenres() {
return filmService.getAllGenres();
}

@GetMapping("genres/{genreId}")
public Genre getGenreById(@PathVariable Integer genreId) {
return filmService.getGenreById(genreId);
}

@GetMapping("/mpa")
public Set<Mpa> getAllMpa() {
return filmService.getAllMpa();
}

@GetMapping("mpa/{mpaId}")
public Mpa getMpaById(@PathVariable Integer mpaId) {
return filmService.getMpaById(mpaId);
}

@PostMapping("/films")
public Film postFilm(@Valid @RequestBody Film film) {
return filmService.postFilm(film);
Expand All @@ -51,14 +74,14 @@ public Film putFilm(@Valid @RequestBody Film film) {
return filmService.putFilm(film);
}

@PutMapping("/films/{id}/like/{userId}")
public void putLike(@PathVariable Integer id, @PathVariable Integer userId) {
filmService.putLike(id, userId);
@PutMapping("/films/{filmId}/like/{userId}")
public void putLike(@PathVariable Integer filmId, @PathVariable Integer userId) {
filmService.putRate(filmId, userId);
}

@DeleteMapping("/films/{id}/like/{userId}")
public void deleteLike(@PathVariable Integer id, @PathVariable Integer userId) {
filmService.deleteLike(id, userId);
@DeleteMapping("/films/{filmId}/like/{userId}")
public void deleteRate(@PathVariable Integer filmId, @PathVariable Integer userId) {
filmService.deleteRate(filmId, userId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.springframework.web.bind.annotation.*;
import ru.yandex.practicum.filmorate.model.User;
import ru.yandex.practicum.filmorate.service.UserService;
import ru.yandex.practicum.filmorate.storage.user.UserStorage;

import javax.validation.Valid;
import java.util.List;
Expand All @@ -17,7 +16,7 @@ public class UserController {
private final UserService userService;

@Autowired
public UserController(UserStorage userStorage, UserService userService) {
public UserController(UserService userService) {
this.userService = userService;
}

Expand All @@ -26,19 +25,19 @@ public List<User> getAllUsers() {
return userService.getAllUsers();
}

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

@GetMapping("users/{id}/friends")
public List<User> getAllUserFriends(@PathVariable Integer id) {
return userService.getAllUserFriends(id);
@GetMapping("users/{userId}/friends")
public List<User> getAllUserFriends(@PathVariable Integer userId) {
return userService.getAllUserFriends(userId);
}

@GetMapping("/users/{id}/friends/common/{otherId}")
public List<User> getUsersCommonFriends(@PathVariable Integer id, @PathVariable Integer otherId) {
return userService.getUsersCommonFriends(id, otherId);
@GetMapping("/users/{userId}/friends/common/{otherId}")
public List<User> getUserCommonFriends(@PathVariable Integer userId, @PathVariable Integer otherId) {
return userService.getUserCommonFriends(userId, otherId);
}

@PostMapping("/users")
Expand All @@ -51,14 +50,14 @@ public User putUser(@Valid @RequestBody User user) {
return userService.putUser(user);
}

@PutMapping("/users/{id}/friends/{friendId}")
public void putNewFriend(@PathVariable Integer id, @PathVariable Integer friendId) {
userService.putNewFriend(id, friendId);
@PutMapping("/users/{userId}/friends/{friendId}")
public void putNewFriend(@PathVariable Integer userId, @PathVariable Integer friendId) {
userService.putNewFriend(userId, friendId);
}

@DeleteMapping("/users/{id}/friends/{friendId}")
public void deleteFriend(@PathVariable Integer id, @PathVariable Integer friendId) {
userService.deleteFriend(id, friendId);
@DeleteMapping("/users/{userId}/friends/{friendId}")
public void deleteFriend(@PathVariable Integer userId, @PathVariable Integer friendId) {
userService.deleteFriend(userId, friendId);
}

}
9 changes: 5 additions & 4 deletions src/main/java/ru/yandex/practicum/filmorate/model/Film.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ru.yandex.practicum.filmorate.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import ru.yandex.practicum.filmorate.validation.ReleaseDateValidation;

Expand All @@ -9,7 +8,6 @@
import javax.validation.constraints.Positive;
import javax.validation.constraints.Size;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.Set;

@Data
Expand All @@ -31,7 +29,10 @@ public class Film {
@Positive(message = "Duration field must be positive!")
private Long duration;

@JsonIgnore
private Set<Integer> likes = new HashSet<>();
private Set<Integer> rates;

private Mpa mpa;

private Set<Genre> genres;

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

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Genre {

Integer id;

String name;

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

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Mpa {

Integer id;

String name;

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

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import javax.validation.constraints.*;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.Set;

@Data
public class User {
Expand All @@ -27,7 +24,4 @@ public class User {
@PastOrPresent(message = "Birthday field must contain a past date!")
private LocalDate birthday;

@JsonIgnore
private Set<Integer> friends = new HashSet<>();

}
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
package ru.yandex.practicum.filmorate.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import ru.yandex.practicum.filmorate.exception.ItemNotPresentException;
import ru.yandex.practicum.filmorate.model.Film;
import ru.yandex.practicum.filmorate.model.Genre;
import ru.yandex.practicum.filmorate.model.Mpa;
import ru.yandex.practicum.filmorate.storage.film.FilmStorage;
import ru.yandex.practicum.filmorate.storage.user.UserStorage;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j

@Service
public class FilmService {

private final FilmStorage filmStorage;
private final UserStorage userStorage;

@Autowired
public FilmService(FilmStorage filmStorage, UserStorage userStorage) {
this.filmStorage = filmStorage;
this.userStorage = userStorage;
}
@Qualifier("FilmDbStorage")
private FilmStorage filmStorage;

public List<Film> getAllFilms() {
return filmStorage.getAllFilms();
Expand All @@ -35,6 +32,22 @@ public Film getFilmById(Integer id) {
() -> new ItemNotPresentException("There's no film with " + id + " id!"));
}

public Set<Genre> getAllGenres() {
return filmStorage.getAllGenres();
}

public Genre getGenreById(Integer genreId) {
return filmStorage.getGenreById(genreId);
}

public Set<Mpa> getAllMpa() {
return filmStorage.getAllMpa();
}

public Mpa getMpaById(Integer mpaId) {
return filmStorage.getMpaById(mpaId);
}

public Film postFilm(Film film) {
return filmStorage.postFilm(film);
}
Expand All @@ -43,25 +56,19 @@ public Film putFilm(Film film) {
return filmStorage.putFilm(film);
}

public void putLike(Integer id, Integer userId) {
userStorage.getUserById(userId);
getFilmById(id).getLikes().add(userId);
log.info("User {} liked film {}", userId, id);
public void putRate(Integer filmId, Integer userId) {
filmStorage.putRate(filmId, userId);
}

public void deleteLike(Integer id, Integer userId) {
userStorage.getUserById(userId);
getFilmById(id).getLikes().remove(userId);
log.info("User {} unliked film {}", userId, id);
public void deleteRate(Integer filmId, Integer userId) {
filmStorage.deleteRate(filmId, userId);
}

public List<Film> getPopularFilms(Integer count) {
List<Film> filmList = filmStorage.getAllFilms().stream()
.sorted(Comparator.comparing(film -> film.getLikes().size(), Comparator.nullsLast(Comparator.reverseOrder())))
return filmStorage.getAllFilms().stream()
.sorted(Comparator.comparing(film -> film.getRates().size(), Comparator.nullsLast(Comparator.reverseOrder())))
.limit(count)
.collect(Collectors.toList());
log.info("Showed popular films {}", filmList);
return filmList;
}

}
Loading

0 comments on commit 9f595be

Please sign in to comment.