summaryrefslogtreecommitdiff
path: root/app/controllers/api/orders_controller.rb
blob: a57bf68af060988b0042a4227af8375fb767686e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true

module Api
  # OrdersController
  class OrdersController < AuthenticatedController
    def index
      @orders = current_user_account.orders

      render json: serialized_collection.serializable_hash, status: :ok
    end

    def show
      @order = current_user_account.orders.find_by(id: params[:id])

      render status: :not_found and return if @order.nil?

      render json: serialized_object.serializable_hash, status: :ok
    end

    def create
      @service = PaymentServices::PaymentService.new(permitted_params[:card_id], current_user_account)
      @order = @service.call

      if @order
        render json: serialized_object.serializable_hash, status: :ok
      else
        render json: { errors: @service.error_messages }, status: :unprocessable_entity
      end
    end

    private

    def serialized_collection
      @serialized_collection ||= OrderSerializer.new(@orders)
    end

    def serialized_object
      @serialized_object ||= OrderSerializer.new(@order)
    end

    def permitted_params
      params.permit(:card_id)
    end
  end
end