From 0b4210a15993ef170db2501fab493e099ca2f612 Mon Sep 17 00:00:00 2001 From: Subin <70842920+bin-pro@users.noreply.github.com> Date: Sun, 21 Jul 2024 14:47:44 +0900 Subject: [PATCH] Fix: logic in user-delete --- .../esthete/repository/FilterRepository.java | 5 +++++ .../repository/FilterTagRepository.java | 6 +----- .../esthete/repository/UserTagRepository.java | 5 +++++ .../service/kafka/KafkaUserInfoConsumer.java | 18 +++++------------- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/blackshoe/esthete/repository/FilterRepository.java b/src/main/java/com/blackshoe/esthete/repository/FilterRepository.java index 4a359d8..ea460b9 100644 --- a/src/main/java/com/blackshoe/esthete/repository/FilterRepository.java +++ b/src/main/java/com/blackshoe/esthete/repository/FilterRepository.java @@ -7,6 +7,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @@ -51,4 +52,8 @@ public interface FilterRepository extends JpaRepository{ @Query("SELECT f FROM Filter f WHERE f.user = :user") List findByUser(User user); + + @Modifying + @Query("DELETE FROM Filter f WHERE f.user = :user") + void deleteByUser(User user); } diff --git a/src/main/java/com/blackshoe/esthete/repository/FilterTagRepository.java b/src/main/java/com/blackshoe/esthete/repository/FilterTagRepository.java index 876f0c0..7266efc 100644 --- a/src/main/java/com/blackshoe/esthete/repository/FilterTagRepository.java +++ b/src/main/java/com/blackshoe/esthete/repository/FilterTagRepository.java @@ -19,11 +19,7 @@ public interface FilterTagRepository extends JpaRepository { @Query("DELETE FROM FilterTag ft WHERE ft.filter = :filter") void deleteByFilter(Filter filter); - @Modifying - @Query("DELETE FROM FilterTag ft WHERE ft.filter.user = :user") - void deleteByUserOfFilter(User user); - @Modifying @Query("DELETE FROM FilterTag ft WHERE ft.temporaryFilter.user = :user") - void deleteByUserOfTemporaryFilter(User user); + void deleteByUser(User user); } diff --git a/src/main/java/com/blackshoe/esthete/repository/UserTagRepository.java b/src/main/java/com/blackshoe/esthete/repository/UserTagRepository.java index 8a9c617..df98749 100644 --- a/src/main/java/com/blackshoe/esthete/repository/UserTagRepository.java +++ b/src/main/java/com/blackshoe/esthete/repository/UserTagRepository.java @@ -4,6 +4,7 @@ import com.blackshoe.esthete.entity.User; import com.blackshoe.esthete.entity.UserTag; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -17,4 +18,8 @@ public interface UserTagRepository extends JpaRepository { @Query("SELECT ut FROM UserTag ut JOIN FETCH ut.tag WHERE ut.user = :user") List findByUserWithTags(@Param("user") User user); + + @Modifying + @Query("DELETE FROM UserTag ut WHERE ut.user = :user") + void deleteByUser(User user); } diff --git a/src/main/java/com/blackshoe/esthete/service/kafka/KafkaUserInfoConsumer.java b/src/main/java/com/blackshoe/esthete/service/kafka/KafkaUserInfoConsumer.java index 05b237e..765ef36 100644 --- a/src/main/java/com/blackshoe/esthete/service/kafka/KafkaUserInfoConsumer.java +++ b/src/main/java/com/blackshoe/esthete/service/kafka/KafkaUserInfoConsumer.java @@ -6,10 +6,7 @@ import com.blackshoe.esthete.dto.KafkaConsumerDto; import com.blackshoe.esthete.entity.User; import com.blackshoe.esthete.exception.KafkaErrorResult; -import com.blackshoe.esthete.repository.FilterRepository; -import com.blackshoe.esthete.repository.FilterTagRepository; -import com.blackshoe.esthete.repository.TemporaryFilterRepository; -import com.blackshoe.esthete.repository.UserRepository; +import com.blackshoe.esthete.repository.*; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -30,7 +27,7 @@ public class KafkaUserInfoConsumer{ private final FilterRepository filterRepository; private final FilterTagRepository filterTagRepository; private final TemporaryFilterRepository temporaryFilterRepository; - + private final UserTagRepository userTagRepository; @KafkaListener(topics = "user-create") @Transactional public void createUser(String payload, Acknowledgment acknowledgment) { @@ -126,15 +123,10 @@ public void deleteUser(String payload, Acknowledgment acknowledgment) { UUID userId = UUID.fromString(userDeleteDto.getUserId()); final User user = userRepository.findByUserId(userId).orElseThrow(() -> new KafkaException(KafkaErrorResult.USER_NOT_FOUND)); - filterTagRepository.deleteByUserOfTemporaryFilter(user); + filterTagRepository.deleteByUser(user); temporaryFilterRepository.deleteByUser(user); - - filterTagRepository.deleteByUserOfFilter(user); - - List filters = user.getFilters(); - for (Filter filter : filters) { - filter.freeFilter(); - } + filterRepository.deleteByUser(user); + userTagRepository.deleteByUser(user); userRepository.delete(user);