Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.

Implement video like command #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tv.codely.mooc.notification.application.like;

import tv.codely.mooc.video.domain.VideoLiked;
import tv.codely.shared.application.DomainEventSubscriber;

public class SendPushToSubscribersOnVideoLiked implements DomainEventSubscriber<VideoLiked> {
@Override
public Class<VideoLiked> subscribedTo() {
return VideoLiked.class;
}

@Override
public void consume(VideoLiked event) {
System.out.println(
String.format(
"Hey! User with ID <%s> liked video with title <%s>",
event.userId(),
event.title()
)
);
}
}
28 changes: 28 additions & 0 deletions src/mooc/main/tv/codely/mooc/shared/domain/user/UserId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tv.codely.mooc.shared.domain.user;

public final class UserId {
private final String value;

public UserId(final String value) {
this.value = value;
}

public String value() {
return value;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

UserId that = (UserId) o;

return value.equals(that.value);
}

@Override
public int hashCode() {
return value.hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package tv.codely.mooc.video.application.like;

import tv.codely.mooc.shared.domain.user.UserId;
import tv.codely.mooc.video.domain.VideoLike;
import tv.codely.mooc.video.domain.VideoTitle;
import tv.codely.shared.domain.EventBus;

public final class VideoLiker {
private final EventBus eventBus;

public VideoLiker(EventBus eventBus) {
this.eventBus = eventBus;
}

public void like(String rawTitle, String rawUserId) {
final var title = new VideoTitle(rawTitle);
final var userId = new UserId(rawUserId);

final var videoLike = VideoLike.like(title, userId);

eventBus.publish(videoLike.pullDomainEvents());
}
}
25 changes: 25 additions & 0 deletions src/mooc/main/tv/codely/mooc/video/domain/VideoLike.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package tv.codely.mooc.video.domain;

import tv.codely.mooc.shared.domain.user.UserId;
import tv.codely.shared.domain.AggregateRoot;

public class VideoLike extends AggregateRoot {
private final VideoTitle title;
private final UserId userId;

private VideoLike(VideoTitle title, UserId userId) {
this.title = title;
this.userId = userId;
}

public static VideoLike like(VideoTitle title, UserId userId) {
var videoLike = new VideoLike(title, userId);

var videoLiked = new VideoLiked(title.value(), userId.value());

videoLike.record(videoLiked);

return videoLike;
}

}
45 changes: 45 additions & 0 deletions src/mooc/main/tv/codely/mooc/video/domain/VideoLiked.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package tv.codely.mooc.video.domain;

import tv.codely.shared.domain.DomainEvent;

public final class VideoLiked implements DomainEvent {
private static final String FULL_QUALIFIED_EVENT_NAME = "codelytv.video.video.event.1.video.liked";

private final String title;
private final String userId;

public VideoLiked(String title, String userId) {
this.title = title;
this.userId = userId;
}

public String fullQualifiedEventName() {
return FULL_QUALIFIED_EVENT_NAME;
}

public String title() {
return title;
}

public String userId() {
return userId;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

VideoLiked that = (VideoLiked) o;

if (!title.equals(that.title)) return false;
return userId.equals(that.userId);
}

@Override
public int hashCode() {
int result = title.hashCode();
result = 31 * result + userId.hashCode();
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tv.codely.mooc.video.infrastructure;

import tv.codely.mooc.notification.application.like.SendPushToSubscribersOnVideoLiked;
import tv.codely.mooc.video.application.like.VideoLiker;
import tv.codely.shared.application.DomainEventSubscriber;
import tv.codely.shared.domain.EventBus;
import tv.codely.shared.infrastructure.bus.ReactorEventBus;

import java.util.Set;

public class VideoLikeCliController {
public static void main(String[] args) {
final Set<DomainEventSubscriber> subscribers = Set.of(
new SendPushToSubscribersOnVideoLiked()
);
final EventBus eventBus = new ReactorEventBus(subscribers);
final var videoLiker = new VideoLiker(eventBus);

final var videoTitle = "\uD83C\uDF89 New YouTube.com/CodelyTV video title";
final var userId = "06e8bb44-486c-41ec-8395-ead1288e5b37";

videoLiker.like(videoTitle, userId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tv.codely.mooc.video.application.like;

import org.junit.jupiter.api.Test;
import tv.codely.mooc.video.domain.VideoLiked;
import tv.codely.shared.domain.EventBus;

import java.util.List;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

final class VideoLikerShould {
@Test
void publish_the_video_liked_domain_event() {
final EventBus eventBus = mock(EventBus.class);
final var videoLiker = new VideoLiker(eventBus);

final var videoTitle = "\uD83C\uDF89 New YouTube.com/CodelyTV video title";
final var userId = "This should be the video description \uD83D\uDE42";

videoLiker.like(videoTitle, userId);

final var expectedVideoLiked = new VideoLiked(videoTitle, userId);

verify(eventBus).publish(List.of(expectedVideoLiked));
}

}