diff --git a/.gitignore b/.gitignore index dc57c96..5fbc4a2 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ out/ # Ignore Gradle GUI config gradle-app.setting +# IntelliJ .idea folder +.idea/ + diff --git a/src/mooc/main/tv/codely/mooc/notification/application/create/NotifyOnVideoPublished.java b/src/mooc/main/tv/codely/mooc/notification/application/create/NotifyOnVideoPublished.java new file mode 100644 index 0000000..db3632c --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/notification/application/create/NotifyOnVideoPublished.java @@ -0,0 +1,27 @@ +package tv.codely.mooc.notification.application.create; + +import tv.codely.mooc.notification.domain.Notifier; +import tv.codely.mooc.video.domain.VideoPublished; +import tv.codely.shared.application.DomainEventSubscriber; + +public class NotifyOnVideoPublished implements DomainEventSubscriber { + private final Notifier notifier; + + public NotifyOnVideoPublished(Notifier notifier) { + this.notifier = notifier; + } + + @Override + public Class subscribedTo() { + return VideoPublished.class; + } + + @Override + public void consume(VideoPublished event) { + notifier.notify(String.format( + "Hey! There is a new video with title <%s> and description <%s>", + event.title(), + event.description() + )); + } +} diff --git a/src/mooc/main/tv/codely/mooc/notification/domain/Notifier.java b/src/mooc/main/tv/codely/mooc/notification/domain/Notifier.java new file mode 100644 index 0000000..c2e7a18 --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/notification/domain/Notifier.java @@ -0,0 +1,5 @@ +package tv.codely.mooc.notification.domain; + +public interface Notifier { + void notify(String message); +} diff --git a/src/mooc/main/tv/codely/mooc/notification/infrastructure/FileHandlerCantBeCreated.java b/src/mooc/main/tv/codely/mooc/notification/infrastructure/FileHandlerCantBeCreated.java new file mode 100644 index 0000000..aba65aa --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/notification/infrastructure/FileHandlerCantBeCreated.java @@ -0,0 +1,7 @@ +package tv.codely.mooc.notification.infrastructure; + +public class FileHandlerCantBeCreated extends RuntimeException { + FileHandlerCantBeCreated(String reason) { + super("Can't create file handler:\n" + reason); + } +} diff --git a/src/mooc/main/tv/codely/mooc/notification/infrastructure/LogNotifier.java b/src/mooc/main/tv/codely/mooc/notification/infrastructure/LogNotifier.java new file mode 100644 index 0000000..5dbefa6 --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/notification/infrastructure/LogNotifier.java @@ -0,0 +1,26 @@ +package tv.codely.mooc.notification.infrastructure; + +import tv.codely.mooc.notification.domain.Notifier; + +import java.io.IOException; +import java.util.logging.FileHandler; +import java.util.logging.Logger; + +public class LogNotifier implements Notifier { + private final static Logger logger = Logger.getLogger(LogNotifier.class.getName()); + + public LogNotifier(String fileName) throws FileHandlerCantBeCreated { + try { + FileHandler fileHandler = new FileHandler(fileName, true); + logger.addHandler(fileHandler); + } catch (IOException e) { + throw new FileHandlerCantBeCreated(fileName); + } + } + + @Override + public void notify(String text) { + logger.info(text); + } +} +