diff --git a/backend/src/main/java/com/example/backend/favorites/service/FavoriteService.java b/backend/src/main/java/com/example/backend/favorites/service/FavoriteService.java index 7ba916f..faab61f 100644 --- a/backend/src/main/java/com/example/backend/favorites/service/FavoriteService.java +++ b/backend/src/main/java/com/example/backend/favorites/service/FavoriteService.java @@ -36,12 +36,6 @@ public class FavoriteService { return repository.findByUserIdAndMovieId(uId, mId); } - // @Transactional(readOnly = true) - // public List getForAddToFavorite(Integer userId, Integer - // movieId) { - // return repository.findByUserIdAndMovieId(userId, movieId); - // } - @Transactional public FavoriteEntity create(FavoriteEntity entity) { return repository.save(entity); diff --git a/backend/src/main/java/com/example/backend/movies/api/MovieUserController.java b/backend/src/main/java/com/example/backend/movies/api/MovieUserController.java index 4ae84a2..b83c732 100644 --- a/backend/src/main/java/com/example/backend/movies/api/MovieUserController.java +++ b/backend/src/main/java/com/example/backend/movies/api/MovieUserController.java @@ -181,6 +181,53 @@ public class MovieUserController { return Constants.REDIRECT_VIEW + URL; } + @PostMapping("/changeViewStatus/{id}") + public String changeViewStatus(@PathVariable(name = "id") Integer movieId, + @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, + Model model) { + + boolean isViewed = false; + + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + Integer currentUserId = userService.getCurrentUserId(authentication.getName()); + + ViewedEntity view = viewedServise.findByUserIdAndMovieId(currentUserId, movieId); + + if (view == null) { + isViewed = false; + } else { + isViewed = true; + } + + if (isViewed == false) { + + UserEntity user = userService.get(currentUserId); + MovieEntity movie = movieService.get(movieId); + + ViewedEntity newView = new ViewedEntity(null, user, movie); + + viewedServise.create(newView); + + } else { + + ViewedEntity delView = viewedServise.findByUserIdAndMovieId(currentUserId, movieId); + + viewedServise.delete(delView.getId()); + } + + model.addAttribute(PAGE_ATTRIBUTE, page); + + return Constants.REDIRECT_VIEW + URL; + } + + @GetMapping("/card/{id}") + public String getCardFilm(@PathVariable(name = "id") Integer movieId, + Model model) { + MovieEntity movie = movieService.get(movieId); + model.addAttribute("movie", movie); + return "card-film"; + } + @GetMapping("/countView") public Integer countView(@RequestParam(name = "movieId", defaultValue = "0") Integer movieId) { return movieService.countView(movieId); diff --git a/backend/src/main/java/com/example/backend/movies/service/MovieService.java b/backend/src/main/java/com/example/backend/movies/service/MovieService.java index 1789034..ac5457c 100644 --- a/backend/src/main/java/com/example/backend/movies/service/MovieService.java +++ b/backend/src/main/java/com/example/backend/movies/service/MovieService.java @@ -10,16 +10,20 @@ import com.example.backend.core.errors.NotFoundException; import com.example.backend.favorites.repository.FavoriteRepository; import com.example.backend.movies.model.MovieEntity; import com.example.backend.movies.repository.MovieRepository; +import com.example.backend.viewed.repository.ViewedRepository; @Service public class MovieService { private final MovieRepository repository; private final FavoriteRepository favoriteRepository; + private final ViewedRepository viewedRepository; - public MovieService(MovieRepository repository, FavoriteRepository favoriteRepository) { + public MovieService(MovieRepository repository, FavoriteRepository favoriteRepository, + ViewedRepository viewedRepository) { this.repository = repository; this.favoriteRepository = favoriteRepository; + this.viewedRepository = viewedRepository; } private void checkName(String name) { @@ -71,6 +75,8 @@ public class MovieService { public MovieEntity delete(Integer id) { final MovieEntity exisEntity = get(id); favoriteRepository.deleteByMovieId(exisEntity.getId()); + viewedRepository.deleteByMovieId(exisEntity.getId()); + repository.delete(exisEntity); return exisEntity; } diff --git a/backend/src/main/java/com/example/backend/viewed/repository/ViewedRepository.java b/backend/src/main/java/com/example/backend/viewed/repository/ViewedRepository.java index 114049f..fbad52f 100644 --- a/backend/src/main/java/com/example/backend/viewed/repository/ViewedRepository.java +++ b/backend/src/main/java/com/example/backend/viewed/repository/ViewedRepository.java @@ -4,6 +4,7 @@ import java.util.Optional; import java.util.List; import org.springframework.data.repository.CrudRepository; + import com.example.backend.viewed.model.ViewedEntity; public interface ViewedRepository extends CrudRepository { @@ -12,4 +13,8 @@ public interface ViewedRepository extends CrudRepository Optional findOneByUserIdAndId(Integer userId, Integer id); + ViewedEntity findByUserIdAndMovieId(Integer userId, Integer movieId); + + Optional deleteByMovieId(Integer movieId); + } diff --git a/backend/src/main/java/com/example/backend/viewed/service/ViewedService.java b/backend/src/main/java/com/example/backend/viewed/service/ViewedService.java index 51810b3..67e2888 100644 --- a/backend/src/main/java/com/example/backend/viewed/service/ViewedService.java +++ b/backend/src/main/java/com/example/backend/viewed/service/ViewedService.java @@ -31,6 +31,11 @@ public class ViewedService { return repository.findById(id).orElseThrow(() -> new NotFoundException(ViewedEntity.class, id)); } + @Transactional + public ViewedEntity findByUserIdAndMovieId(Integer uId, Integer mId) { + return repository.findByUserIdAndMovieId(uId, mId); + } + @Transactional public ViewedEntity create(ViewedEntity entity) { return repository.save(entity); diff --git a/backend/src/main/resources/templates/card-category.html b/backend/src/main/resources/templates/card-category.html deleted file mode 100644 index 37ffe6b..0000000 --- a/backend/src/main/resources/templates/card-category.html +++ /dev/null @@ -1,19 +0,0 @@ - - - - - Категории - - - -
-
-
- - {categorie.name} -
-
-
- - - \ No newline at end of file diff --git a/backend/src/main/resources/templates/card-film.html b/backend/src/main/resources/templates/card-film.html new file mode 100644 index 0000000..24d6bd5 --- /dev/null +++ b/backend/src/main/resources/templates/card-film.html @@ -0,0 +1,27 @@ + + + + + Карточка фильма + + + +
+ + +
+
+ +

+

+

+ +
+ +
+ +
+ + + \ No newline at end of file diff --git a/backend/src/main/resources/templates/movies.html b/backend/src/main/resources/templates/movies.html index 9e63504..a83ca76 100644 --- a/backend/src/main/resources/templates/movies.html +++ b/backend/src/main/resources/templates/movies.html @@ -96,8 +96,10 @@ th:each="movie : ${movies}">
- + + +
@@ -109,8 +111,16 @@ method="post"> + +
+ +
diff --git a/data.mv.db b/data.mv.db index 58631d5..f5509a0 100644 Binary files a/data.mv.db and b/data.mv.db differ