diff --git a/backend/src/main/java/com/example/backend/favorites/repository/FavoriteRepository.java b/backend/src/main/java/com/example/backend/favorites/repository/FavoriteRepository.java index 897c8c9..667dfea 100644 --- a/backend/src/main/java/com/example/backend/favorites/repository/FavoriteRepository.java +++ b/backend/src/main/java/com/example/backend/favorites/repository/FavoriteRepository.java @@ -16,4 +16,6 @@ public interface FavoriteRepository extends CrudRepository deleteByMovieId(Integer movieId); + Optional deleteByUserId(Integer user); + } diff --git a/backend/src/main/java/com/example/backend/users/service/UserService.java b/backend/src/main/java/com/example/backend/users/service/UserService.java index 900e891..f21be34 100644 --- a/backend/src/main/java/com/example/backend/users/service/UserService.java +++ b/backend/src/main/java/com/example/backend/users/service/UserService.java @@ -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; } 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 fbad52f..604c54e 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 @@ -17,4 +17,6 @@ public interface ViewedRepository extends CrudRepository Optional deleteByMovieId(Integer movieId); + Optional deleteByUserId(Integer user); + }