оаоао ао ао о а о а работает добавлене в избранное неужели аааоаоаоа

This commit is contained in:
Елена Бакальская 2024-06-21 00:36:24 +04:00
parent 463296c358
commit 0cf8d17cca
8 changed files with 157 additions and 86 deletions

View File

@ -89,13 +89,15 @@ public class BackendApplication implements CommandLineRunner {
// categorieService.create(cat4);
// categorieService.create(cat5);
// movieService.create(mov1);
favoriyService.create(new FavoriteEntity(null, userService.get(2), movieService.get(98)));
// movieService.get(98));
// for (var fav : favoriyService.getAll(2)) {
// favoriyService.delete(fav.getId());
// }
// favoriyService.create(new FavoriteEntity(null, userService.get(2),
// movieService.get(98)));
_logger.info("The program is started");
_logger.info("The program is started, coun favs is " + favoriyService.getAll(2).size());
}
}

View File

@ -10,7 +10,10 @@ public interface FavoriteRepository extends CrudRepository<FavoriteEntity, Integ
List<FavoriteEntity> findByUserId(Integer userId);
List<FavoriteEntity> findByUserIdAndMovieId(Integer userId, Integer movieId);
FavoriteEntity findByUserIdAndMovieId(Integer userId, Integer movieId);
Optional<FavoriteEntity> findOneByUserIdAndId(Integer userId, Integer id);
Optional<FavoriteEntity> deleteByMovieId(Integer movieId);
}

View File

@ -31,6 +31,11 @@ public class FavoriteService {
return repository.findById(id).orElseThrow(() -> new NotFoundException(FavoriteEntity.class, id));
}
@Transactional
public FavoriteEntity findByUserIdAndMovieId(Integer uId, Integer mId) {
return repository.findByUserIdAndMovieId(uId, mId);
}
// @Transactional(readOnly = true)
// public List<FavoriteEntity> getForAddToFavorite(Integer userId, Integer
// movieId) {

View File

