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

Commit aeae0ae

Browse files
committed
Implement video like command
1 parent 0de578f commit aeae0ae

File tree

7 files changed

+195
-0
lines changed

7 files changed

+195
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package tv.codely.mooc.notification.application.like;
2+
3+
import tv.codely.mooc.video.domain.VideoLiked;
4+
import tv.codely.shared.application.DomainEventSubscriber;
5+
6+
public class SendPushToSubscribersOnVideoLiked implements DomainEventSubscriber<VideoLiked> {
7+
@Override
8+
public Class<VideoLiked> subscribedTo() {
9+
return VideoLiked.class;
10+
}
11+
12+
@Override
13+
public void consume(VideoLiked event) {
14+
System.out.println(
15+
String.format(
16+
"Hey! User with ID <%s> liked video with title <%s>",
17+
event.userId(),
18+
event.title()
19+
)
20+
);
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package tv.codely.mooc.shared.domain.user;
2+
3+
public final class UserId {
4+
private final String value;
5+
6+
public UserId(final String value) {
7+
this.value = value;
8+
}
9+
10+
public String value() {
11+
return value;
12+
}
13+
14+
@Override
15+
public boolean equals(Object o) {
16+
if (this == o) return true;
17+
if (o == null || getClass() != o.getClass()) return false;
18+
19+
UserId that = (UserId) o;
20+
21+
return value.equals(that.value);
22+
}
23+
24+
@Override
25+
public int hashCode() {
26+
return value.hashCode();
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package tv.codely.mooc.video.application.like;
2+
3+
import tv.codely.mooc.shared.domain.user.UserId;
4+
import tv.codely.mooc.video.domain.VideoLike;
5+
import tv.codely.mooc.video.domain.VideoTitle;
6+
import tv.codely.shared.domain.EventBus;
7+
8+
public final class VideoLiker {
9+
private final EventBus eventBus;
10+
11+
public VideoLiker(EventBus eventBus) {
12+
this.eventBus = eventBus;
13+
}
14+
15+
public void like(String rawTitle, String rawUserId) {
16+
final var title = new VideoTitle(rawTitle);
17+
final var userId = new UserId(rawUserId);
18+
19+
final var videoLike = VideoLike.like(title, userId);
20+
21+
eventBus.publish(videoLike.pullDomainEvents());
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package tv.codely.mooc.video.domain;
2+
3+
import tv.codely.mooc.shared.domain.user.UserId;
4+
import tv.codely.shared.domain.AggregateRoot;
5+
6+
public class VideoLike extends AggregateRoot {
7+
private final VideoTitle title;
8+
private final UserId userId;
9+
10+
private VideoLike(VideoTitle title, UserId userId) {
11+
this.title = title;
12+
this.userId = userId;
13+
}
14+
15+
public static VideoLike like(VideoTitle title, UserId userId) {
16+
var videoLike = new VideoLike(title, userId);
17+
18+
var videoLiked = new VideoLiked(title.value(), userId.value());
19+
20+
videoLike.record(videoLiked);
21+
22+
return videoLike;
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package tv.codely.mooc.video.domain;
2+
3+
import tv.codely.shared.domain.DomainEvent;
4+
5+
public final class VideoLiked implements DomainEvent {
6+
private static final String FULL_QUALIFIED_EVENT_NAME = "codelytv.video.video.event.1.video.liked";
7+
8+
private final String title;
9+
private final String userId;
10+
11+
public VideoLiked(String title, String userId) {
12+
this.title = title;
13+
this.userId = userId;
14+
}
15+
16+
public String fullQualifiedEventName() {
17+
return FULL_QUALIFIED_EVENT_NAME;
18+
}
19+
20+
public String title() {
21+
return title;
22+
}
23+
24+
public String userId() {
25+
return userId;
26+
}
27+
28+
@Override
29+
public boolean equals(Object o) {
30+
if (this == o) return true;
31+
if (o == null || getClass() != o.getClass()) return false;
32+
33+
VideoLiked that = (VideoLiked) o;
34+
35+
if (!title.equals(that.title)) return false;
36+
return userId.equals(that.userId);
37+
}
38+
39+
@Override
40+
public int hashCode() {
41+
int result = title.hashCode();
42+
result = 31 * result + userId.hashCode();
43+
return result;
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package tv.codely.mooc.video.infrastructure;
2+
3+
import tv.codely.mooc.notification.application.like.SendPushToSubscribersOnVideoLiked;
4+
import tv.codely.mooc.video.application.like.VideoLiker;
5+
import tv.codely.shared.application.DomainEventSubscriber;
6+
import tv.codely.shared.domain.EventBus;
7+
import tv.codely.shared.infrastructure.bus.ReactorEventBus;
8+
9+
import java.util.Set;
10+
11+
public class VideoLikeCliController {
12+
public static void main(String[] args) {
13+
final Set<DomainEventSubscriber> subscribers = Set.of(
14+
new SendPushToSubscribersOnVideoLiked()
15+
);
16+
final EventBus eventBus = new ReactorEventBus(subscribers);
17+
final var videoLiker = new VideoLiker(eventBus);
18+
19+
final var videoTitle = "\uD83C\uDF89 New YouTube.com/CodelyTV video title";
20+
final var userId = "06e8bb44-486c-41ec-8395-ead1288e5b37";
21+
22+
videoLiker.like(videoTitle, userId);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package tv.codely.mooc.video.application.like;
2+
3+
import org.junit.jupiter.api.Test;
4+
import tv.codely.mooc.video.domain.VideoLiked;
5+
import tv.codely.shared.domain.EventBus;
6+
7+
import java.util.List;
8+
9+
import static org.mockito.Mockito.mock;
10+
import static org.mockito.Mockito.verify;
11+
12+
final class VideoLikerShould {
13+
@Test
14+
void publish_the_video_liked_domain_event() {
15+
final EventBus eventBus = mock(EventBus.class);
16+
final var videoLiker = new VideoLiker(eventBus);
17+
18+
final var videoTitle = "\uD83C\uDF89 New YouTube.com/CodelyTV video title";
19+
final var userId = "This should be the video description \uD83D\uDE42";
20+
21+
videoLiker.like(videoTitle, userId);
22+
23+
final var expectedVideoLiked = new VideoLiked(videoTitle, userId);
24+
25+
verify(eventBus).publish(List.of(expectedVideoLiked));
26+
}
27+
28+
}

0 commit comments

Comments
 (0)