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

63 lines
1.3 KiB
Ruby
Raw Normal View History

# 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
@prices = PriceService.list(params)
2016-03-23 18:39:41 +01:00
end
def update
authorize Price
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
@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
cs = CartService.new(current_user)
cart = cs.from_hash(params)
@amount = cart.total
2016-03-23 18:39:41 +01:00
end
private
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
params.require(:price).permit(:amount, :duration)
2016-03-23 18:39:41 +01:00
end
end