Skip to content

Instantly share code, notes, and snippets.

@stolarczykt
Created April 13, 2022 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stolarczykt/6906bd2aadce98a7fef464e5c481dca6 to your computer and use it in GitHub Desktop.
Save stolarczykt/6906bd2aadce98a7fef464e5c481dca6 to your computer and use it in GitHub Desktop.
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