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

65 lines
1.4 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
2022-12-01 15:43:16 +01:00
include ApplicationHelper
2016-03-23 18:39:41 +01:00
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)
2022-12-01 15:43:16 +01:00
@price.amount = to_centimes(price_create_params[:amount])
2021-12-21 14:18:03 +01:00
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
2022-12-01 15:43:16 +01:00
price_parameters[:amount] = to_centimes(price_parameters[:amount])
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