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/prices_controller.rb
Sylvain 66f81a975e WIP: array of items
Migration from cart_items:{reservation:{}, subscription:{}, ...}
to cart_items:{items:[{reservation:{}, ...}], ...}
2021-05-19 18:12:52 +02:00

51 lines
1.3 KiB
Ruby

# frozen_string_literal: true
# API Controller for resources of type Price
# Prices are used in reservations (Machine, Space)
class API::PricesController < API::ApiController
before_action :authenticate_user!
def index
authorize Price
@prices = Price.all
if params[:priceable_type]
@prices = @prices.where(priceable_type: params[:priceable_type])
@prices = @prices.where(priceable_id: params[:priceable_id]) if params[:priceable_id]
end
if params[:plan_id]
plan_id = if /no|nil|null|undefined/i.match?(params[:plan_id])
nil
else
params[:plan_id]
end
@prices = @prices.where(plan_id: plan_id)
end
@prices = @prices.where(group_id: params[:group_id]) if params[:group_id]
end
def update
authorize Price
@price = Price.find(params[:id])
price_parameters = price_params
price_parameters[:amount] = price_parameters[:amount] * 100
if @price.update(price_parameters)
render status: :ok
else
render status: :unprocessable_entity
end
end
def compute
cs = CartService.new(current_user)
cart = cs.from_hash(params)
@amount = cart.total
end
private
def price_params
params.require(:price).permit(:amount)
end
end