Skip to content

Commit

Permalink
feat: add creation of special badges
Browse files Browse the repository at this point in the history
  • Loading branch information
juliano-quatrin-nunes committed Jan 17, 2025
1 parent 9db77c7 commit 0e259d6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,14 @@ class EarnBadge < Infra::Command

alias_method :aggregate_id, :user_id
end

class CreateSpecialBadge < Infra::Command
attribute :badge_id, Infra::Types::String
attribute :display_data, Infra::Types::Hash
attribute :badge_type, Infra::Types::String
attribute :badge_data, Infra::Types::Hash
attribute :points, Infra::Types::Integer

alias_method :aggregate_id, :badge_id
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,12 @@ class BadgeEarned < Infra::Event
attribute :badgeable_type, Infra::Types::String
attribute :earned_at, Infra::Types::DateTime
end

class SpecialBadgeCreated < Infra::Event
attribute :badge_id, Infra::Types::String
attribute :display_data, Infra::Types::Hash
attribute :badge_type, Infra::Types::String
attribute :badge_data, Infra::Types::Hash
attribute :points, Infra::Types::Integer
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Gamification
class SpecialBadge
include AggregateRoot

def initialize(id)
@id = id
@display_data = nil
@badge_type = nil
@badge_data = nil
@points = nil
end

def create(display_data, badge_type, badge_data, points)
apply SpecialBadgeCreated.new(data: {
badge_id: @id,
display_data:,
badge_type:,
badge_data:,
points:,
})
end

private

on SpecialBadgeCreated do |event|
@display_data = event.data[:display_data]
@badge_type = event.data[:badge_type]
@badge_data = event.data[:badge_data]
@points = event.data[:points]
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Gamification
class OnSpecialBadgeCreated
def call(event)
SpecialBadgeReadModel.create!(
badge_id: event.data[:badge_id],
display_data: event.data[:display_data],
badge_type: event.data[:badge_type],
badge_data: event.data[:badge_data],
points: event.data[:points]
)
end
end
end

0 comments on commit 0e259d6

Please sign in to comment.