-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Good-Moneying/feature/6-login-event
[Feature] #6 로그인 이벤트 발행하기
- Loading branch information
Showing
11 changed files
with
215 additions
and
3 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
api-server/src/main/java/kusitms/duduk/apiserver/user/event/LoginEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package kusitms.duduk.apiserver.user.event; | ||
|
||
import kusitms.duduk.application.user.event.LoginUserEvent; | ||
import kusitms.duduk.core.user.port.input.AttendUserUseCase; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class LoginEventListener { | ||
|
||
private final AttendUserUseCase attendUserUseCase; | ||
|
||
@EventListener | ||
public void attendUser(LoginUserEvent event) { | ||
attendUserUseCase.attend(event.getEmail()); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
api-server/src/test/java/kusitms/duduk/apiserver/user/event/LoginEventListenerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package kusitms.duduk.apiserver.user.event; | ||
|
||
import static org.codehaus.groovy.runtime.DefaultGroovyMethods.any; | ||
|
||
import kusitms.duduk.application.attendence.persistence.AttendantRepository; | ||
import kusitms.duduk.application.attendence.persistence.entity.AttendantJpaEntity; | ||
import kusitms.duduk.application.user.event.LoginUserEvent; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
|
||
@SpringBootTest | ||
public class LoginEventListenerTest { | ||
|
||
@MockBean | ||
private LoginEventListener loginEventListener; | ||
|
||
@Autowired | ||
private ApplicationEventPublisher applicationEventPublisher; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
MockitoAnnotations.initMocks(this); | ||
// Ensure correct initialization | ||
} | ||
|
||
@Test | ||
public void 로그인_이벤트가_발생하면_리스너가_호출된다() { | ||
// given | ||
// Event 인스턴스를 생성한다 | ||
LoginUserEvent event = new LoginUserEvent(this, "test@test,com"); | ||
|
||
// when | ||
// 이벤트를 발행한다 | ||
applicationEventPublisher.publishEvent(event); | ||
|
||
// then | ||
// verify 메소드를 활요하여 리스너의 attendUser 메소드가 호출되었는지 확인한다 | ||
Mockito.verify(loginEventListener).attendUser(event); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...in/java/kusitms/duduk/application/attendence/persistence/AttendantPersistenceAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package kusitms.duduk.application.attendence.persistence; | ||
|
||
import java.time.LocalDate; | ||
import kusitms.duduk.application.attendence.persistence.entity.AttendantJpaEntity; | ||
import kusitms.duduk.core.annotation.Adapter; | ||
import kusitms.duduk.core.attendant.port.output.LoadAttendantPort; | ||
import kusitms.duduk.core.attendant.port.output.SaveAttendantPort; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Adapter | ||
public class AttendantPersistenceAdapter implements SaveAttendantPort, LoadAttendantPort { | ||
|
||
private final AttendantRepository attendantRepository; | ||
|
||
@Override | ||
public void save(LocalDate today, String email) { | ||
attendantRepository.save(AttendantJpaEntity.builder() | ||
.date(today) | ||
.email(email) | ||
.build()); | ||
} | ||
|
||
@Override | ||
public boolean isAttendedToday(String email) { | ||
return attendantRepository.existsByEmailAndDate(email, LocalDate.now()); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...n/src/main/java/kusitms/duduk/application/attendence/persistence/AttendantRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package kusitms.duduk.application.attendence.persistence; | ||
|
||
import java.time.LocalDate; | ||
import kusitms.duduk.application.attendence.persistence.entity.AttendantJpaEntity; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface AttendantRepository extends JpaRepository<AttendantJpaEntity, Long> { | ||
|
||
boolean existsByEmailAndDate(String email, LocalDate date); | ||
} |
31 changes: 31 additions & 0 deletions
31
...main/java/kusitms/duduk/application/attendence/persistence/entity/AttendantJpaEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package kusitms.duduk.application.attendence.persistence.entity; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import jakarta.persistence.UniqueConstraint; | ||
import java.time.LocalDate; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
@Entity | ||
@Table(name = "attendences", uniqueConstraints = { | ||
@UniqueConstraint(columnNames = {"date", "email"}) | ||
}) | ||
@Builder(toBuilder = true) | ||
public class AttendantJpaEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "attendence_id") | ||
private Long id; | ||
private LocalDate date; | ||
private String email; | ||
} |
26 changes: 26 additions & 0 deletions
26
...ication/src/main/java/kusitms/duduk/application/attendence/service/AttendUserCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package kusitms.duduk.application.attendence.service; | ||
|
||
import java.time.LocalDate; | ||
import kusitms.duduk.core.attendant.port.output.LoadAttendantPort; | ||
import kusitms.duduk.core.attendant.port.output.SaveAttendantPort; | ||
import kusitms.duduk.core.user.port.input.AttendUserUseCase; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Service | ||
public class AttendUserCommand implements AttendUserUseCase { | ||
|
||
private final SaveAttendantPort saveAttendantPort; | ||
private final LoadAttendantPort loadAttendantPort; | ||
|
||
@Override | ||
public void attend(String email) { | ||
if (!loadAttendantPort.isAttendedToday(email)) { | ||
log.info("attend user: {}", email); | ||
saveAttendantPort.save(LocalDate.now(), email); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
application/src/main/java/kusitms/duduk/application/user/event/LoginUserEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package kusitms.duduk.application.user.event; | ||
|
||
import lombok.Getter; | ||
import org.springframework.context.ApplicationEvent; | ||
|
||
@Getter | ||
public class LoginUserEvent extends ApplicationEvent { | ||
|
||
private final String email; | ||
|
||
public LoginUserEvent(Object source, String email) { | ||
super(source); | ||
this.email = email; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
core/src/main/java/kusitms/duduk/core/attendant/port/output/LoadAttendantPort.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package kusitms.duduk.core.attendant.port.output; | ||
|
||
import java.time.LocalDate; | ||
|
||
public interface LoadAttendantPort { | ||
boolean isAttendedToday(String email); | ||
|
||
// todo : 한 주에 몇번 출석 체크 했는지도 검증 가능 | ||
} |
7 changes: 7 additions & 0 deletions
7
core/src/main/java/kusitms/duduk/core/attendant/port/output/SaveAttendantPort.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package kusitms.duduk.core.attendant.port.output; | ||
|
||
import java.time.LocalDate; | ||
|
||
public interface SaveAttendantPort { | ||
void save(LocalDate date, String email); | ||
} |
6 changes: 6 additions & 0 deletions
6
core/src/main/java/kusitms/duduk/core/user/port/input/AttendUserUseCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package kusitms.duduk.core.user.port.input; | ||
|
||
public interface AttendUserUseCase { | ||
|
||
void attend(String email); | ||
} |