-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor/#754 delete with outbox #758
Changes from all commits
a5e4479
125e8c3
b05e28e
1a334fe
e6c87c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package hanglog.outbox.domain; | ||
|
||
import static hanglog.outbox.domain.type.TargetType.MEMBER; | ||
import static hanglog.outbox.domain.type.TargetType.TRIP; | ||
import static jakarta.persistence.EnumType.STRING; | ||
import static jakarta.persistence.GenerationType.IDENTITY; | ||
import static lombok.AccessLevel.PROTECTED; | ||
|
||
import hanglog.global.BaseEntity; | ||
import hanglog.outbox.domain.type.TargetType; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Enumerated; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.hibernate.annotations.SQLDelete; | ||
import org.hibernate.annotations.Where; | ||
|
||
@Getter | ||
@Entity | ||
@NoArgsConstructor(access = PROTECTED) | ||
@SQLDelete(sql = "UPDATE trip_outbox SET status = 'DELETED' WHERE id = ?") | ||
@Where(clause = "status = 'USABLE'") | ||
public class OutBox extends BaseEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = IDENTITY) | ||
private Long id; | ||
|
||
@Column(nullable = false) | ||
private Long targetId; | ||
|
||
@Column(nullable = false) | ||
@Enumerated(value = STRING) | ||
private TargetType targetType; | ||
|
||
public OutBox(final Long targetId, final TargetType targetType) { | ||
this.targetId = targetId; | ||
this.targetType = targetType; | ||
} | ||
|
||
public boolean isMember() { | ||
return this.targetType.equals(MEMBER); | ||
} | ||
|
||
public boolean isTrip() { | ||
return this.targetType.equals(TRIP); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package hanglog.outbox.domain.repository; | ||
|
||
import hanglog.outbox.domain.OutBox; | ||
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; | ||
|
||
public interface OutBoxRepository extends JpaRepository<OutBox, Long> { | ||
|
||
@Modifying | ||
@Query(""" | ||
UPDATE OutBox outBox | ||
SET outBox.status = 'DELETED' | ||
WHERE outBox.targetId = :targetId | ||
""") | ||
void deleteByTargetId(@Param("targetId") final Long targetId); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package hanglog.outbox.domain.type; | ||
|
||
public enum TargetType { | ||
|
||
MEMBER, | ||
TRIP | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package hanglog.outbox.service; | ||
|
||
import hanglog.member.domain.MemberDeleteEvent; | ||
import hanglog.outbox.domain.OutBox; | ||
import hanglog.outbox.domain.repository.OutBoxRepository; | ||
import hanglog.trip.domain.TripDeleteEvent; | ||
import hanglog.trip.domain.repository.CustomTripRepository; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class OutBoxExecutor { | ||
|
||
private final OutBoxRepository outBoxRepository; | ||
private final CustomTripRepository customTripRepository; | ||
private final ApplicationEventPublisher publisher; | ||
|
||
@Scheduled(cron = "*/2 * * * * *") | ||
public void execute() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. νμμ΄ μ€μΌμ€λ§μμ λ°λ‘ λ°ννλκ΅°μ! νμ€ν μ§κΈμ μμκ° μ€μν μ΄λ²€νΈκ° μμΌλ λλ Έλ§λλ‘ μ΄ λ°©μμ΄ λ λͺ λ£ν΄λ³΄μ λλ€ |
||
final List<OutBox> outBoxes = outBoxRepository.findAll(); | ||
outBoxes.forEach(outBox -> { | ||
if (outBox.isMember()) { | ||
final List<Long> tripIds = customTripRepository.findTripIdsByMemberId(outBox.getTargetId()); | ||
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. outboxμ λͺ¨λ μ 보λ₯Ό μ μ₯νλ λμ νκ² λλ©μΈμ μμ΄λλ§ μ μ₯νκ³ , λλ¨Έμ§ μ 보λ λ€μ μ‘°νν΄μ κ°μ Έμ€λκ΅°μ! outboxμ μ§λ ¬νλ κ°μΌλ‘ μ 보 λ€ μ μ₯ν λ, μ΄κ±° 리μ€νΈλ μ§μ§ μ½€λ§λ‘ ꡬλΆν΄μ μ μ₯ν΄μΌνλ... rdbλμ λ€λ₯Έ dbλ₯Ό κ³ λ €ν΄λ΄μΌνλνκ³ νΌμ μ¬κ°ν κ³ λ―Όνκ³ μμλλ° κ·Έλ₯.......μ΄λ κ² μ°Ύμμ€λ©΄ λλκ±°μλ€μ μ λμ°¬μ±μ λλ€ |
||
publisher.publishEvent(new MemberDeleteEvent(tripIds, outBox.getTargetId())); | ||
} | ||
if (outBox.isTrip()) { | ||
publisher.publishEvent(new TripDeleteEvent(outBox.getTargetId())); | ||
} | ||
}); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
targetIdκ° κ²ΉμΉ μλ μμΌλ typeμ 보κΉμ§ ν¨κ» λ£μ΄μ€μΌκ² κ΅°μ