Skip to content

Commit

Permalink
Adding orders endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
sofianegargouri committed Feb 1, 2025
1 parent 915e489 commit 80dc61f
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 387 deletions.
6 changes: 6 additions & 0 deletions app/controllers/v2/order_products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module V2
class OrderProductsController < ResourcesController
end
end
14 changes: 8 additions & 6 deletions app/controllers/v2/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

module V2
class OrdersController < ResourcesController
def cancel
authorize @record, :update?
%i[order deliver withdraw cancel].each do |action|
define_method(action) do
authorize @record, :update?

if @record.update(status: :canceled)
render json: @record
else
render json: @record.errors, status: :unprocessable_entity
if @record.send(:"#{action}!")
render json: @record
else
render json: @record.errors, status: :unprocessable_entity
end
end
end
end
Expand Down
36 changes: 36 additions & 0 deletions app/machines/order/state_machine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

class Order
module StateMachine
extend ActiveSupport::Concern

included do
include AASM

# enum :status, { pending: 0, delivered: 1, cancelled: 2 }
aasm column: :state, timestamps: true do
state :pending, initial: true
state :ordered
state :delivered
state :withdrawn
state :cancelled

event :order do
transitions from: :pending, to: :ordered
end

event :deliver do
transitions from: :ordered, to: :delivered
end

event :withdraw do
transitions from: :delivered, to: :withdrawn
end

event :cancel do
transitions from: %i[ordered delivered], to: :cancelled
end
end
end
end
end
2 changes: 1 addition & 1 deletion app/models/order.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class Order < ApplicationRecord
enum :status, { pending: 0, delivered: 1, canceled: 2 }
include Order::StateMachine

belongs_to :customer, optional: false
belongs_to :store, optional: false
Expand Down
9 changes: 9 additions & 0 deletions app/policies/order_product_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class OrderProductPolicy < Presets::UserEditablePolicy
class Scope < ApplicationPolicy::Scope
def resolve
scope.where(order: OrderPolicy::Scope.new(user, Order).resolve)
end
end
end
7 changes: 7 additions & 0 deletions app/serializers/order_product_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class OrderProductSerializer < ActiveModel::Serializer
attributes :id, :quantity

belongs_to :product
end
9 changes: 8 additions & 1 deletion app/serializers/order_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# frozen_string_literal: true

class OrderSerializer < ActiveModel::Serializer
attributes :id, :status, :created_at
attributes :id,
:state,
:created_at,
:updated_at,
:ordered_at,
:delivered_at,
:withdrawn_at,
:cancelled_at
belongs_to :customer
end
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@
resources :manufacturers
resources :orders, except: :destroy do
patch :cancel, on: :member
patch :deliver, on: :member
patch :order, on: :member
patch :withdraw, on: :member
end
resources :order_products, only: :index
resources :payment_types
resources :products
resources :providers
Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20250201110103_change_order_status_to_state.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class ChangeOrderStatusToState < ActiveRecord::Migration[8.0]
def change
change_table :orders, bulk: true do |t|
t.remove :status, type: :integer, default: 0
t.string :state
t.datetime :ordered_at
t.datetime :delivered_at
t.datetime :withdrawn_at
t.datetime :cancelled_at
end
end
end
8 changes: 6 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[8.0].define(version: 2025_01_27_201932) do
ActiveRecord::Schema[8.0].define(version: 2025_02_01_110103) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"
enable_extension "pgcrypto"
Expand Down Expand Up @@ -192,12 +192,16 @@
end

create_table "orders", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.integer "status", default: 0
t.uuid "customer_id"
t.uuid "store_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "deleted_at", precision: nil
t.string "state"
t.datetime "ordered_at"
t.datetime "delivered_at"
t.datetime "withdrawn_at"
t.datetime "cancelled_at"
t.index ["customer_id"], name: "index_orders_on_customer_id"
t.index ["deleted_at"], name: "index_orders_on_deleted_at"
t.index ["store_id"], name: "index_orders_on_store_id"
Expand Down
Loading

0 comments on commit 80dc61f

Please sign in to comment.