Skip to content

Commit

Permalink
Implement card services for create, update, and destroy actions
Browse files Browse the repository at this point in the history
  • Loading branch information
piowit committed Jul 17, 2024
1 parent ede08bf commit e1e26d8
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 4 deletions.
11 changes: 7 additions & 4 deletions app/controllers/cards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ def new
end

def create
@card = Card.new(card_params)
service = Cards::CreateCardService.new(params: card_params)

respond_to do |format|
if @card.save
if service.call
@card = service.card
format.html { redirect_to board_url(@card.board), notice: "Card was successfully created." }
else
@card = service.card
format.html { render :new, status: :unprocessable_entity }
end
end
Expand All @@ -21,9 +23,10 @@ def edit

def update
@card = Card.find(params[:id])
service = Cards::UpdateCardService.new(card: @card, params: card_params)

respond_to do |format|
if @card.update(card_params)
if service.call
format.html { redirect_to board_url(@card.board_column.board), notice: "Card was successfully updated." }
else
format.html { render :edit, status: :unprocessable_entity }
Expand All @@ -34,7 +37,7 @@ def update
def destroy
@card = Card.find(params[:id])
board = @card.board_column.board
@card.destroy!
Cards::DestroyCardService.new(card: @card).call

respond_to do |format|
format.html { redirect_to board_url(board), notice: "Card was successfully destroyed." }
Expand Down
26 changes: 26 additions & 0 deletions app/services/cards/create_card_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Cards
class CreateCardService
attr_reader :card

def initialize(params:)
@params = params
end

def call
@card = Card.new(params)

ActiveRecord::Base.transaction do
card.save!
end
true
rescue ActiveRecord::RecordInvalid
false
end

private

attr_reader :params
end
end
20 changes: 20 additions & 0 deletions app/services/cards/destroy_card_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Cards
class DestroyCardService
attr_reader :card

def initialize(card:)
@card = card
end

def call
ActiveRecord::Base.transaction do
card.destroy!
end
true
rescue ActiveRecord::RecordInvalid
false
end
end
end
25 changes: 25 additions & 0 deletions app/services/cards/update_card_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Cards
class UpdateCardService
attr_reader :card

def initialize(card:, params:)
@card = card
@params = params
end

def call
ActiveRecord::Base.transaction do
card.update!(params)
end
true
rescue ActiveRecord::RecordInvalid
false
end

private

attr_reader :params
end
end
15 changes: 15 additions & 0 deletions spec/create_card_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

RSpec.describe Cards::CreateCardService, type: :service do
describe '#call' do
it 'creates a new card' do
board = create(:board)
board_column = create(:board_column, board: board)
params = { title: 'Card title', description: 'Card description', board_column_id: board_column.id }
service = described_class.new(params: params)

expect { service.call }.to change { Card.count }.by(1)
expect(service.card.title).to eq('Card title')
end
end
end
12 changes: 12 additions & 0 deletions spec/services/cards/destroy_card_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'rails_helper'

RSpec.describe Cards::DestroyCardService, type: :service do
describe '#call' do
it 'destroys a card' do
card = create(:card)
service = described_class.new(card: card)

expect { service.call }.to change { Card.count }.by(-1)
end
end
end
18 changes: 18 additions & 0 deletions spec/services/cards/update_card_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'rails_helper'

RSpec.describe Cards::UpdateCardService, type: :service do
describe '#call' do
it 'updates a card' do
board = create(:board)
board_column1 = create(:board_column, board: board)
board_column2 = create(:board_column, board: board)
card = create(:card, title: 'Old title', description: 'Old description', board_column: board_column1)
params = { title: 'New title', description: 'New description', board_column_id: board_column2.id }
service = described_class.new(card: card, params: params)

expect { service.call }.to change { card.reload.title }.from('Old title').to('New title')
expect(card.description).to eq('New description')
expect(card.board_column).to eq(board_column2)
end
end
end

0 comments on commit e1e26d8

Please sign in to comment.