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
|
2018-09-13 17:08:58 +08:00
|
|
|
before_action :check_manage_permissions, only: %i(update 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
|
|
|
|
if @inventory.changed? && @inventory.save!
|
|
|
|
render jsonapi: @inventory, serializer: InventorySerializer
|
|
|
|
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.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
|
|
|
|
unless can_manage_repository?(@inventory)
|
2018-11-07 22:43:44 +08:00
|
|
|
raise PermissionError.new(Repository, :manage)
|
2018-08-24 22:41:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
|
|
|
params.permit(data: { attributes: %i(name) })[:data]
|
|
|
|
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
|