class API::CarsController < ApplicationController
  before_action :set_car, only: [:edit, :show, :update, :destroy]

  def index
    @cars = Car.all
    render json: @cars
  end	

  def show
    if stale?(last_modified: @car.updated_at, public: true)
      render json: @car
    end
  end

  def create
    @car = Car.new(car_params)
    if @car.save
      render json: @car
    else	
      render json: @car.errors, status: :unprocessable_entity
    end
  end  	 

  def update
    if @car.update(car_params)
      render json: @car
    else
      render json: @car.errors, status: :unprocessable_entity 
    end
  end

  def destroy
    @car.destroy
    head :no_content 
  end  

  private
    def car_params
      params.require(:car).permit(:make, :model, :year)
    end	

    def set_car
      @car = Car.find(params[:id])
    end	
end