1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-12 23:09:03 +01:00
fab-manager/app/controllers/api/trainings_controller.rb

87 lines
2.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# API Controller for resources of type Training
2023-02-24 17:26:55 +01:00
class API::TrainingsController < API::APIController
include ApplicationHelper
2019-01-07 12:48:22 +01:00
before_action :authenticate_user!, except: %i[index show]
before_action :set_training, only: %i[update destroy]
2016-03-23 18:39:41 +01:00
def index
@requested_attributes = params[:requested_attributes]
@trainings = TrainingService.list(params)
2016-03-23 18:39:41 +01:00
end
def show
2016-09-27 16:00:22 +02:00
@training = Training.friendly.find(params[:id])
2016-03-23 18:39:41 +01:00
end
def create
authorize Training
@training = Training.new(training_params)
if @training.save
render :show, status: :created, location: @training
else
render json: @training.errors, status: :unprocessable_entity
end
end
def update
authorize Training
if params[:training][:users].present?
members = User.where(id: valid_training_params[:users])
members.each do |m|
m.trainings << @training
end
2016-07-13 18:15:14 +02:00
head :no_content
2020-04-27 17:28:11 +02:00
elsif current_user.admin? && @training.update(training_params)
# only admins can fully update a training, not managers
2019-01-07 12:48:22 +01:00
render :show, status: :ok, location: @training
2016-03-23 18:39:41 +01:00
else
2019-01-07 12:48:22 +01:00
render json: @training.errors, status: :unprocessable_entity
2016-03-23 18:39:41 +01:00
end
end
def destroy
authorize @training
@training.destroy
head :no_content
end
2023-01-31 17:10:07 +01:00
# This endpoint is used to get a list of trainings to validate
def availabilities
authorize Training
@training = Training.find(params[:id])
2019-01-07 12:48:22 +01:00
@availabilities = @training.availabilities
.includes(slots: {
slots_reservations: {
reservation: {
statistic_profile: [:trainings, { user: [:profile] }]
}
}
})
.where('slots_reservations.canceled_at': nil)
.order('availabilities.start_at DESC')
end
2016-03-23 18:39:41 +01:00
private
2019-01-07 12:48:22 +01:00
def set_training
@training = Training.find(params[:id])
end
2016-03-23 18:39:41 +01:00
2019-01-07 12:48:22 +01:00
def valid_training_params
params.require(:training).permit(:id, users: [])
end
def training_params
params.require(:training)
.permit(:id, :name, :description, :machine_ids, :plan_ids, :nb_total_places, :public_page, :disabled,
:auto_cancel, :auto_cancel_threshold, :auto_cancel_deadline, :authorization, :authorization_period,
:invalidation, :invalidation_period,
training_image_attributes: %i[id attachment], machine_ids: [], plan_ids: [],
advanced_accounting_attributes: %i[code analytical_section])
2019-01-07 12:48:22 +01:00
end
2016-03-23 18:39:41 +01:00
end