|
| 1 | +package com.baeldung.dddjmolecules.article; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import org.jmolecules.ddd.annotation.AggregateRoot; |
| 8 | +import org.jmolecules.ddd.annotation.Identity; |
| 9 | + |
| 10 | +import com.baeldung.dddjmolecules.author.Username; |
| 11 | + |
| 12 | +@AggregateRoot |
| 13 | +public class Article { |
| 14 | + @Identity |
| 15 | + private Slug slug; |
| 16 | + private Username author; |
| 17 | + private String title; |
| 18 | + private String content; |
| 19 | + private Status status; |
| 20 | + private List<Comment> comments; |
| 21 | + private List<Username> likedBy; |
| 22 | + |
| 23 | + enum Status { |
| 24 | + DRAFT, PUBLISHED, HIDDEN, ARCHIVED |
| 25 | + } |
| 26 | + |
| 27 | + public Article(Username author, String content, String title) { |
| 28 | + this.status = Status.DRAFT; |
| 29 | + this.author = author; |
| 30 | + this.title = title; |
| 31 | + this.content = content; |
| 32 | + this.slug = new Slug(title.toLowerCase() |
| 33 | + .replaceAll(" ", "-")); |
| 34 | + this.comments = new ArrayList<>(); |
| 35 | + this.likedBy = new ArrayList<>(); |
| 36 | + } |
| 37 | + |
| 38 | + void publish() { |
| 39 | + if (status == Status.DRAFT || status == Status.HIDDEN) { |
| 40 | + status = Status.PUBLISHED; |
| 41 | + } |
| 42 | + throw new IllegalStateException("we cannot publish an article with status=" + status); |
| 43 | + } |
| 44 | + |
| 45 | + void hide() { |
| 46 | + if (status == Status.PUBLISHED) { |
| 47 | + status = Status.HIDDEN; |
| 48 | + } |
| 49 | + throw new IllegalStateException("we cannot hide an article with status=" + status); |
| 50 | + } |
| 51 | + |
| 52 | + void archive() { |
| 53 | + if (status != Status.ARCHIVED) { |
| 54 | + status = Status.ARCHIVED; |
| 55 | + } |
| 56 | + throw new IllegalStateException("the article is already archived"); |
| 57 | + } |
| 58 | + |
| 59 | + void comment(Username user, String message) { |
| 60 | + comments.add(new Comment(user, message)); |
| 61 | + } |
| 62 | + |
| 63 | + void like(Username user) { |
| 64 | + likedBy.add(user); |
| 65 | + } |
| 66 | + |
| 67 | + void dislike(Username user) { |
| 68 | + likedBy.remove(user); |
| 69 | + } |
| 70 | + |
| 71 | + public Slug getSlug() { |
| 72 | + return slug; |
| 73 | + } |
| 74 | + |
| 75 | + public Username getAuthor() { |
| 76 | + return author; |
| 77 | + } |
| 78 | + |
| 79 | + public String getTitle() { |
| 80 | + return title; |
| 81 | + } |
| 82 | + |
| 83 | + public String getContent() { |
| 84 | + return content; |
| 85 | + } |
| 86 | + |
| 87 | + public Status getStatus() { |
| 88 | + return status; |
| 89 | + } |
| 90 | + |
| 91 | + public List<Comment> getComments() { |
| 92 | + return Collections.unmodifiableList(comments); |
| 93 | + } |
| 94 | + |
| 95 | + public List<Username> getLikedBy() { |
| 96 | + return Collections.unmodifiableList(likedBy); |
| 97 | + } |
| 98 | +} |
0 commit comments