2019-01-16 16:28:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type Project
|
2015-05-05 03:10:25 +02:00
|
|
|
class API::ProjectsController < API::ApiController
|
2018-12-27 14:15:58 +01:00
|
|
|
before_action :authenticate_user!, except: %i[index show last_published search]
|
|
|
|
before_action :set_project, only: %i[update destroy]
|
2015-05-05 03:10:25 +02:00
|
|
|
respond_to :json
|
|
|
|
|
|
|
|
def index
|
|
|
|
# DEPEND per(12) -> projects.coffee
|
|
|
|
@projects = policy_scope(Project).page(params[:page]).per(12)
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_published
|
|
|
|
@projects = Project.includes(:project_image).published.order('created_at desc').limit(5)
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2018-12-27 14:15:58 +01:00
|
|
|
@project = Project.friendly.find(params[:id])
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2019-06-06 16:34:53 +02:00
|
|
|
@project = Project.new(project_params.merge(author_statistic_profile_id: current_user.statistic_profile.id))
|
2015-05-05 03:10:25 +02:00
|
|
|
if @project.save
|
|
|
|
render :show, status: :created, location: @project
|
|
|
|
else
|
|
|
|
render json: @project.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
authorize @project
|
|
|
|
if @project.update(project_params)
|
|
|
|
render :show, status: :ok, location: @project
|
|
|
|
else
|
|
|
|
render json: @project.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2016-03-23 18:39:41 +01:00
|
|
|
authorize @project
|
2015-05-05 03:10:25 +02:00
|
|
|
@project.destroy
|
|
|
|
head :no_content
|
|
|
|
end
|
|
|
|
|
|
|
|
def collaborator_valid
|
2016-11-23 16:30:19 +01:00
|
|
|
project_user = ProjectUser.find_by(valid_token: params[:valid_token])
|
2015-05-05 03:10:25 +02:00
|
|
|
if project_user
|
|
|
|
project_user.update(is_valid: true, valid_token: '')
|
|
|
|
redirect_to "/#!/projects/#{project_user.project.id}" and return
|
|
|
|
end
|
|
|
|
redirect_to root_url
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def search
|
|
|
|
query_params = JSON.parse(params[:search])
|
2016-04-22 18:17:55 +02:00
|
|
|
records = Project.search(query_params, current_user).page(params[:page]).records
|
|
|
|
@total = records.total
|
|
|
|
@projects = records.includes(:users, :project_image)
|
2016-03-23 18:39:41 +01:00
|
|
|
render :index
|
|
|
|
end
|
|
|
|
|
2016-09-27 14:08:04 +02:00
|
|
|
def allowed_extensions
|
|
|
|
render json: ENV['ALLOWED_EXTENSIONS'].split(' '), status: :ok
|
|
|
|
end
|
|
|
|
|
2015-05-05 03:10:25 +02:00
|
|
|
private
|
|
|
|
|
2018-12-27 14:15:58 +01:00
|
|
|
def set_project
|
|
|
|
@project = Project.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_params
|
2019-06-06 16:34:53 +02:00
|
|
|
params.require(:project).permit(:name, :description, :tags, :machine_ids, :component_ids, :theme_ids, :licence_id, :licence_id, :state,
|
2018-12-27 14:15:58 +01:00
|
|
|
user_ids: [], machine_ids: [], component_ids: [], theme_ids: [],
|
|
|
|
project_image_attributes: [:attachment],
|
|
|
|
project_caos_attributes: %i[id attachment _destroy],
|
|
|
|
project_steps_attributes: [
|
|
|
|
:id, :description, :title, :_destroy, :step_nb,
|
|
|
|
project_step_images_attributes: %i[id attachment _destroy]
|
|
|
|
])
|
|
|
|
end
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|