2018-10-05 06:18:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Api
|
|
|
|
module V1
|
|
|
|
class InventoryCellsController < BaseController
|
2018-10-23 23:52:48 +08:00
|
|
|
before_action :load_team
|
|
|
|
before_action :load_inventory
|
|
|
|
before_action :load_inventory_column, only: :create
|
|
|
|
before_action :load_inventory_item
|
2018-10-05 06:18:42 +08:00
|
|
|
before_action :load_inventory_cell, only: %i(show update destroy)
|
|
|
|
before_action :check_manage_permissions, only: %i(create update destroy)
|
|
|
|
|
|
|
|
def index
|
|
|
|
cells = @inventory_item.repository_cells
|
|
|
|
.includes(Extends::REPOSITORY_SEARCH_INCLUDES)
|
|
|
|
.page(params.dig(:page, :number))
|
|
|
|
.per(params.dig(:page, :size))
|
|
|
|
render jsonapi: cells, each_serializer: InventoryCellSerializer
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
cell = RepositoryCell.create_with_value!(@inventory_item,
|
2018-10-23 23:52:48 +08:00
|
|
|
@inventory_column,
|
2018-10-05 06:18:42 +08:00
|
|
|
inventory_cell_params[:value],
|
|
|
|
current_user)
|
|
|
|
render jsonapi: cell,
|
|
|
|
serializer: InventoryCellSerializer,
|
|
|
|
status: :created
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
render jsonapi: @inventory_cell, serializer: InventoryCellSerializer
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
value = update_inventory_cell_params[:value]
|
|
|
|
if @inventory_cell.value.data_changed?(value)
|
|
|
|
@inventory_cell.value.update_data!(value, current_user)
|
|
|
|
render jsonapi: @inventory_cell, serializer: InventoryCellSerializer
|
|
|
|
else
|
|
|
|
render body: nil, status: :no_content
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@inventory_cell.destroy!
|
|
|
|
render body: nil
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-10-23 23:52:48 +08:00
|
|
|
def load_inventory_item
|
2018-10-05 06:18:42 +08:00
|
|
|
@inventory_item = @inventory.repository_rows.find(params[:item_id].to_i)
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_inventory_cell
|
|
|
|
@inventory_cell = @inventory_item.repository_cells
|
|
|
|
.find(params[:id].to_i)
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_manage_permissions
|
|
|
|
unless can_manage_repository_rows?(@team)
|
2018-10-23 23:52:48 +08:00
|
|
|
permission_error(RepositoryRow, :manage)
|
2018-10-05 06:18:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def inventory_cell_params
|
|
|
|
unless params.require(:data).require(:type) == 'inventory_cells'
|
2018-10-23 23:52:48 +08:00
|
|
|
raise TypeError
|
2018-10-05 06:18:42 +08:00
|
|
|
end
|
2018-10-08 19:24:02 +08:00
|
|
|
params.require(:data).require(:attributes).require(:column_id)
|
|
|
|
params.require(:data).require(:attributes).require(:value)
|
|
|
|
params[:data][:attributes]
|
2018-10-05 06:18:42 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_inventory_cell_params
|
|
|
|
unless params.require(:data).require(:id).to_i == params[:id].to_i
|
2018-10-23 23:52:48 +08:00
|
|
|
raise IDMismatchError
|
2018-10-05 06:18:42 +08:00
|
|
|
end
|
|
|
|
inventory_cell_params
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|