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/products_controller.rb

62 lines
1.9 KiB
Ruby
Raw Normal View History

2022-07-13 15:06:46 +02:00
# frozen_string_literal: true
# API Controller for resources of type Product
# Products are used in store
class API::ProductsController < API::ApiController
before_action :authenticate_user!, except: %i[index show]
2022-08-16 18:53:11 +02:00
before_action :set_product, only: %i[update destroy]
2022-07-13 15:06:46 +02:00
def index
2022-08-16 18:53:11 +02:00
@products = ProductService.list(params)
2022-07-13 15:06:46 +02:00
end
2022-08-16 18:53:11 +02:00
def show
@product = Product.includes(:product_images, :product_files).friendly.find(params[:id])
end
2022-07-13 15:06:46 +02:00
def create
authorize Product
@product = Product.new(product_params)
2022-08-04 14:02:19 +02:00
@product.amount = ProductService.amount_multiplied_by_hundred(@product.amount)
2022-07-13 15:06:46 +02:00
if @product.save
render status: :created
else
render json: @product.errors.full_messages, status: :unprocessable_entity
end
end
def update
authorize @product
2022-07-22 18:48:28 +02:00
product_parameters = product_params
2022-08-04 14:02:19 +02:00
product_parameters[:amount] = ProductService.amount_multiplied_by_hundred(product_parameters[:amount])
2022-07-22 18:48:28 +02:00
if @product.update(product_parameters)
2022-07-13 15:06:46 +02:00
render status: :ok
else
render json: @product.errors.full_messages, status: :unprocessable_entity
end
end
def destroy
authorize @product
@product.destroy
head :no_content
end
private
def set_product
@product = Product.find(params[:id])
end
def product_params
params.require(:product).permit(:name, :slug, :sku, :description, :is_active,
:product_category_id, :amount, :quantity_min,
2022-08-02 19:47:56 +02:00
:low_stock_alert, :low_stock_threshold,
machine_ids: [],
product_files_attributes: %i[id attachment _destroy],
2022-08-05 15:25:51 +02:00
product_images_attributes: %i[id attachment is_main _destroy],
product_stock_movements_attributes: %i[id quantity reason stock_type _destroy])
2022-07-13 15:06:46 +02:00
end
end