2019-01-16 16:28:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type Machine
|
2015-05-05 03:10:25 +02:00
|
|
|
class API::MachinesController < API::ApiController
|
2019-01-16 16:28:25 +01:00
|
|
|
before_action :authenticate_user!, except: %i[index show]
|
|
|
|
before_action :set_machine, only: %i[update destroy]
|
2015-05-05 03:10:25 +02:00
|
|
|
respond_to :json
|
|
|
|
|
|
|
|
def index
|
2021-06-22 11:13:44 +02:00
|
|
|
@machines = MachineService.list(params)
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@machine = Machine.includes(:machine_files, :projects).friendly.find(params[:id])
|
2022-11-22 14:17:25 +01:00
|
|
|
|
|
|
|
head :not_found if @machine.deleted_at
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
authorize Machine
|
|
|
|
@machine = Machine.new(machine_params)
|
|
|
|
if @machine.save
|
|
|
|
render :show, status: :created, location: @machine
|
|
|
|
else
|
|
|
|
render json: @machine.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
authorize Machine
|
|
|
|
if @machine.update(machine_params)
|
|
|
|
render :show, status: :ok, location: @machine
|
|
|
|
else
|
|
|
|
render json: @machine.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2016-03-23 18:39:41 +01:00
|
|
|
authorize @machine
|
2022-11-23 11:59:06 +01:00
|
|
|
method = @machine.destroyable? ? :destroy : :soft_destroy!
|
|
|
|
@machine.send(method)
|
2015-05-05 03:10:25 +02:00
|
|
|
head :no_content
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-01-16 16:28:25 +01:00
|
|
|
def set_machine
|
|
|
|
@machine = Machine.find(params[:id])
|
|
|
|
end
|
2015-05-05 03:10:25 +02:00
|
|
|
|
2019-01-16 16:28:25 +01:00
|
|
|
def machine_params
|
2022-12-27 16:14:29 +01:00
|
|
|
params.require(:machine).permit(:name, :description, :spec, :disabled, :machine_category_id, :plan_ids, :reservable,
|
2023-01-09 10:45:37 +01:00
|
|
|
plan_ids: [], machine_image_attributes: %i[id attachment],
|
2022-11-10 16:14:49 +01:00
|
|
|
machine_files_attributes: %i[id attachment _destroy],
|
|
|
|
advanced_accounting_attributes: %i[code analytical_section])
|
2019-01-16 16:28:25 +01:00
|
|
|
end
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|