2019-01-16 16:28:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type Credit
|
|
|
|
# Credits are used to give free reservations to users
|
2023-02-24 17:26:55 +01:00
|
|
|
class API::CreditsController < API::APIController
|
2016-03-23 18:39:41 +01:00
|
|
|
before_action :authenticate_user!
|
2019-01-16 16:28:25 +01:00
|
|
|
before_action :set_credit, only: %i[show update destroy]
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
def index
|
|
|
|
authorize Credit
|
2019-01-16 16:28:25 +01:00
|
|
|
@credits = if params
|
|
|
|
Credit.includes(:creditable).where(params.permit(:creditable_type))
|
|
|
|
else
|
|
|
|
Credit.includes(:creditable).all
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
2023-02-24 17:26:55 +01:00
|
|
|
def show; end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def create
|
|
|
|
authorize Credit
|
|
|
|
@credit = Credit.new(credit_params)
|
|
|
|
if @credit.save
|
|
|
|
render :show, status: :created, location: @credit
|
|
|
|
else
|
|
|
|
render json: @credit.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
authorize Credit
|
|
|
|
if @credit.update(credit_params)
|
|
|
|
render :show, status: :ok, location: @credit
|
|
|
|
else
|
|
|
|
render json: @credit.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
authorize Credit
|
|
|
|
@credit.destroy
|
|
|
|
head :no_content
|
|
|
|
end
|
|
|
|
|
2022-06-29 14:15:36 +02:00
|
|
|
def user_resource
|
|
|
|
@user = User.find(params[:id])
|
|
|
|
authorize @user, policy_class: CreditPolicy
|
|
|
|
|
|
|
|
@credits = []
|
|
|
|
return unless @user.subscribed_plan
|
|
|
|
|
|
|
|
@credits = @user.subscribed_plan
|
|
|
|
.credits
|
|
|
|
.where(creditable_type: params[:resource])
|
|
|
|
.includes(:users_credits)
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
private
|
|
|
|
|
2019-01-16 16:28:25 +01:00
|
|
|
def set_credit
|
|
|
|
@credit = Credit.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def credit_params
|
2021-02-24 11:11:18 +01:00
|
|
|
params.require(:credit).permit(:creditable_id, :creditable_type, :plan_id, :hours)
|
2019-01-16 16:28:25 +01:00
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|