2018-08-08 22:55:51 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Api
|
|
|
|
module V1
|
|
|
|
class InventoryColumnsController < BaseController
|
|
|
|
before_action :load_team
|
|
|
|
before_action :load_inventory
|
2018-11-07 22:43:44 +08:00
|
|
|
before_action only: %i(show update destroy) do
|
|
|
|
load_inventory_column(:id)
|
|
|
|
end
|
2018-09-24 19:33:44 +08:00
|
|
|
before_action :check_manage_permissions, only: %i(update destroy)
|
|
|
|
before_action :check_create_permissions, only: %i(create)
|
2018-10-03 19:22:11 +08:00
|
|
|
|
2018-08-08 22:55:51 +08:00
|
|
|
def index
|
|
|
|
columns = @inventory.repository_columns
|
|
|
|
.includes(:repository_list_items)
|
2018-08-24 22:41:26 +08:00
|
|
|
.page(params.dig(:page, :number))
|
|
|
|
.per(params.dig(:page, :size))
|
2018-10-06 19:47:41 +08:00
|
|
|
render jsonapi: columns,
|
|
|
|
each_serializer: InventoryColumnSerializer,
|
|
|
|
hide_list_items: true
|
2018-08-08 22:55:51 +08:00
|
|
|
end
|
|
|
|
|
2018-08-24 22:41:26 +08:00
|
|
|
def create
|
|
|
|
inventory_column =
|
2018-09-12 19:44:10 +08:00
|
|
|
@inventory.repository_columns.create!(inventory_column_params)
|
2018-08-24 22:41:26 +08:00
|
|
|
render jsonapi: inventory_column,
|
|
|
|
serializer: InventoryColumnSerializer,
|
2018-10-06 23:11:59 +08:00
|
|
|
hide_list_items: true,
|
2018-08-24 22:41:26 +08:00
|
|
|
status: :created
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2018-10-06 19:47:41 +08:00
|
|
|
render jsonapi: @inventory_column,
|
|
|
|
serializer: InventoryColumnSerializer,
|
|
|
|
include: :inventory_list_items
|
2018-08-24 22:41:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@inventory_column.attributes = update_inventory_column_params
|
|
|
|
if @inventory_column.changed? && @inventory_column.save!
|
|
|
|
render jsonapi: @inventory_column,
|
2018-10-06 23:11:59 +08:00
|
|
|
serializer: InventoryColumnSerializer,
|
|
|
|
hide_list_items: true
|
2018-08-24 22:41:26 +08:00
|
|
|
else
|
2018-10-23 23:52:48 +08:00
|
|
|
render body: nil, status: :no_content
|
2018-08-24 22:41:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@inventory_column.destroy!
|
|
|
|
render body: nil
|
|
|
|
end
|
|
|
|
|
2018-08-08 22:55:51 +08:00
|
|
|
private
|
|
|
|
|
2018-08-24 22:41:26 +08:00
|
|
|
def check_manage_permissions
|
|
|
|
unless can_manage_repository_column?(@inventory_column)
|
2018-11-07 22:43:44 +08:00
|
|
|
raise PermissionError.new(RepositoryColumn, :manage)
|
2018-08-24 22:41:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-24 19:33:44 +08:00
|
|
|
def check_create_permissions
|
2019-07-12 22:43:54 +08:00
|
|
|
raise PermissionError.new(RepositoryColumn, :create) unless can_create_repository_columns?(@inventory)
|
2018-09-24 19:33:44 +08:00
|
|
|
end
|
|
|
|
|
2018-08-24 22:41:26 +08:00
|
|
|
def inventory_column_params
|
|
|
|
unless params.require(:data).require(:type) == 'inventory_columns'
|
2018-10-23 23:52:48 +08:00
|
|
|
raise TypeError
|
2018-08-24 22:41:26 +08:00
|
|
|
end
|
|
|
|
params.require(:data).require(:attributes)
|
2018-10-16 21:35:02 +08:00
|
|
|
new_params = params
|
|
|
|
.permit(data: { attributes: %i(name data_type) })[:data]
|
|
|
|
.merge(created_by: @current_user)
|
|
|
|
if new_params[:attributes][:data_type].present?
|
|
|
|
new_params[:attributes][:data_type] =
|
|
|
|
Extends::API_REPOSITORY_DATA_TYPE_MAPPINGS
|
|
|
|
.key(new_params.dig(:attributes, :data_type))
|
|
|
|
end
|
|
|
|
new_params
|
2018-08-24 22:41:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_inventory_column_params
|
|
|
|
unless params.require(:data).require(:id).to_i == params[:id].to_i
|
2018-10-23 23:52:48 +08:00
|
|
|
raise IDMismatchError
|
2018-08-24 22:41:26 +08:00
|
|
|
end
|
2018-10-16 21:35:02 +08:00
|
|
|
if inventory_column_params[:attributes].include?(:data_type)
|
2018-11-07 23:39:00 +08:00
|
|
|
raise ActiveRecord::RecordInvalid,
|
|
|
|
I18n.t('api.core.errors.inventory_column_type.detail')
|
2018-10-06 23:11:59 +08:00
|
|
|
end
|
2018-08-24 22:41:26 +08:00
|
|
|
inventory_column_params[:attributes]
|
|
|
|
end
|
2018-08-08 22:55:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|