2018-08-07 20:19:49 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Api
|
|
|
|
module V1
|
|
|
|
class InventoriesController < BaseController
|
2018-08-24 22:41:26 +08:00
|
|
|
before_action :load_team
|
2018-11-07 22:43:44 +08:00
|
|
|
before_action only: %i(show update destroy) do
|
|
|
|
load_inventory(:id)
|
|
|
|
end
|
2022-02-28 17:33:43 +08:00
|
|
|
before_action :check_manage_permissions, only: :update
|
|
|
|
before_action :check_delete_permissions, only: :destroy
|
2018-08-07 20:19:49 +08:00
|
|
|
|
|
|
|
def index
|
2018-08-24 17:26:49 +08:00
|
|
|
inventories = @team.repositories
|
2020-06-23 16:23:22 +08:00
|
|
|
.active
|
2018-08-24 17:26:49 +08:00
|
|
|
.page(params.dig(:page, :number))
|
|
|
|
.per(params.dig(:page, :size))
|
2018-08-23 22:48:27 +08:00
|
|
|
render jsonapi: inventories, each_serializer: InventorySerializer
|
2018-08-07 20:19:49 +08:00
|
|
|
end
|
|
|
|
|
2018-08-24 22:41:26 +08:00
|
|
|
def create
|
2018-09-13 17:08:58 +08:00
|
|
|
unless can_create_repositories?(@team)
|
2018-11-07 22:43:44 +08:00
|
|
|
raise PermissionError.new(Repository, :create)
|
2018-09-13 17:08:58 +08:00
|
|
|
end
|
|
|
|
inventory = @team.repositories.create!(
|
|
|
|
inventory_params.merge(created_by: current_user)
|
|
|
|
)
|
2018-08-24 22:41:26 +08:00
|
|
|
render jsonapi: inventory,
|
|
|
|
serializer: InventorySerializer,
|
|
|
|
status: :created
|
|
|
|
end
|
|
|
|
|
2018-08-07 20:19:49 +08:00
|
|
|
def show
|
2018-10-06 18:39:44 +08:00
|
|
|
render jsonapi: @inventory,
|
|
|
|
serializer: InventorySerializer,
|
|
|
|
include: :created_by
|
2018-08-07 20:19:49 +08:00
|
|
|
end
|
|
|
|
|
2018-08-24 22:41:26 +08:00
|
|
|
def update
|
|
|
|
@inventory.attributes = update_inventory_params
|
2022-02-28 17:33:43 +08:00
|
|
|
|
|
|
|
return render body: nil, status: :no_content unless @inventory.changed?
|
|
|
|
|
|
|
|
if @inventory.archived_changed?
|
|
|
|
@inventory.archived? ? @inventory.archive(current_user) : @inventory.restore(current_user)
|
2018-08-24 22:41:26 +08:00
|
|
|
end
|
2022-02-28 17:33:43 +08:00
|
|
|
@inventory.save!
|
|
|
|
render jsonapi: @inventory, serializer: InventorySerializer
|
2018-08-24 22:41:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@inventory.destroy!
|
|
|
|
render body: nil
|
|
|
|
end
|
|
|
|
|
2018-08-07 20:19:49 +08:00
|
|
|
private
|
|
|
|
|
2018-08-24 22:41:26 +08:00
|
|
|
def check_manage_permissions
|
2022-02-28 17:33:43 +08:00
|
|
|
if update_inventory_params.keys.excluding('archived').present?
|
|
|
|
raise PermissionError.new(Repository, :manage) unless can_manage_repository?(@inventory)
|
|
|
|
elsif update_inventory_params.key?('archived')
|
|
|
|
raise PermissionError.new(Repository, :archive) unless can_archive_repository?(@inventory)
|
2018-08-24 22:41:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-28 17:33:43 +08:00
|
|
|
def check_delete_permissions
|
|
|
|
raise PermissionError.new(Repository, :delete) unless can_delete_repository?(@inventory)
|
|
|
|
end
|
|
|
|
|
2018-08-24 22:41:26 +08:00
|
|
|
def inventory_params
|
|
|
|
unless params.require(:data).require(:type) == 'inventories'
|
2018-10-23 23:52:48 +08:00
|
|
|
raise TypeError
|
2018-08-24 22:41:26 +08:00
|
|
|
end
|
|
|
|
params.require(:data).require(:attributes)
|
2022-02-28 17:33:43 +08:00
|
|
|
params.permit(data: { attributes: %i(name archived) })[:data]
|
2018-08-24 22:41:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_inventory_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
|
|
|
|
inventory_params[:attributes]
|
|
|
|
end
|
2018-08-07 20:19:49 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|