1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-17 06:52:27 +01:00

API for coupons

This commit is contained in:
Sylvain 2016-08-03 17:56:36 +02:00
parent 3a932e75c0
commit 84a7e81813
4 changed files with 54 additions and 0 deletions

View File

@ -0,0 +1,45 @@
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

View File

@ -0,0 +1,7 @@
class CouponPolicy < ApplicationPolicy
%w(index show create update destroy).each do |action|
define_method "#{action}?" do
user.is_admin?
end
end
end

View File

@ -0,0 +1 @@
json.extract! coupon, :id, :name, :code, :percent_off, :valid_until, :max_usages, :active, :created_at

View File

@ -0,0 +1 @@
json.partial! 'api/coupons/coupon', coupon: @coupon