Initial commit
This commit is contained in:
156
app/Http/Controllers/CandidateController.php
Normal file
156
app/Http/Controllers/CandidateController.php
Normal file
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
use App\Models\Candidate;
|
||||
use App\Models\Document;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CandidateController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$candidates = Candidate::with(['user', 'documents'])->latest()->get();
|
||||
|
||||
return \Inertia\Inertia::render('Admin/Candidates/Index', [
|
||||
'candidates' => $candidates
|
||||
]);
|
||||
}
|
||||
|
||||
public function comparative()
|
||||
{
|
||||
$candidates = Candidate::with(['user', 'attempts.quiz'])
|
||||
->whereHas('attempts', function($query) {
|
||||
$query->whereNotNull('finished_at');
|
||||
})
|
||||
->get();
|
||||
|
||||
return \Inertia\Inertia::render('Admin/Comparative', [
|
||||
'candidates' => $candidates
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'email' => 'required|string|email|max:255|unique:users',
|
||||
'phone' => 'nullable|string|max:20',
|
||||
'linkedin_url' => 'nullable|url|max:255',
|
||||
'cv' => 'nullable|file|mimes:pdf|max:5120',
|
||||
'cover_letter' => 'nullable|file|mimes:pdf|max:5120',
|
||||
]);
|
||||
|
||||
$password = Str::random(10);
|
||||
|
||||
$user = User::create([
|
||||
'name' => $request->name,
|
||||
'email' => $request->email,
|
||||
'password' => Hash::make($password),
|
||||
'role' => 'candidate',
|
||||
]);
|
||||
|
||||
$candidate = Candidate::create([
|
||||
'user_id' => $user->id,
|
||||
'phone' => $request->phone,
|
||||
'linkedin_url' => $request->linkedin_url,
|
||||
'status' => 'en_attente',
|
||||
]);
|
||||
|
||||
$this->storeDocument($candidate, $request->file('cv'), 'cv');
|
||||
$this->storeDocument($candidate, $request->file('cover_letter'), 'cover_letter');
|
||||
|
||||
return back()->with('success', 'Candidat créé avec succès. Mot de passe généré: ' . $password);
|
||||
}
|
||||
|
||||
public function show(Candidate $candidate)
|
||||
{
|
||||
$candidate->load([
|
||||
'user',
|
||||
'documents',
|
||||
'attempts.quiz',
|
||||
'attempts.answers.question',
|
||||
'attempts.answers.option'
|
||||
]);
|
||||
|
||||
return \Inertia\Inertia::render('Admin/Candidates/Show', [
|
||||
'candidate' => $candidate
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(Candidate $candidate)
|
||||
{
|
||||
$user = $candidate->user;
|
||||
|
||||
// Delete files
|
||||
foreach ($candidate->documents as $doc) {
|
||||
Storage::disk('local')->delete($doc->file_path);
|
||||
}
|
||||
|
||||
// Delete user (cascades to candidate, documents, attempts via DB constraints usually)
|
||||
$user->delete();
|
||||
|
||||
return redirect()->route('admin.candidates.index')->with('success', 'Candidat supprimé avec succès.');
|
||||
}
|
||||
|
||||
public function update(Request $request, Candidate $candidate)
|
||||
{
|
||||
$request->validate([
|
||||
'cv' => 'nullable|file|mimes:pdf|max:5120',
|
||||
'cover_letter' => 'nullable|file|mimes:pdf|max:5120',
|
||||
]);
|
||||
|
||||
if ($request->hasFile('cv')) {
|
||||
$this->replaceDocument($candidate, $request->file('cv'), 'cv');
|
||||
}
|
||||
|
||||
if ($request->hasFile('cover_letter')) {
|
||||
$this->replaceDocument($candidate, $request->file('cover_letter'), 'cover_letter');
|
||||
}
|
||||
|
||||
return back()->with('success', 'Documents mis à jour avec succès.');
|
||||
}
|
||||
|
||||
public function resetPassword(Candidate $candidate)
|
||||
{
|
||||
$password = Str::random(10);
|
||||
$candidate->user->update([
|
||||
'password' => Hash::make($password)
|
||||
]);
|
||||
|
||||
return back()->with('success', 'Nouveau mot de passe généré: ' . $password);
|
||||
}
|
||||
|
||||
private function replaceDocument(Candidate $candidate, $file, string $type)
|
||||
{
|
||||
// Delete old one if exists
|
||||
$oldDoc = $candidate->documents()->where('type', $type)->first();
|
||||
if ($oldDoc) {
|
||||
Storage::disk('local')->delete($oldDoc->file_path);
|
||||
$oldDoc->delete();
|
||||
}
|
||||
|
||||
$this->storeDocument($candidate, $file, $type);
|
||||
}
|
||||
|
||||
private function storeDocument(Candidate $candidate, $file, string $type)
|
||||
{
|
||||
if (!$file) {
|
||||
return;
|
||||
}
|
||||
|
||||
$path = $file->store('private/documents/' . $candidate->id, 'local');
|
||||
|
||||
Document::create([
|
||||
'candidate_id' => $candidate->id,
|
||||
'type' => $type,
|
||||
'file_path' => $path,
|
||||
'original_name' => $file->getClientOriginalName(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user