Skip to content

Commit

Permalink
Feat: 허브 태그 자동 생성 이벤트 리스너를 구현한다.
Browse files Browse the repository at this point in the history
Feat: 허브 태그 자동 생성 이벤트 리스너를 구현한다.
  • Loading branch information
hseong3243 authored Apr 9, 2024
2 parents 816ac2a + e2ed74c commit 80721cf
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import com.seong.shoutlink.domain.hubmember.service.HubMemberRepository;
import com.seong.shoutlink.domain.link.Link;
import com.seong.shoutlink.domain.link.LinkWithLinkBundle;
import com.seong.shoutlink.domain.link.service.event.CreateLinkEvent;
import com.seong.shoutlink.domain.link.service.event.CreateHubLinkEvent;
import com.seong.shoutlink.domain.link.service.event.CreateMemberLinkEvent;
import com.seong.shoutlink.domain.link.service.request.CreateHubLinkCommand;
import com.seong.shoutlink.domain.link.service.request.CreateLinkCommand;
import com.seong.shoutlink.domain.link.service.request.FindHubLinksCommand;
Expand Down Expand Up @@ -45,7 +46,8 @@ public CreateLinkResponse createLink(CreateLinkCommand command) {
Link link = new Link(command.url(), command.description());
LinkWithLinkBundle linkWithLinkBundle = new LinkWithLinkBundle(link, linkBundle);
Long linkId = linkRepository.save(linkWithLinkBundle);
eventPublisher.publishEvent(new CreateLinkEvent(linkId, command.url()));
eventPublisher.publishEvent(
new CreateMemberLinkEvent(linkId, link.getUrl(), member.getMemberId()));
return new CreateLinkResponse(linkId);
}

Expand Down Expand Up @@ -78,7 +80,7 @@ public CreateHubLinkResponse createHubLink(CreateHubLinkCommand command) {
LinkBundle hubLinkBundle = getHubLinkBundle(command.linkBundleId(), hub);
Link link = new Link(command.url(), command.description());
Long linkId = linkRepository.save(new LinkWithLinkBundle(link, hubLinkBundle));
eventPublisher.publishEvent(new CreateLinkEvent(linkId, command.url()));
eventPublisher.publishEvent(new CreateHubLinkEvent(linkId, link.getUrl(), hub.getHubId()));
return new CreateHubLinkResponse(linkId);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.seong.shoutlink.domain.link.service.event;

import java.util.Objects;

public class CreateHubLinkEvent extends CreateLinkEvent {

private final Long hubId;

public CreateHubLinkEvent(Long linkId, String url, Long hubId) {
super(linkId, url);
this.hubId = hubId;
}

public Long hubId() {
return hubId;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
CreateHubLinkEvent that = (CreateHubLinkEvent) o;
return Objects.equals(hubId, that.hubId);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), hubId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

import com.seong.shoutlink.domain.common.Event;

public record CreateLinkEvent(Long linkId, String url) implements Event {
public abstract class CreateLinkEvent implements Event {

private final Long linkId;
private final String url;

protected CreateLinkEvent(Long linkId, String url) {
this.linkId = linkId;
this.url = url;
}

public Long linkId() {
return linkId;
}

public String url() {
return url;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.seong.shoutlink.domain.link.service.event;

import java.util.Objects;

public class CreateMemberLinkEvent extends CreateLinkEvent{

private final Long memberId;

public CreateMemberLinkEvent(Long linkId, String url, Long memberId) {
super(linkId, url);
this.memberId = memberId;
}

public Long memberId() {
return memberId;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
CreateMemberLinkEvent that = (CreateMemberLinkEvent) o;
return Objects.equals(memberId, that.memberId);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), memberId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.seong.shoutlink.domain.common.EventPublisher;
import com.seong.shoutlink.domain.domain.service.DomainService;
import com.seong.shoutlink.domain.linkbundle.service.LinkBundleService;
import com.seong.shoutlink.domain.tag.service.TagService;
import com.seong.shoutlink.global.event.DomainEventListener;
import com.seong.shoutlink.global.event.LinkBundleEventListener;
import com.seong.shoutlink.global.event.SpringEventPublisher;
import com.seong.shoutlink.global.event.TagEventListener;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -19,12 +21,17 @@ public EventPublisher eventPublisher(ApplicationEventPublisher applicationEventP
}

@Bean
public LinkBundleEventListener springEventListener(LinkBundleService linkBundleService) {
public LinkBundleEventListener linkBundleEventListener(LinkBundleService linkBundleService) {
return new LinkBundleEventListener(linkBundleService);
}

@Bean
public DomainEventListener domainEventListener(DomainService domainService) {
return new DomainEventListener(domainService);
}

@Bean
public TagEventListener tagEventListener(TagService tagService) {
return new TagEventListener(tagService);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.seong.shoutlink.global.event;

import com.seong.shoutlink.domain.exception.ErrorCode;
import com.seong.shoutlink.domain.exception.ShoutLinkException;
import com.seong.shoutlink.domain.link.service.event.CreateHubLinkEvent;
import com.seong.shoutlink.domain.tag.service.TagService;
import com.seong.shoutlink.domain.tag.service.request.AutoCreateTagCommand;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;

@Slf4j
@RequiredArgsConstructor
public class TagEventListener {

private final TagService tagService;

@TransactionalEventListener
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void createHubTags(CreateHubLinkEvent event) {
AutoCreateTagCommand command = new AutoCreateTagCommand(event.hubId());
try {
tagService.autoCreateHubTags(command);
log.debug("[Tag] 링크 개수가 최소 태그 자동 생성 조건을 만족");
} catch (ShoutLinkException e) {
if(e.getErrorCode().equals(ErrorCode.NOT_MET_CONDITION)) {
log.debug("[Tag] 링크 개수가 최소 태그 자동 생성 조건을 만족하지 않음");
} else {
throw e;
}
}
}
}

0 comments on commit 80721cf

Please sign in to comment.