теперь еще и удаляется все грамотно

This commit is contained in:
Елена Бакальская 2024-06-21 01:37:26 +04:00
parent f1de853000
commit 8ac47b0fdf
3 changed files with 16 additions and 2 deletions

View File

@ -16,4 +16,6 @@ public interface FavoriteRepository extends CrudRepository<FavoriteEntity, Integ
Optional<FavoriteEntity> deleteByMovieId(Integer movieId);
Optional<FavoriteEntity> deleteByUserId(Integer user);
}

View File

@ -8,7 +8,6 @@ import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
@ -21,19 +20,27 @@ import java.util.stream.StreamSupport;
import com.example.backend.core.configurations.Constants;
import com.example.backend.core.errors.NotFoundException;
import com.example.backend.core.security.UserPrincipal;
import com.example.backend.favorites.repository.FavoriteRepository;
import com.example.backend.users.model.UserEntity;
import com.example.backend.users.model.UserRole;
import com.example.backend.users.repository.UserRepository;
import com.example.backend.viewed.repository.ViewedRepository;
@Service
public class UserService implements UserDetailsService {
private final UserRepository repository;
private final PasswordEncoder passwordEncoder;
private final FavoriteRepository favoriteRepository;
private final ViewedRepository viewedRepository;
public UserService(UserRepository repository, PasswordEncoder passwordEncoder) {
public UserService(UserRepository repository, PasswordEncoder passwordEncoder,
FavoriteRepository favoriteRepository,
ViewedRepository viewedRepository) {
this.repository = repository;
this.passwordEncoder = passwordEncoder;
this.favoriteRepository = favoriteRepository;
this.viewedRepository = viewedRepository;
}
private void checkLogin(Integer id, String login) {
@ -94,6 +101,9 @@ public class UserService implements UserDetailsService {
@Transactional
public UserEntity delete(Integer id) {
final UserEntity existsentity = get(id);
favoriteRepository.deleteByUserId(existsentity.getId());
viewedRepository.deleteByUserId(existsentity.getId());
repository.delete(existsentity);
return existsentity;
}

View File

@ -17,4 +17,6 @@ public interface ViewedRepository extends CrudRepository<ViewedEntity, Integer>
Optional<ViewedEntity> deleteByMovieId(Integer movieId);
Optional<ViewedEntity> deleteByUserId(Integer user);
}