2019-01-16 16:28:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type Price
|
|
|
|
# Prices are used in reservations (Machine, Space)
|
2016-03-23 18:39:41 +01:00
|
|
|
class API::PricesController < API::ApiController
|
|
|
|
before_action :authenticate_user!
|
2021-12-21 17:13:40 +01:00
|
|
|
before_action :set_price, only: %i[update destroy]
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2021-12-21 14:18:03 +01:00
|
|
|
def create
|
2021-12-21 17:13:40 +01:00
|
|
|
@price = Price.new(price_create_params)
|
2021-12-21 14:18:03 +01:00
|
|
|
@price.amount *= 100
|
|
|
|
|
|
|
|
authorize @price
|
|
|
|
|
|
|
|
if @price.save
|
|
|
|
render json: @price, status: :created
|
|
|
|
else
|
|
|
|
render json: @price.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def index
|
2021-06-22 11:13:44 +02:00
|
|
|
@prices = PriceService.list(params)
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
authorize Price
|
2019-01-16 16:28:25 +01:00
|
|
|
price_parameters = price_params
|
|
|
|
price_parameters[:amount] = price_parameters[:amount] * 100
|
|
|
|
if @price.update(price_parameters)
|
2016-03-23 18:39:41 +01:00
|
|
|
render status: :ok
|
|
|
|
else
|
|
|
|
render status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-21 17:13:40 +01:00
|
|
|
def destroy
|
|
|
|
authorize @price
|
2021-12-28 11:25:10 +01:00
|
|
|
@price.safe_destroy
|
2021-12-21 17:13:40 +01:00
|
|
|
head :no_content
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def compute
|
2021-04-23 12:52:06 +02:00
|
|
|
cs = CartService.new(current_user)
|
|
|
|
cart = cs.from_hash(params)
|
|
|
|
@amount = cart.total
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2019-01-16 16:28:25 +01:00
|
|
|
|
2021-12-21 17:13:40 +01:00
|
|
|
def set_price
|
|
|
|
@price = Price.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2021-12-21 14:18:03 +01:00
|
|
|
def price_create_params
|
|
|
|
params.require(:price).permit(:amount, :duration, :group_id, :plan_id, :priceable_id, :priceable_type)
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def price_params
|
2021-12-20 17:08:14 +01:00
|
|
|
params.require(:price).permit(:amount, :duration)
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
end
|