Project refactorize
This commit is contained in:
parent
4edcc3c19c
commit
704f215f80
9 changed files with 121 additions and 50 deletions
|
|
@ -2,55 +2,54 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\StoreProject;
|
||||
use App\Http\Requests\UpdateProject;
|
||||
use App\Models\Project;
|
||||
use App\Models\Ticket;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class ProjectController extends Controller
|
||||
{
|
||||
public function index() {
|
||||
public function index()
|
||||
{
|
||||
$projects = Project::all();
|
||||
return view('project.index', compact('projects'));
|
||||
}
|
||||
|
||||
public function create() {
|
||||
public function create()
|
||||
{
|
||||
return view('project.create');
|
||||
}
|
||||
|
||||
public function store(Request $request) {
|
||||
$project = new Project();
|
||||
$project->name = $request->name;
|
||||
$project->save();
|
||||
public function store(StoreProject $request)
|
||||
{
|
||||
$project = new Project($request->validated());
|
||||
$project->saveOrFail();
|
||||
|
||||
return redirect()->route('projects');
|
||||
return redirect()->route('project.index');
|
||||
}
|
||||
|
||||
public function edit($id) {
|
||||
$project = Project::findOrFail($id);
|
||||
return view('project.edit', compact('project'));
|
||||
public function edit(Project $project)
|
||||
{
|
||||
return view('project.edit', [
|
||||
'project' => $project
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, $id) {
|
||||
$project = Project::findOrFail($id);
|
||||
|
||||
$projectSlug = Str::slug($project->name);
|
||||
|
||||
if (file_exists(public_path('fichiers/' . $projectSlug)))
|
||||
rename(public_path('fichiers/' . $projectSlug), public_path('fichiers/' . Str::slug($request->name)));
|
||||
|
||||
$project->name = $request->name;
|
||||
$project->save();
|
||||
public function update(UpdateProject $request, Project $project)
|
||||
{
|
||||
$project->fill($request->validated());
|
||||
$project->saveOrFail();
|
||||
|
||||
return redirect()->back()->with('success', 'Modifications enregistrées !');
|
||||
}
|
||||
|
||||
public function destroy($id) {
|
||||
$project = Project::findOrFail($id);
|
||||
$tickets = Ticket::where('project_id', $project->id)->count();
|
||||
if ($tickets > 0)
|
||||
return redirect()->back()->withErrors("Ce projet ne peut être supprimer, des tickets lui sont encore liés.");
|
||||
public function destroy(Project $project)
|
||||
{
|
||||
if (Ticket::where('project_id', $project->id)->count()) {
|
||||
return redirect()->back()
|
||||
->withErrors("Ce projet ne peut être supprimer, des tickets lui sont encore liés.");
|
||||
}
|
||||
$project->delete();
|
||||
return redirect()->route('projects');
|
||||
return redirect()->route('project.index');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
30
app/Http/Requests/StoreProject.php
Normal file
30
app/Http/Requests/StoreProject.php
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class StoreProject extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'name' => 'required|string|min:4|max:30'
|
||||
];
|
||||
}
|
||||
}
|
||||
8
app/Http/Requests/UpdateProject.php
Normal file
8
app/Http/Requests/UpdateProject.php
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
|
||||
class UpdateProject extends StoreProject
|
||||
{
|
||||
}
|
||||
|
|
@ -4,12 +4,53 @@ namespace App\Models;
|
|||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Project extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public function tickets() {
|
||||
protected $fillable = ['name'];
|
||||
|
||||
protected static function booted()
|
||||
{
|
||||
static::created(function (Project $project) {
|
||||
$path = Project::ticketAssetsPath($project->slug_name);
|
||||
if (!File::exists($path)) {
|
||||
File::makeDirectory($path);
|
||||
}
|
||||
});
|
||||
static::updating(function (Project $project) {
|
||||
$oldName = Project::where('id', $project->id)->first()->name;
|
||||
$oldPath = Project::ticketAssetsPath($oldName);
|
||||
$newPath = Project::ticketAssetsPath($project->slug_name);
|
||||
if (File::exists($oldPath)) {
|
||||
File::move($oldPath, $newPath);
|
||||
} else {
|
||||
File::makeDirectory($newPath);
|
||||
}
|
||||
});
|
||||
static::deleted(function (Project $project) {
|
||||
$path = Project::ticketAssetsPath($project->slug_name);
|
||||
if (File::exists($path)) {
|
||||
File::deleteDirectory($path);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function tickets()
|
||||
{
|
||||
return $this->hasMany('App\Models\Ticket', 'project_id');
|
||||
}
|
||||
|
||||
public function getSlugNameAttribute()
|
||||
{
|
||||
return Str::slug($this->name);
|
||||
}
|
||||
|
||||
protected static function ticketAssetsPath(string $str)
|
||||
{
|
||||
return public_path(sprintf('fichiers/%s', $str));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@
|
|||
<a class="nav-link @yield('tickets')" href="{{ route('home') }}">Tickets</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link @yield('projects')" href="{{ route('projects') }}">Projets</a>
|
||||
<a class="nav-link @yield('project')" href="{{ route('project.index') }}">Projets</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
@extends('layout.app')
|
||||
|
||||
@section('projects', 'active')
|
||||
@section('project', 'active')
|
||||
|
||||
@section('content')
|
||||
<div class="container">
|
||||
|
|
@ -9,8 +9,7 @@
|
|||
<div class="card">
|
||||
<div class="card-header">Créer un projet</div>
|
||||
<div class="card-body">
|
||||
<form method="POST" action="{{ route('store-project') }}">
|
||||
@method('PUT')
|
||||
<form method="POST" action="{{ route('project.store') }}">
|
||||
@csrf
|
||||
@include('modules.error-messages')
|
||||
|
||||
|
|
|
|||
|
|
@ -1,16 +1,17 @@
|
|||
@extends('layout.app')
|
||||
|
||||
@section('projects', 'active')
|
||||
@section('project', 'active')
|
||||
|
||||
@section('content')
|
||||
<div class="container">
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-md-8">
|
||||
<div class="card">
|
||||
<div class="card-header">Modifier un projet<a href="{{ route('projects') }}" class="btn btn-sm btn-primary float-right">Retour</a></div>
|
||||
<div class="card-header">Modifier un projet<a href="{{ route('project.index') }}" class="btn btn-sm btn-primary float-right">Retour</a></div>
|
||||
<div class="card-body">
|
||||
<form method="POST" action="{{ route('update-project', $project->id) }}">
|
||||
<form method="POST" action="{{ route('project.update', $project->id) }}">
|
||||
@csrf
|
||||
@method('PUT')
|
||||
@include('modules.error-messages')
|
||||
@if (session('success'))
|
||||
<div class="alert alert-success">
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
@extends('layout.app')
|
||||
|
||||
@section('projects', 'active')
|
||||
@section('project', 'active')
|
||||
|
||||
@section('content')
|
||||
<div class="container content">
|
||||
@include('modules.error-messages')
|
||||
<h2>Gestion des projets<a href="{{ route('create-project') }}" class="float-right btn btn-sm btn-primary">Créer un projet</a></h2>
|
||||
<h2>Gestion des projets<a href="{{ route('project.create') }}" class="float-right btn btn-sm btn-primary">Créer un projet</a></h2>
|
||||
<table class="table table-hover">
|
||||
<thead>
|
||||
<th scope="col" class="w-75">Projet</th>
|
||||
|
|
@ -16,13 +16,12 @@
|
|||
@foreach ($projects as $project)
|
||||
<tr>
|
||||
<td>{{ $project->name }}</td>
|
||||
<td><a href="{{ route('edit-project', $project->id) }}" class="btn btn-sm btn-primary">Modifier</a></td>
|
||||
<td><a href="{{ route('project.edit', $project->id) }}" class="btn btn-sm btn-primary">Modifier</a></td>
|
||||
<td>
|
||||
<form action="{{ route('destroy-project', $project->id) }}" method="POST" class="d-inline">
|
||||
<form action="{{ route('project.destroy', $project->id) }}" method="POST" class="d-inline">
|
||||
@method('DELETE')
|
||||
@csrf
|
||||
<input type="submit" class="btn btn-sm btn-danger" value="Supprimer"
|
||||
onclick="return confirm('Êtes-vous sûr(e) ?')">
|
||||
<input type="submit" class="btn btn-sm btn-danger" value="Supprimer" onclick="return confirm('Êtes-vous sûr(e) ?')">
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
|
|
|
|||
|
|
@ -30,14 +30,8 @@ Route::middleware(['auth'])->group(function () {
|
|||
Route::post('/ticket/change-status/{id}', [TicketController::class, 'changeStatus'])->name('change-status-ticket');
|
||||
Route::put('/ticket/message/create', [TicketController::class, 'createMessage'])->name('create-message');
|
||||
Route::post('/ticket/subscribe/{id}', [TicketController::class, 'subscribe'])->name('subscribe-ticket');
|
||||
// Projets (admin)
|
||||
// Projets
|
||||
Route::middleware(['is-admin'])->group(function () {
|
||||
Route::get('/projects', [ProjectController::class, 'index'])->name('projects');
|
||||
Route::get('/project/create', [ProjectController::class, 'create'])->name('create-project');
|
||||
Route::put('/project/create', [ProjectController::class, 'store'])->name('store-project');
|
||||
Route::get('/project/{id}/edit', [ProjectController::class, 'edit'])->name('edit-project');
|
||||
Route::post('/project/{id}/edit', [ProjectController::class, 'update'])->name('update-project');
|
||||
Route::delete('/project/{id}/destroy', [ProjectController::class, 'destroy'])->name('destroy-project');
|
||||
Route::resource('project', ProjectController::class)->except('show');
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue