|
| 1 | +class ProjectsController < ApplicationController |
| 2 | + before_action :set_project, only: [:show, :edit, :update, :destroy] |
| 3 | + before_action :set_clients, only: [:new, :edit] |
| 4 | + before_action :set_employees, only: [:new, :edit] |
| 5 | + |
| 6 | + def index |
| 7 | + @projects = Project.all |
| 8 | + end |
| 9 | + |
| 10 | + def show |
| 11 | + @employees = Employee.all |
| 12 | + end |
| 13 | + |
| 14 | + def new |
| 15 | + @project = Project.new |
| 16 | + set_clients |
| 17 | + set_employees |
| 18 | + end |
| 19 | + |
| 20 | + def create |
| 21 | + @project = Project.new(project_params) |
| 22 | + if @project.save |
| 23 | + redirect_to @project, notice: "Projet créé avec succès." |
| 24 | + else |
| 25 | + set_clients |
| 26 | + set_employees |
| 27 | + render :new |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + def edit |
| 32 | + set_clients |
| 33 | + set_employees |
| 34 | + end |
| 35 | + |
| 36 | + def update |
| 37 | + if @project.update(project_params) |
| 38 | + redirect_to @project, notice: "Projet mis à jour avec succès." |
| 39 | + else |
| 40 | + set_clients |
| 41 | + set_employees |
| 42 | + render :edit |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + def destroy |
| 47 | + @project.destroy |
| 48 | + redirect_to projects_path, notice: "Projet supprimé avec succès." |
| 49 | + end |
| 50 | + |
| 51 | + def assign_employees |
| 52 | + @project = Project.find(params[:id]) |
| 53 | + |
| 54 | + # Mettre à jour les employés assignés |
| 55 | + @project.employees = Employee.where(id: params[:employee_ids]) |
| 56 | + |
| 57 | + if @project.save |
| 58 | + redirect_to @project, notice: "Les employés ont été mis à jour avec succès." |
| 59 | + else |
| 60 | + redirect_to @project, alert: "Une erreur s'est produite lors de la mise à jour des employés." |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + private |
| 65 | + |
| 66 | + def set_project |
| 67 | + @project = Project.find(params[:id]) |
| 68 | + end |
| 69 | + |
| 70 | + def set_clients |
| 71 | + @clients = Client.all |
| 72 | + end |
| 73 | + |
| 74 | +def set_employees |
| 75 | + @employees = Employee.all |
| 76 | + end |
| 77 | + |
| 78 | + def project_params |
| 79 | + params.require(:project).permit( |
| 80 | + :name, :description, :address, :latitude, :longitude, :start_at, :end_at, |
| 81 | + :initial_start_at, :initial_end_at, :totalbudget, :progression, |
| 82 | + :human_cost, :material_cost, :customer_budget, :total_expenses, :client_id, |
| 83 | + employee_ids: [] |
| 84 | + ) |
| 85 | + end |
| 86 | +end |
0 commit comments