@ -1,6 +1,7 @@
package com.example.backend.movies.api;
import java.util.List;
import java.util.ArrayList;
import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.RequestMapping;
@ -12,7 +13,10 @@ import com.example.backend.favorites.model.FavoriteEntity;
import com.example.backend.favorites.service.FavoriteService;
import com.example.backend.movies.model.MovieEntity;
import com.example.backend.movies.service.MovieService;
import com.example.backend.users.model.UserEntity;
import com.example.backend.users.service.UserService;
import com.example.backend.viewed.model.ViewedEntity;
import com.example.backend.viewed.service.ViewedService;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
@ -38,11 +42,13 @@ public class MovieUserController {
private static final String PAGE_ATTRIBUTE = "page";
private static final String MOVIE_ATTRIBUTE = "movie";
private static final String CATEGORIEID_ATTRIBUTE = "categorieId";
private static final String MOVEID_ATTRIBUTE = "movieId";
private final MovieService movieService;
private final CategorieService categorieService;
private final UserService userService;
private final FavoriteService favoriteService;
private final ViewedService viewedServise;
private final ModelMapper modelMapper;
@ -50,12 +56,14 @@ public class MovieUserController {
CategorieService categorieService,
ModelMapper modelMapper,
UserService userService,
FavoriteService favoriteService) {
FavoriteService favoriteService,
ViewedService viewedServise) {
this.modelMapper = modelMapper;
this.categorieService = categorieService;
this.movieService = movieService;
this.userService = userService;
this.favoriteService = favoriteService;
this.viewedServise = viewedServise;
}
private MovieDTO toDto(MovieEntity entity) {
@ -68,23 +76,53 @@ public class MovieUserController {
return entity;
}
@GetMapping()
public String getAll(@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
Model model) {
model.addAttribute("movies",
movieService.getAll(0, page, Constants.DEFUALT_PAGE_SIZE)
.stream()
.map(this::toDto)
.toList());
model.addAttribute(CATEGORIEID_ATTRIBUTE, model);
model.addAttribute("categories", categorieService.getAll());
private List<MovieUserDTO> getListMovieUserDTOs(Integer categorieId, int page) {
List<MovieDTO> movies = movieService.getAll(categorieId, page, Constants.DEFUALT_PAGE_SIZE)
.stream()
.map(this::toDto)
.toList();
List<MovieUserDTO> muDTO = new ArrayList<MovieUserDTO>();
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Integer currentUserId = userService.getCurrentUserId(authentication.getName());
List<FavoriteEntity> favs = favoriteService.getAll(currentUserId);
List<ViewedEntity> viewes = viewedServise.getAll(currentUserId);
model.addAttribute("favorites", favs);
for (MovieDTO movieDTO : movies) {
MovieUserDTO newMovDto = new MovieUserDTO();
newMovDto.setMovieDTO(movieDTO);
for (FavoriteEntity favoriteEntity : favs) {
if ((int) favoriteEntity.getMovie().getId() == (int) movieDTO.getId()) {
newMovDto.setFavorite(true);
break;
}
newMovDto.setFavorite(false);
}
for (ViewedEntity viewedEntity : viewes) {
if ((int) viewedEntity.getMovie().getId() == (int) movieDTO.getId()) {
newMovDto.setViewed(true);
break;
}
newMovDto.setViewed(false);
}
muDTO.add(newMovDto);
}
return muDTO;
}
@GetMapping()
public String getAll(@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
Model model) {
List<MovieUserDTO> muDTO = getListMovieUserDTOs(0, page);
model.addAttribute("movies", muDTO);
model.addAttribute(CATEGORIEID_ATTRIBUTE, model);
model.addAttribute("categories", categorieService.getAll());
model.addAttribute(PAGE_ATTRIBUTE, page);
model.addAttribute(CATEGORIEID_ATTRIBUTE, 0);
return MOVIE_VIEW;
}
@ -93,27 +131,53 @@ public class MovieUserController {
public String getAll(@PathVariable(name = "id") Integer categorieId,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
Model model) {
model.addAttribute("movies",
movieService.getAll(categorieId, page, Constants.DEFUALT_PAGE_SIZE)
.stream()
.map(this::toDto)
.toList());
List<MovieUserDTO> muDTO = getListMovieUserDTOs(categorieId, page);
model.addAttribute("movies", muDTO);
model.addAttribute("categories", categorieService.getAll());
model.addAttribute(PAGE_ATTRIBUTE, page);
model.addAttribute(CATEGORIEID_ATTRIBUTE, categorieId);
return MOVIE_VIEW;
}
@PostMapping("/changeFavStatus/{id}")
public String changeFavStatus(@PathVariable(name = "id") Integer movieId,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
Model model) {
boolean isFavorite = false;
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Integer currentUserId = userService.getCurrentUserId(authentication.getName());
List<FavoriteEntity> favs = favoriteService.getAll(currentUserId);
FavoriteEntity fav = favoriteService.findByUserIdAndMovieId(currentUserId, movieId);
if (fav == null) {
isFavorite = false;
} else {
isFavorite = true;
}
if (isFavorite == false) {
UserEntity user = userService.get(currentUserId);
MovieEntity movie = movieService.get(movieId);
FavoriteEntity newFav = new FavoriteEntity(null, user, movie);
favoriteService.create(newFav);
} else {
FavoriteEntity delFav = favoriteService.findByUserIdAndMovieId(currentUserId, movieId);
favoriteService.delete(delFav.getId());
}
model.addAttribute("favorites", favs);
model.addAttribute(PAGE_ATTRIBUTE, page);
model.addAttribute(CATEGORIEID_ATTRIBUTE, categorieId);
return MOVIE_VIEW;
}
@GetMapping("/addToFavorite/{id}")
public String changeStatusForFavorite() {
return Constants.REDIRECT_VIEW + URL;
}

View File

@ -0,0 +1,39 @@
package com.example.backend.movies.api;
import jakarta.validation.constraints.NotBlank;
public class MovieUserDTO {
MovieDTO movieDTO;
@NotBlank
private boolean isFavorite;
@NotBlank
private boolean isViewed;
public MovieDTO getMovieDTO() {
return movieDTO;
}
public void setMovieDTO(MovieDTO movieDTO) {
this.movieDTO = movieDTO;
}
public boolean isFavorite() {
return isFavorite;
}
public void setFavorite(boolean isFavorite) {
this.isFavorite = isFavorite;
}
public boolean isViewed() {
return isViewed;
}
public void setViewed(boolean isViewed) {
this.isViewed = isViewed;
}
}

View File

@ -7,6 +7,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
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;
@ -14,9 +15,11 @@ import com.example.backend.movies.repository.MovieRepository;
public class MovieService {
private final MovieRepository repository;
private final FavoriteRepository favoriteRepository;
public MovieService(MovieRepository repository) {
public MovieService(MovieRepository repository, FavoriteRepository favoriteRepository) {
this.repository = repository;
this.favoriteRepository = favoriteRepository;
}
private void checkName(String name) {
@ -67,6 +70,7 @@ public class MovieService {
@Transactional
public MovieEntity delete(Integer id) {
final MovieEntity exisEntity = get(id);
favoriteRepository.deleteByMovieId(exisEntity.getId());
repository.delete(exisEntity);
return exisEntity;
}

View File

@ -91,83 +91,37 @@
<h2 th:case="0">Данные отсутствуют</h2>
<th:block th:case="*">
<!--
<th:block th:switch="${viewes.size()}">
<h2 th:case="0">
д етить колотить
</h2>
<th:block th:case="*">
</th:block>
</th:block> -->
<div class="categories-container d-flex flex-row justify-content-center mt-3 mb-3">
<div class="category-item d-flex flex-column justify-content-center align-items-center mb-5 mb-md-0"
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|" />
<div class="card-body">
<h5 class="card-title" th:text="${movie.name}"></h5>
<h5 class="card-title" th:text="${movie.movieDTO.name}"></h5>
</div>
</div>
<div class="d-flex flex-row ms-mt-2">
<th:block th:switch="${favorites.size()}">
<h2 th:case="0">
д етить колотить
</h2>
<form th:action="@{/movies/changeFavStatus/{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.isFavorite}" class="bi bi-heart-fill"></i>
<i th:if="${!movie.isFavorite}" class="bi bi-heart"></i>
</button>
</form>
<th:block th:case="*">
<form th:action="@{/movies/addToFavorite/{id}(id=${movie.id})}" method="get">
<input type="hidden" th:name="page" th:value="${page}">
<!-- <div th:each="fav : ${favorites}"> -->
<button type="submit" class="btn btn-link button-link"
th:each="fav : ${favorites}">
<svg th:if="${fav.movie.id} == ${movie.id}"
xmlns="http://www.w3.org/2000/svg" width="16" height="16"
fill="currentColor" class="bi bi-heart-fill" viewBox="0 0 16 16">
<path fill-rule="evenodd"
d="M8 1.314C12.438-3.248 23.534 4.735 8 15-7.534 4.736 3.562-3.248 8 1.314" />
</svg>
<svg th:if="${fav.movie.id} != ${movie.id}"
xmlns="http://www.w3.org/2000/svg" width="16" height="16"
fill="currentColor" class="bi bi-heart" viewBox="0 0 16 16">
<path fill-rule="evenodd"
d="M8 1.314C12.438-3.248 23.534 4.735 8 15-7.534 4.736 3.562-3.248 8 1.314" />
</svg>
</button>
<!-- </div> -->
</form>
</th:block>
</th:block>
</div>
</div>
</div>
</th:block>
</th:block>
<!-- <form th:action="@{/movies/delete/{id}(id=${movie.id})}" method="post"> -->
<!-- <input type="hidden" th:name="page" th:value="${page}">
<button type="submit" class="btn btn-link button-link" onclick="return confirm('Вы уверены?')">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
class="bi bi-eye-fill" viewBox="0 0 16 16">
<path d="M10.5 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 0 1 5 0" />
<path
d="M0 8s3-5.5 8-5.5S16 8 16 8s-3 5.5-8 5.5S0 8 0 8m8 3.5a3.5 3.5 0 1 0 0-7 3.5 3.5 0 0 0 0 7" />
</svg>
</button> -->
<!-- </form> -->
<!-- </div>
</div>
</div> -->
</th:block>
<th:block th:replace="~{ pagination :: pagination (

Binary file not shown.