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:
parent
3a932e75c0
commit
84a7e81813
45
app/controllers/api/coupons_controller.rb
Normal file
45
app/controllers/api/coupons_controller.rb
Normal 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
|
7
app/policies/coupon_policy.rb
Normal file
7
app/policies/coupon_policy.rb
Normal 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
|
1
app/views/api/coupons/_coupon.json.jbuilder
Normal file
1
app/views/api/coupons/_coupon.json.jbuilder
Normal file
@ -0,0 +1 @@
|
||||
json.extract! coupon, :id, :name, :code, :percent_off, :valid_until, :max_usages, :active, :created_at
|
1
app/views/api/coupons/show.json.jbuilder
Normal file
1
app/views/api/coupons/show.json.jbuilder
Normal file
@ -0,0 +1 @@
|
||||
json.partial! 'api/coupons/coupon', coupon: @coupon
|
Loading…
x
Reference in New Issue
Block a user