class Api::V1::OrdersController < ApplicationController
  before_action :set_order, only: [:show, :update, :destroy]

#   # GET /orders
  # def index
  #   @orders = Order.all

  #   render json: @orders
  # end

  # GET /orders/1
  def show
    render json: @order, include: {
      ordered_items: {
        include: :product
      } 
    }
  end

  # POST /orders
  def create
    @order = Order.new(order_params)
    if @order.save
      render json: {
        status: 201,
        order: @order
        }, status: :created
    else
      render json: {
          status: 422,
          errors: @order.errors.full_messages.join(", ")
      }, status: :unprocessable_entity
    end
  end

#   # PATCH/PUT /orders/1
#   def update
#     if @order.update(order_params)
#       render json: @order
#     else
#       render json: @order.errors, status: :unprocessable_entity
#     end
#   end

#   # DELETE /orders/1
#   def destroy
#     @order.destroy
#   end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_order
      @order = Order.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def order_params
      params.require(:order).permit(:email, cart_array: [])
    end
end
