From fa6fe4083b60b39aaf58248ad4d7758da458fd75 Mon Sep 17 00:00:00 2001 From: Wesley Buck Date: Wed, 19 Jul 2023 15:18:39 +0200 Subject: [PATCH] Dependency solution --- Backend/dependency/inquiry/build.gradle | 5 ++- .../inquiry/CreateInquiryEvent.java | 17 ++++++++ .../dependency/inquiry/InquiryService.java | 20 +++++++++- Backend/dependency/notifications/build.gradle | 17 ++++++-- .../notifications/NotificationService.java | 39 +++++++++++++++++++ 5 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 Backend/dependency/inquiry/src/main/java/com/staffinghub/coding/challenges/dependency/inquiry/CreateInquiryEvent.java create mode 100644 Backend/dependency/notifications/src/main/java/com/staffinghub/coding/challenges/dependency/notifications/NotificationService.java diff --git a/Backend/dependency/inquiry/build.gradle b/Backend/dependency/inquiry/build.gradle index 3e96382..360d80d 100644 --- a/Backend/dependency/inquiry/build.gradle +++ b/Backend/dependency/inquiry/build.gradle @@ -17,10 +17,13 @@ sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { + implementation 'org.projectlombok:lombok:1.18.26' compile('org.springframework.boot:spring-boot-starter') testCompile('org.springframework.boot:spring-boot-starter-test') } dependencyManagement { - imports { mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}") } + imports { + mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}") + } } diff --git a/Backend/dependency/inquiry/src/main/java/com/staffinghub/coding/challenges/dependency/inquiry/CreateInquiryEvent.java b/Backend/dependency/inquiry/src/main/java/com/staffinghub/coding/challenges/dependency/inquiry/CreateInquiryEvent.java new file mode 100644 index 0000000..9ba7f88 --- /dev/null +++ b/Backend/dependency/inquiry/src/main/java/com/staffinghub/coding/challenges/dependency/inquiry/CreateInquiryEvent.java @@ -0,0 +1,17 @@ +package com.staffinghub.coding.challenges.dependency.inquiry; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.util.Date; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class CreateInquiryEvent { + private Date date; + private Inquiry inquiry; +} diff --git a/Backend/dependency/inquiry/src/main/java/com/staffinghub/coding/challenges/dependency/inquiry/InquiryService.java b/Backend/dependency/inquiry/src/main/java/com/staffinghub/coding/challenges/dependency/inquiry/InquiryService.java index 7b6dc3b..22d7246 100644 --- a/Backend/dependency/inquiry/src/main/java/com/staffinghub/coding/challenges/dependency/inquiry/InquiryService.java +++ b/Backend/dependency/inquiry/src/main/java/com/staffinghub/coding/challenges/dependency/inquiry/InquiryService.java @@ -2,15 +2,31 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Component; +import java.util.Date; + +/** + * The inquiry service is used to create new inquiries. + */ @Component public class InquiryService { + private final ApplicationEventPublisher events; + private static final Logger LOG = LoggerFactory.getLogger(InquiryService.class); + public InquiryService(ApplicationEventPublisher events) { + this.events = events; + } + + /** + * Create a new inquiry. + * @param inquiry incoming inquiry to create of type {@link Inquiry} + */ public void create(final Inquiry inquiry) { LOG.info("User sent inquiry: {}", inquiry); + events.publishEvent(new CreateInquiryEvent(new Date(), inquiry)); } - -} +} \ No newline at end of file diff --git a/Backend/dependency/notifications/build.gradle b/Backend/dependency/notifications/build.gradle index 74bfc56..7ae0591 100644 --- a/Backend/dependency/notifications/build.gradle +++ b/Backend/dependency/notifications/build.gradle @@ -1,5 +1,10 @@ buildscript { - repositories { mavenCentral() } + repositories { + mavenCentral() + maven { + url "https://repo.spring.io/milestone/" + } + } } plugins { id "io.spring.dependency-management" version "1.0.5.RELEASE" } @@ -14,14 +19,20 @@ jar { } sourceCompatibility = 1.8 -repositories { mavenCentral() } +repositories { mavenCentral() + maven { + url "https://repo.spring.io/milestone/" + }} dependencies { + implementation 'org.projectlombok:lombok:1.18.26' compile project(":inquiry") compile('org.springframework.boot:spring-boot-starter') testCompile('org.springframework.boot:spring-boot-starter-test') } dependencyManagement { - imports { mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}") } + imports { + mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}") + } } diff --git a/Backend/dependency/notifications/src/main/java/com/staffinghub/coding/challenges/dependency/notifications/NotificationService.java b/Backend/dependency/notifications/src/main/java/com/staffinghub/coding/challenges/dependency/notifications/NotificationService.java new file mode 100644 index 0000000..eb86a25 --- /dev/null +++ b/Backend/dependency/notifications/src/main/java/com/staffinghub/coding/challenges/dependency/notifications/NotificationService.java @@ -0,0 +1,39 @@ +package com.staffinghub.coding.challenges.dependency.notifications; + +import com.staffinghub.coding.challenges.dependency.inquiry.CreateInquiryEvent; +import com.staffinghub.coding.challenges.dependency.inquiry.InquiryService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.event.EventListener; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +/** + * Notification service sends email and push notifications based off of incoming events. + */ +@Component +public class NotificationService { + + private EmailHandler emailHandler; + private PushNotificationHandler pushNotificationHandler; + private static final Logger LOG = LoggerFactory.getLogger(InquiryService.class); + + public NotificationService(EmailHandler emailHandler, PushNotificationHandler pushNotificationHandler) { + this.emailHandler = emailHandler; + this.pushNotificationHandler = pushNotificationHandler; + } + + /** + * Process create Inquiry event to send emails and push notifications. + * @param event incoming Inquiry event of type {@link CreateInquiryEvent} + */ + @Async + @EventListener + public void notificationEvent(CreateInquiryEvent event) { + LOG.info("Received notification by event for inquiry {} in date {}.", + event.getInquiry(), + event.getDate()); + emailHandler.sendEmail(event.getInquiry()); + pushNotificationHandler.sendNotification(event.getInquiry()); + } +}