diff --git a/src/main/java/com/example/daobe/objet/application/ObjetEventListener.java b/src/main/java/com/example/daobe/objet/application/ObjetEventListener.java index dcced378..d5c85d0e 100644 --- a/src/main/java/com/example/daobe/objet/application/ObjetEventListener.java +++ b/src/main/java/com/example/daobe/objet/application/ObjetEventListener.java @@ -1,8 +1,11 @@ package com.example.daobe.objet.application; import com.example.daobe.lounge.domain.event.LoungeDeleteEvent; +import com.example.daobe.lounge.domain.event.LoungeWithdrawEvent; import com.example.daobe.objet.domain.Objet; +import com.example.daobe.objet.domain.ObjetSharer; import com.example.daobe.objet.domain.repository.ObjetRepository; +import com.example.daobe.objet.domain.repository.ObjetSharerRepository; import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -19,13 +22,26 @@ public class ObjetEventListener { private final ObjetRepository objetRepository; + private final ObjetSharerRepository objetSharerRepository; @Async @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) @Transactional(propagation = Propagation.REQUIRES_NEW) public void loungeDeletedListener(LoungeDeleteEvent event) { List objetList = objetRepository.findActiveObjetListInLounge(event.loungeId()); - objetList.forEach(Objet::updateDeleteStatus); + objetList.forEach(Objet::updateStatusDeleted); objetRepository.saveAll(objetList); } + + @Async + @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) + @Transactional(propagation = Propagation.REQUIRES_NEW) + public void loungeWithdrawListener(LoungeWithdrawEvent event) { + List objetSharerList = objetSharerRepository.findByUserIdAndLoungeId( + event.userId(), + event.loungeId() + ); + objetSharerList.forEach(ObjetSharer::updateStatusDeleted); + objetSharerRepository.saveAll(objetSharerList); + } } diff --git a/src/main/java/com/example/daobe/objet/application/ObjetSharerService.java b/src/main/java/com/example/daobe/objet/application/ObjetSharerService.java index c1aab279..5436afb0 100644 --- a/src/main/java/com/example/daobe/objet/application/ObjetSharerService.java +++ b/src/main/java/com/example/daobe/objet/application/ObjetSharerService.java @@ -72,7 +72,7 @@ public void updateObjetSharerList(Objet objet, List sharerIdList) { @Transactional public ObjetSharerResponseDto getObjetSharerList(Long objetId) { - List objetSharerList = objetSharerRepository.findAllByObjetId(objetId); + List objetSharerList = objetSharerRepository.findAllByObjetIdAndActiveStatus(objetId); return ObjetSharerResponseDto.of(objetSharerList); } diff --git a/src/main/java/com/example/daobe/objet/domain/Objet.java b/src/main/java/com/example/daobe/objet/domain/Objet.java index 4d54480b..24398e2a 100644 --- a/src/main/java/com/example/daobe/objet/domain/Objet.java +++ b/src/main/java/com/example/daobe/objet/domain/Objet.java @@ -95,7 +95,7 @@ public void softDelete(Long userId) { this.status = ObjetStatus.DELETED; } - public void updateDeleteStatus() { + public void updateStatusDeleted() { this.status = ObjetStatus.DELETED; } diff --git a/src/main/java/com/example/daobe/objet/domain/ObjetSharer.java b/src/main/java/com/example/daobe/objet/domain/ObjetSharer.java index 23080b1f..a7b0fbc6 100644 --- a/src/main/java/com/example/daobe/objet/domain/ObjetSharer.java +++ b/src/main/java/com/example/daobe/objet/domain/ObjetSharer.java @@ -1,8 +1,13 @@ package com.example.daobe.objet.domain; +import static com.example.daobe.objet.domain.ObjetSharerStatus.ACTIVE; +import static com.example.daobe.objet.domain.ObjetSharerStatus.DELETED; + import com.example.daobe.user.domain.User; import jakarta.persistence.Column; import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; @@ -34,9 +39,25 @@ public class ObjetSharer { @ManyToOne(fetch = FetchType.LAZY) private Objet objet; + @Enumerated(EnumType.STRING) + private ObjetSharerStatus status; + @Builder public ObjetSharer(User user, Objet objet) { this.user = user; this.objet = objet; + this.status = ACTIVE; + } + + public boolean isActive() { + return status.isActive(); + } + + public void updateStatusActive() { + this.status = ACTIVE; + } + + public void updateStatusDeleted() { + this.status = DELETED; } } diff --git a/src/main/java/com/example/daobe/objet/domain/ObjetSharerStatus.java b/src/main/java/com/example/daobe/objet/domain/ObjetSharerStatus.java new file mode 100644 index 00000000..bbb4add9 --- /dev/null +++ b/src/main/java/com/example/daobe/objet/domain/ObjetSharerStatus.java @@ -0,0 +1,11 @@ +package com.example.daobe.objet.domain; + +public enum ObjetSharerStatus { + DELETED, + ACTIVE, + ; + + public boolean isActive() { + return this == ACTIVE; + } +} diff --git a/src/main/java/com/example/daobe/objet/domain/repository/ObjetSharerRepository.java b/src/main/java/com/example/daobe/objet/domain/repository/ObjetSharerRepository.java index f88cabbf..60b0f5ed 100644 --- a/src/main/java/com/example/daobe/objet/domain/repository/ObjetSharerRepository.java +++ b/src/main/java/com/example/daobe/objet/domain/repository/ObjetSharerRepository.java @@ -12,17 +12,25 @@ public interface ObjetSharerRepository extends JpaRepository @Query(""" SELECT os FROM ObjetSharer os JOIN FETCH Objet o ON os.objet.id = o.id - WHERE o.status = 'ACTIVE' AND os.user.id = :userId + WHERE o.status = 'ACTIVE' AND os.status='ACTIVE' AND os.user.id = :userId ORDER BY o.createdAt DESC LIMIT 4 """) List findTop4ByUserIdAndActiveStatus(Long userId); - List findAllByObjetId(Long objetId); + @Query("SELECT os FROM ObjetSharer os WHERE os.id=:objetId AND os.status = 'ACTIVE'") + List findAllByObjetIdAndActiveStatus(Long objetId); - @Query("SELECT os.user.id FROM ObjetSharer os WHERE os.objet = :objet") + @Query("SELECT os.user.id FROM ObjetSharer os WHERE os.objet = :objet AND os.status = 'ACTIVE'") List findSharerIdListByObjet(@Param("objet") Objet objet); void deleteAllByObjetAndUserIdIn(Objet objet, List userIds); + @Query(""" + SELECT os FROM ObjetSharer os JOIN FETCH Objet o + ON os.objet.id = o.id + WHERE os.user.id = :userId AND o.lounge.id = :loungeId AND os.status = 'ACTIVE' + """) + List findByUserIdAndLoungeId(Long userId, Long loungeId); + } diff --git a/src/main/java/com/example/daobe/objet/infrastructure/querydsl/QueryDslCustomObjetRepository.java b/src/main/java/com/example/daobe/objet/infrastructure/querydsl/QueryDslCustomObjetRepository.java index 3a71a368..47d34ae7 100644 --- a/src/main/java/com/example/daobe/objet/infrastructure/querydsl/QueryDslCustomObjetRepository.java +++ b/src/main/java/com/example/daobe/objet/infrastructure/querydsl/QueryDslCustomObjetRepository.java @@ -4,6 +4,7 @@ import static com.example.daobe.objet.domain.QObjetSharer.objetSharer; import com.example.daobe.objet.domain.Objet; +import com.example.daobe.objet.domain.ObjetSharerStatus; import com.example.daobe.objet.domain.ObjetStatus; import com.example.daobe.objet.domain.repository.CustomObjetRepository; import com.example.daobe.objet.domain.repository.dto.ObjetFindCondition; @@ -35,7 +36,10 @@ public Slice getObjetListByCondition(ObjetFindCondition condition) { List result = (condition.userId() != null) ? query.join(objetSharer).on(objetSharer.objet.eq(objet)) - .where(objetSharer.user.id.eq(condition.userId())).fetch() + .where( + objetSharer.user.id.eq(condition.userId()), + objetSharer.status.eq(ObjetSharerStatus.ACTIVE) + ).fetch() : query.fetch(); boolean hasNext = false; diff --git a/src/main/resources/db/migration/V4__add_status_in_objets_sharer_status.sql b/src/main/resources/db/migration/V4__add_status_in_objets_sharer_status.sql new file mode 100644 index 00000000..4ea1f054 --- /dev/null +++ b/src/main/resources/db/migration/V4__add_status_in_objets_sharer_status.sql @@ -0,0 +1,2 @@ +ALTER TABLE objets_sharers + ADD COLUMN status varchar(255);