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

54 lines
1.3 KiB
Ruby
Raw Normal View History

2021-06-21 17:39:48 +02:00
# frozen_string_literal: true
# API Controller for resources of type PrepaidPack
# PrepaidPacks are used to provide discounts to users that bought many hours at once
class API::PrepaidPacksController < API::ApiController
before_action :authenticate_user!, except: :index
before_action :set_pack, only: %i[show update destroy]
def index
2021-06-25 09:28:05 +02:00
@packs = PrepaidPackService.list(params).order(minutes: :asc)
2021-06-21 17:39:48 +02:00
end
def show; end
def create
authorize PrepaidPack
@pack = PrepaidPack.new(pack_params)
if @pack.save
render status: :created
else
render json: @pack.errors.full_messages, status: :unprocessable_entity
end
end
def update
authorize @pack
if @pack.update(pack_params)
render status: :ok
else
render json: @pack.errors.full_messages, status: :unprocessable_entity
end
end
def destroy
authorize @pack
@pack.destroy
head :no_content
end
private
def set_pack
@pack = PrepaidPack.find(params[:id])
end
def pack_params
pack_params = params
pack_params[:pack][:amount] = pack_params[:pack][:amount].to_f * 100.0 if pack_params[:pack][:amount]
2021-06-25 09:56:39 +02:00
params.require(:pack).permit(:priceable_id, :priceable_type, :group_id, :amount, :minutes, :validity_count, :validity_interval,
:disabled)
2021-06-21 17:39:48 +02:00
end
end