1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00
fab-manager/app/controllers/api/coupons_controller.rb

46 lines
947 B
Ruby
Raw Normal View History

2016-08-03 17:56:36 +02:00
class API::CouponsController < API::ApiController
before_action :authenticate_user!
before_action :set_coupon, only: [:show, :update, :destroy]
def index
@coupons = Coupon.all
end
def show
end
def create
authorize Coupon
@coupon = Coupon.new(coupon_params)
if @coupon.save
render :show, status: :created, location: @coupon
else
render json: @coupon.errors, status: :unprocessable_entity
end
end
def update
authorize Coupon
if @coupon.update(coupon_params)
render :show, status: :ok, location: @coupon
else
render json: @coupon.errors, status: :unprocessable_entity
end
end
def destroy
authorize Coupon
@coupon.destroy
head :no_content
end
private
def set_coupon
@coupon = Coupon.find(params[:id])
end
def coupon_params
params.require(:coupon).permit(:name, :code, :percent_off, :valid_until, :max_usages, :active)
end
end