-
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 #32 from seyedali-dev/feature/event-project-kafka
Feature/event project kafka done! (Close #14)
- Loading branch information
Showing
10 changed files
with
194 additions
and
3 deletions.
There are no files selected for viewing
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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/seyed/ali/timeentryservice/event/ProjectEventListener.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,33 @@ | ||
package com.seyed.ali.timeentryservice.event; | ||
|
||
import com.seyed.ali.timeentryservice.model.enums.OperationType; | ||
import com.seyed.ali.timeentryservice.model.payload.ProjectDTO; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.springframework.kafka.annotation.KafkaListener; | ||
import org.springframework.messaging.handler.annotation.Header; | ||
import org.springframework.messaging.handler.annotation.Payload; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class ProjectEventListener { | ||
|
||
private final ProjectEventService projectEventService; | ||
|
||
@KafkaListener( | ||
topics = "${spring.kafka.topic.name}", | ||
groupId = "${spring.kafka.consumer.group-id}" // if we change the group-id name, the events from the previous data will also be logged, otherwise, only the new events will be logged! | ||
) | ||
public void handleProjectEvent(@Payload ConsumerRecord<String, ProjectDTO> record, @Header("OperationType") String operationType) { | ||
ProjectDTO projectDTO = record.value(); | ||
switch (OperationType.valueOf(operationType)) { | ||
case DELETE -> this.projectEventService.handleDeleteOperation(projectDTO); | ||
case DETACH -> this.projectEventService.handleDetachOperation(projectDTO); | ||
default -> log.warn("\"{}\" operation type not supported.", operationType); | ||
} | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/seyed/ali/timeentryservice/event/ProjectEventService.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,36 @@ | ||
package com.seyed.ali.timeentryservice.event; | ||
|
||
import com.seyed.ali.timeentryservice.model.payload.ProjectDTO; | ||
import com.seyed.ali.timeentryservice.repository.TimeEntryRepository; | ||
import com.seyed.ali.timeentryservice.service.interfaces.TimeEntryService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Optional; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class ProjectEventService { | ||
|
||
private final TimeEntryService timeEntryService; | ||
private final TimeEntryRepository timeEntryRepository; | ||
|
||
public void handleDeleteOperation(ProjectDTO projectDTO) { | ||
log.info("\"Delete\" project, associated tasks & time entries: {}", projectDTO); | ||
this.timeEntryRepository.findByProjectId(projectDTO.getProjectId()) | ||
.forEach(this.timeEntryService::deleteTimeEntry); | ||
} | ||
|
||
public void handleDetachOperation(ProjectDTO projectDTO) { | ||
log.info("\"Detach\" project, associated tasks & time entries: {}", projectDTO); | ||
this.timeEntryRepository.findByProjectId(projectDTO.getProjectId()) | ||
.forEach(timeEntry -> { | ||
timeEntry.setProjectId(null); | ||
timeEntry.setTaskId(null); | ||
this.timeEntryRepository.save(timeEntry); | ||
}); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/seyed/ali/timeentryservice/model/enums/OperationType.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 com.seyed.ali.timeentryservice.model.enums; | ||
|
||
public enum OperationType { | ||
|
||
DELETE, DETACH | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/seyed/ali/timeentryservice/model/payload/ProjectDTO.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,39 @@ | ||
package com.seyed.ali.timeentryservice.model.payload; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* DTO for {@link com.seyed.ali.projectservice.model.domain.Project} | ||
*/ | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class ProjectDTO implements Serializable { | ||
|
||
@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED, description = "Unique identifier for the project", example = "12345") | ||
private String projectId; | ||
|
||
@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED, description = "The project name", example = "Microservices-Springboot") | ||
private String projectName; | ||
|
||
@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED, description = "The project name", example = "Learning microservices is really exciting and HARD ;)") | ||
private String projectDescription; | ||
|
||
@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED, description = "The task associated with the project", implementation = TaskDTO.class) | ||
private List<TaskDTO> taskDTO = new ArrayList<>(); | ||
|
||
public ProjectDTO(String projectId, String projectName, String projectDescription) { | ||
this.projectId = projectId; | ||
this.projectName = projectName; | ||
this.projectDescription = projectDescription; | ||
this.taskDTO = new ArrayList<>(); | ||
} | ||
|
||
} |
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
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
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
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
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