Skip to content

Commit

Permalink
Merge pull request #19 from nickel/events
Browse files Browse the repository at this point in the history
Create events
  • Loading branch information
nickel authored Mar 27, 2024
2 parents bc25493 + f86e801 commit 28a975e
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 1 deletion.
11 changes: 11 additions & 0 deletions app/packages/planning/models/event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class Event < ApplicationRecord
belongs_to :plan
belongs_to :picto

def to_struct
CustomStruct
.new(attributes)
end
end
23 changes: 23 additions & 0 deletions app/packages/planning/operations/event/create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

class Event::Create < CommandHandler::Command
class Form
include CommandHandler::Form

attribute :plan_id, :integer
attribute :picto_id, :integer
attribute :title, :string
attribute :day_of_the_week, :integer
end

delegate(*Form.new.attributes.keys, to: :form)

def call
Event
.new(plan_id:, picto_id:, title:, day_of_the_week:, position: 0)
.save_with_response
.and_then do |event|
Response.success(event.to_struct)
end
end
end
13 changes: 13 additions & 0 deletions db/migrate/20240327091940_create_events.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateEvents < ActiveRecord::Migration[7.1]
def change
create_table :events do |t|
t.references :plan, null: false, foreign_key: true
t.references :picto, null: false, foreign_key: true
t.string :title
t.integer :day_of_the_week, null: false
t.integer :position, null: false

t.timestamps
end
end
end
16 changes: 15 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/packages/planning/models/event_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require "test_helper"

class EventTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
15 changes: 15 additions & 0 deletions test/packages/planning/operations/event/create_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require "test_helper"

class Event::CreateTest < ActiveSupport::TestCase
test "event creation with the proper values" do
plan = Factory.generate_plan
picto = Factory.generate_picto

response = Event::Create
.call(plan_id: plan.id, picto_id: picto.id, title: "Event", day_of_the_week: 0)

assert response.success?
end
end

0 comments on commit 28a975e

Please sign in to comment.