Created
April 13, 2022 20:01
-
-
Save stolarczykt/6906bd2aadce98a7fef464e5c481dca6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Advertisements | |
class Advertisement | |
include AggregateRoot | |
AlreadyPublished = Class.new(StandardError) | |
NotPublished = Class.new(StandardError) | |
NotAnAuthorOfAdvertisement = Class.new(StandardError) | |
def initialize(id) | |
@id = id | |
end | |
def publish(author_id) | |
raise AlreadyPublished if @state.equal?(:published) | |
apply AdvertisementPublished.new(data: {advertisement_id: @id, author_id: author_id}) | |
end | |
def change_content(new_content, author_id) | |
raise NotPublished unless @state.equal?(:published) | |
raise NotAnAuthorOfAdvertisement unless @author_id.equal?(author_id) | |
apply ContentHasChanged.new(data: {content: new_content}) | |
end | |
on AdvertisementPublished do |event| | |
@state = :published | |
@author_id = event.data[:author_id] | |
end | |
on ContentHasChanged do |event| | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment