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

80 lines
2.3 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-10-11 18:53:12 +02:00
before_action :set_product, only: %i[update clone destroy]
2022-07-13 15:06:46 +02:00
def index
@products = ProductService.list(params, current_user)
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
2022-09-13 15:01:55 +02:00
@product = ProductService.create(product_params, params[:product][:product_stock_movements_attributes])
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-09-13 15:01:55 +02:00
@product = ProductService.update(@product, product_params, params[:product][:product_stock_movements_attributes])
if @product.save
2022-07-13 15:06:46 +02:00
render status: :ok
else
render json: @product.errors.full_messages, status: :unprocessable_entity
end
end
2022-10-11 18:53:12 +02:00
def clone
authorize @product
@product = ProductService.clone(@product, product_params)
if @product.save
render status: :ok
else
render json: @product.errors.full_messages, status: :unprocessable_entity
end
end
2022-07-13 15:06:46 +02:00
def destroy
authorize @product
begin
ProductService.destroy(@product)
head :no_content
rescue StandardError => e
render json: e, status: :unprocessable_entity
end
2022-07-13 15:06:46 +02:00
end
2022-09-08 17:51:48 +02:00
def stock_movements
authorize Product
@movements = ProductService.stock_movements(params)
2022-09-08 17:51:48 +02:00
end
2022-07-13 15:06:46 +02:00
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],
product_images_attributes: %i[id attachment is_main _destroy],
advanced_accounting_attributes: %i[code analytical_section])
2022-07-13 15:06:46 +02:00
end
end