Skip to content

Commit

Permalink
Reorganized ClientOrders read model
Browse files Browse the repository at this point in the history
grouped rendering classes (all in arbre now)
grouped product and order handlers
bigger handlers (items) left as separate files
  • Loading branch information
andrzejkrzywda committed Sep 27, 2024
1 parent 314a425 commit fcc771d
Show file tree
Hide file tree
Showing 20 changed files with 127 additions and 135 deletions.
8 changes: 2 additions & 6 deletions rails_application/app/controllers/client/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ def create
end

def show
@order = ClientOrders::Order.find_by_order_uid(params[:id])
@order_lines = ClientOrders::OrderLine.where(order_uid: params[:id])
render html: ClientOrders::ShowOrder.build(view_context, @order, @order_lines), layout: true
render html: ClientOrders::ShowOrder.build(view_context, params[:id]), layout: true
end

def edit
order_id = params[:id]
order_lines = ClientOrders::OrderLine.where(order_uid: params[:id])
products = ClientOrders::Product.all
render html: ClientOrders::EditOrder.build(view_context, order_id, order_lines, products), layout: true
render html: ClientOrders::EditOrder.build(view_context, order_id), layout: true
end

def add_item
Expand Down

This file was deleted.

10 changes: 0 additions & 10 deletions rails_application/app/read_models/client_orders/cancel_order.rb

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
require_relative 'rendering/orders_list'
require_relative 'rendering/show_order'
require_relative 'rendering/edit_order'

require_relative 'product_handlers'
require_relative 'order_handlers'

module ClientOrders

class Client < ApplicationRecord
Expand Down
10 changes: 0 additions & 10 deletions rails_application/app/read_models/client_orders/confirm_order.rb

This file was deleted.

10 changes: 0 additions & 10 deletions rails_application/app/read_models/client_orders/expire_order.rb

This file was deleted.

82 changes: 82 additions & 0 deletions rails_application/app/read_models/client_orders/order_handlers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
module ClientOrders

class ConfirmOrder
def call(event)
order = Order.find_by(order_uid: event.data.fetch(:order_id))
order.state = "Paid"
order.save!
end
end

class SubmitOrder
def call(event)
order = Order.find_or_create_by(order_uid: event.data.fetch(:order_id))
order.number = event.data.fetch(:order_number)
order.state = "Submitted"
order.save!
end
end

class UpdateDiscount
def call(event)
order = Order.find_or_create_by!(order_uid: event.data.fetch(:order_id))
order.percentage_discount = event.data.fetch(:amount)
order.save!
end
end

class UpdateOrderTotalValue
def call(event)
order = Order.find_or_create_by!(order_uid: event.data.fetch(:order_id)) { |order| order.state = "Draft" }
order.discounted_value = event.data.fetch(:discounted_amount)
order.total_value = event.data.fetch(:total_amount)
order.save!
end
end

class UpdatePaidOrdersSummary
def call(event)
order = Order.find_by(order_uid: event.data.fetch(:order_id))
client = Client.where(uid: order.client_uid).first
client.update(paid_orders_summary: client.paid_orders_summary + order.discounted_value)
end
end

class ExpireOrder
def call(event)
order = Order.find_by(order_uid: event.data.fetch(:order_id))
order.state = "Expired"
order.save!
end
end

class CancelOrder
def call(event)
order = Order.find_by(order_uid: event.data.fetch(:order_id))
order.state = "Cancelled"
order.save!
end
end

class AssignCustomerToOrder
def call(event)
order_uid = event.data.fetch(:order_id)
order = Order.find_by(order_uid: order_uid)

if order.nil?
order = Order.create!(order_uid: order_uid, state: "Draft")
end

order.client_uid = event.data.fetch(:customer_id)
order.save!
end
end

class ResetDiscount
def call(event)
order = Order.find_by(order_uid: event.data.fetch(:order_id))
order.percentage_discount = nil
order.save!
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module ClientOrders
class RegisterProduct
def call(event)
Product.find_or_create_by(uid: event.data.fetch(:product_id))
end
end

class ChangeProductName
def call(event)
Product.find_or_create_by(uid: event.data.fetch(:product_id)).update(
name: event.data.fetch(:name)
)
end
end

class ChangeProductPrice
def call(event)
Product.find_or_create_by(uid: event.data.fetch(:product_id)).update(price: event.data.fetch(:price))
end
end

class UpdateProductAvailability
def call(event)
product = Product.find_by(uid: event.data.fetch(:product_id))
available = event.data.fetch(:available)

product.update(available: available.positive?)
end
end
end

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ class EditOrder < Arbre::Component
include ActionView::Helpers::FormTagHelper
include ActionView::Helpers::UrlHelper

def self.build(view_context, order_id, order_lines, products)
def self.build(view_context, order_id)
order_lines = ClientOrders::OrderLine.where(order_uid: order_id)
products = ClientOrders::Product.all
new(Arbre::Context.new(nil, view_context)).build(order_id, order_lines, products)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ module ClientOrders
class ShowOrder < Arbre::Component
include Rails.application.routes.url_helpers

def self.build(view_context, order, order_lines)
def self.build(view_context, order_id)
order = ClientOrders::Order.find_by_order_uid(order_id)
order_lines = ClientOrders::OrderLine.where(order_uid: order_id)
new(Arbre::Context.new(nil, view_context)).build(order, order_lines)
end

Expand Down

This file was deleted.

11 changes: 0 additions & 11 deletions rails_application/app/read_models/client_orders/submit_order.rb

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit fcc771d

Please sign in to comment.