категории создаются, удаляются, фильмы редактируются, починила удаление категорий - добавила динамический запрос для удаления всех связанных фильмов

This commit is contained in:
Елена Бакальская 2024-06-17 21:09:08 +04:00
parent ae0b310be9
commit 1fe72328a0
4 changed files with 9 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import com.example.backend.core.errors.NotFoundException; import com.example.backend.core.errors.NotFoundException;
import com.example.backend.movies.repository.MovieRepository;
import com.example.backend.categories.model.CategorieEntity; import com.example.backend.categories.model.CategorieEntity;
import com.example.backend.categories.repository.CategorieRepository; import com.example.backend.categories.repository.CategorieRepository;
@ -15,9 +16,11 @@ import com.example.backend.categories.repository.CategorieRepository;
public class CategorieService { public class CategorieService {
private final CategorieRepository repository; private final CategorieRepository repository;
private final MovieRepository movieRepository;
public CategorieService(CategorieRepository repository) { public CategorieService(CategorieRepository repository, MovieRepository movieRepository) {
this.repository = repository; this.repository = repository;
this.movieRepository = movieRepository;
} }
private void checkName(String name) { private void checkName(String name) {
@ -57,6 +60,7 @@ public class CategorieService {
@Transactional @Transactional
public CategorieEntity delete(Integer id) { public CategorieEntity delete(Integer id) {
final CategorieEntity existsentity = get(id); final CategorieEntity existsentity = get(id);
movieRepository.deleteByCategorieId(existsentity.getId());
repository.delete(existsentity); repository.delete(existsentity);
return existsentity; return existsentity;
} }

View File

@ -110,6 +110,7 @@ public class MovieController {
return MOVIE_EDIT_VIEW; return MOVIE_EDIT_VIEW;
} }
model.addAttribute("categories", categorieService.getAll()); model.addAttribute("categories", categorieService.getAll());
model.addAttribute(CATEGORIEID_ATTRIBUTE, 0);
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page); redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
movieService.create(toEntity(movieDTO)); movieService.create(toEntity(movieDTO));
return Constants.REDIRECT_VIEW + URL; return Constants.REDIRECT_VIEW + URL;
@ -121,6 +122,7 @@ public class MovieController {
if (id <= 0) { if (id <= 0) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
model.addAttribute("categories", categorieService.getAll());
model.addAttribute(MOVIE_ATTRIBUTE, toDto(movieService.get(id))); model.addAttribute(MOVIE_ATTRIBUTE, toDto(movieService.get(id)));
model.addAttribute(PAGE_ATTRIBUTE, page); model.addAttribute(PAGE_ATTRIBUTE, page);
return MOVIE_EDIT_VIEW; return MOVIE_EDIT_VIEW;

View File

@ -20,6 +20,8 @@ public interface MovieRepository
Page<MovieEntity> findByCategorieId(Integer categorieId, Pageable page); Page<MovieEntity> findByCategorieId(Integer categorieId, Pageable page);
Optional<MovieEntity> deleteByCategorieId(Integer categorieId);
@Query("SELECT COUNT(DISTINCT v.user.id) FROM ViewedEntity v WHERE v.movie.id = :movieId") @Query("SELECT COUNT(DISTINCT v.user.id) FROM ViewedEntity v WHERE v.movie.id = :movieId")
Integer getCountViews(@Param("movieId") Integer movieId); Integer getCountViews(@Param("movieId") Integer movieId);

Binary file not shown.