ес.... работает добавление и удаление избранных и просмотренных

This commit is contained in:
Елена Бакальская 2024-06-21 01:32:57 +04:00
parent 0cf8d17cca
commit f1de853000
9 changed files with 105 additions and 30 deletions

View File

@ -36,12 +36,6 @@ public class FavoriteService {
return repository.findByUserIdAndMovieId(uId, mId);
}
// @Transactional(readOnly = true)
// public List<FavoriteEntity> getForAddToFavorite(Integer userId, Integer
// movieId) {
// return repository.findByUserIdAndMovieId(userId, movieId);
// }
@Transactional
public FavoriteEntity create(FavoriteEntity entity) {
return repository.save(entity);

View File

@ -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);

View File

@ -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;
}

View File

@ -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<ViewedEntity, Integer> {
@ -12,4 +13,8 @@ public interface ViewedRepository extends CrudRepository<ViewedEntity, Integer>
Optional<ViewedEntity> findOneByUserIdAndId(Integer userId, Integer id);
ViewedEntity findByUserIdAndMovieId(Integer userId, Integer movieId);
Optional<ViewedEntity> deleteByMovieId(Integer movieId);
}

View File

@ -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);

View File

@ -1,19 +0,0 @@
<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{categories}">
<head>
<title>Категории</title>
</head>
<body>
<main layout:fragment="content">
<form action="#" th:action="@{/admin/categories/}" method="post">
<div className="category-card d-flex flex-column justify-content-center align-items-center mb-5 mb-md-0">
<img src="/images/logo_light.png" className="rounded-3"></img>
<span className="category-name text-center mt-2">{categorie.name}</span>
</div>
</form>
</main>
</body>
</html>

View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{index}">
<head>
<title>Карточка фильма</title>
</head>
<body>
<main layout:fragment="content">
<!-- <form action="#" th:action="@{/movies/card/{id}(id=${movie.movieDTO.id})}" -->
<!-- th:with="movie=${#authentication.name}" method="get"> -->
<div class="d-flex flex-row justify-content-center align-items-center mb-5 mt-5" th:object="${movie}">
<div class="d-flex flex-column justify-content-center align-items-center">
<img th:src="|https://live.funnelmates.com/wp-content/uploads/2021/08/placeholder-200x200-1-1-1.jpeg|"
class="rounded-3 mt-3 mb-3"></img>
<p class="category-name text-center mt-2 mb-3" th:text="${movie.name}"></p>
<p class="category-name text-center mt-2 mb-3" th:text="${movie.description}"></p>
<p class="category-name text-center mt-2 mb-3" th:text="${movie.duration}"></p>
</div>
</div>
<!-- </form> -->
</main>
</body>
</html>

View File

@ -96,8 +96,10 @@
th:each="movie : ${movies}">
<div class="category-card">
<img class="card-img-top rounded-3"
th:src="|https://live.funnelmates.com/wp-content/uploads/2021/08/placeholder-200x200-1-1-1.jpeg|" />
<a th:href="@{/movies/card/{id}(id=${movie.movieDTO.id})}">
<img class="card-img-top rounded-3"
th:src="|https://live.funnelmates.com/wp-content/uploads/2021/08/placeholder-200x200-1-1-1.jpeg|" />
</a>
<div class="card-body">
<h5 class="card-title" th:text="${movie.movieDTO.name}"></h5>
</div>
@ -109,8 +111,16 @@
method="post">
<input type="hidden" th:name="page" th:value="${page}">
<button type="submit" class="btn btn-link button-link">
<i th:if="${movie.isFavorite}" class="bi bi-heart-fill"></i>
<i th:if="${!movie.isFavorite}" class="bi bi-heart"></i>
<i th:if="${movie.isFavorite}" class="heart-icon bi bi-heart-fill"></i>
<i th:if="${!movie.isFavorite}" class="heart-icon bi bi-heart"></i>
</button>
</form>
<form th:action="@{/movies/changeViewStatus/{id}(id=${movie.movieDTO.id})}"
method="post">
<input type="hidden" th:name="page" th:value="${page}">
<button type="submit" class="btn btn-link button-link">
<i th:if="${movie.isViewed}" class="eye-icon bi bi-eye-fill"></i>
<i th:if="${!movie.isViewed}" class="eye-icon bi bi-eye"></i>
</button>
</form>

Binary file not shown.