Compare commits
2 Commits
gestion_de
...
validation
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cfb482ddf5 | ||
| fe87272b43 |
@@ -7,6 +7,7 @@ use App\Http\Requests\UpdateBilletRequest;
|
|||||||
use App\Models\Billet;
|
use App\Models\Billet;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Database\QueryException;
|
||||||
|
|
||||||
class BilletController extends Controller
|
class BilletController extends Controller
|
||||||
{
|
{
|
||||||
@@ -15,12 +16,11 @@ class BilletController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
Log::info("coucou");
|
|
||||||
try {
|
try {
|
||||||
$billets = Billet::all();
|
$billets = Billet::all();
|
||||||
} catch (\Illuminate\Database\QueryException $e) {
|
} catch (QueryException $e) {
|
||||||
Log::error('Erreur accès à la base de donnée');
|
Log::channel('projectError')->error('Erreur d\'accès à la base de données');
|
||||||
return view('errors.unavailable');
|
return view('errors.dberror');
|
||||||
}
|
}
|
||||||
return view('index', compact('billets'));
|
return view('index', compact('billets'));
|
||||||
}
|
}
|
||||||
@@ -47,7 +47,11 @@ class BilletController extends Controller
|
|||||||
public function show(Billet $billet)
|
public function show(Billet $billet)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
$commentaires = $billet->commentaires;
|
try {
|
||||||
|
$commentaires = $billet->commentaires;
|
||||||
|
} catch (QueryException $e) {
|
||||||
|
return view('errors.dberror');
|
||||||
|
}
|
||||||
return view('vBillet', compact('billet', 'commentaires'));
|
return view('vBillet', compact('billet', 'commentaires'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ namespace App\Http\Controllers;
|
|||||||
use App\Http\Requests\StoreCommentaireRequest;
|
use App\Http\Requests\StoreCommentaireRequest;
|
||||||
use App\Http\Requests\UpdateCommentaireRequest;
|
use App\Http\Requests\UpdateCommentaireRequest;
|
||||||
use App\Models\Commentaire;
|
use App\Models\Commentaire;
|
||||||
|
use App\Models\Billet;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
class CommentaireController extends Controller
|
class CommentaireController extends Controller
|
||||||
{
|
{
|
||||||
@@ -19,9 +21,20 @@ class CommentaireController extends Controller
|
|||||||
/**
|
/**
|
||||||
* Show the form for creating a new resource.
|
* Show the form for creating a new resource.
|
||||||
*/
|
*/
|
||||||
public function create()
|
public function create($idBillet)
|
||||||
{
|
{
|
||||||
//
|
try {
|
||||||
|
$billet = Billet::findOrFail($idBillet);
|
||||||
|
}
|
||||||
|
catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
|
||||||
|
Log::channel('projectError')->error('Commentaire : Billet non trouvé');
|
||||||
|
return view('errors.unavailable');
|
||||||
|
}
|
||||||
|
catch (\Illuminate\Database\QueryException $e) {
|
||||||
|
Log::channel('projectError')->error('Erreur accès base de données');
|
||||||
|
return view('errors.dberror');
|
||||||
|
}
|
||||||
|
return view('vCommenter', compact('idBillet', 'billet'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -29,7 +42,15 @@ class CommentaireController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function store(StoreCommentaireRequest $request)
|
public function store(StoreCommentaireRequest $request)
|
||||||
{
|
{
|
||||||
//
|
try {
|
||||||
|
Commentaire::create($request->all());
|
||||||
|
}
|
||||||
|
catch (\Illuminate\Database\QueryException $e) {
|
||||||
|
Log::channel('projectError')->error('Insertion en base de données impossible');
|
||||||
|
return view('errors.dberror');
|
||||||
|
}
|
||||||
|
Log::channel('projectInfo')->info('Commentaire ajouté par : '.$request->ip());
|
||||||
|
return view('vConfirmStore');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class StoreCommentaireRequest extends FormRequest
|
|||||||
*/
|
*/
|
||||||
public function authorize(): bool
|
public function authorize(): bool
|
||||||
{
|
{
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -22,7 +22,24 @@ class StoreCommentaireRequest extends FormRequest
|
|||||||
public function rules(): array
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
//
|
'COM_AUTEUR' => ['required', 'alpha', 'max:100'],
|
||||||
|
'COM_CONTENU' => ['required', 'string', 'max:200'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the error messages for the defined validation rules.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function messages() {
|
||||||
|
return [
|
||||||
|
'COM_AUTEUR.required' => 'Le nom de l\'auteur est requis.',
|
||||||
|
'COM_AUTEUR.alpha' => 'Le nom de l\'auteur ne doit contenir que des lettres.',
|
||||||
|
'COM_AUTEUR.max' => 'Le nom de l\'auteur ne doit pas dépasser 100 caractères.',
|
||||||
|
'COM_CONTENU.required' => 'Le contenu du commentaire est requis.',
|
||||||
|
'COM_CONTENU.string' => 'Le contenu du commentaire doit être une chaîne de caractères.',
|
||||||
|
'COM_CONTENU.max' => 'Le contenu du commentaire ne doit pas dépasser 200 caractères.',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -127,6 +127,18 @@ return [
|
|||||||
'path' => storage_path('logs/laravel.log'),
|
'path' => storage_path('logs/laravel.log'),
|
||||||
],
|
],
|
||||||
|
|
||||||
|
"projectError" => [
|
||||||
|
'driver' => 'single',
|
||||||
|
'path' => storage_path('logs/project.log'),
|
||||||
|
'level' => 'error',
|
||||||
|
],
|
||||||
|
|
||||||
|
"projectInfo" => [
|
||||||
|
'driver' => 'single',
|
||||||
|
'path' => storage_path('logs/project.log'),
|
||||||
|
'level' => 'info',
|
||||||
|
],
|
||||||
|
|
||||||
],
|
],
|
||||||
|
|
||||||
];
|
];
|
||||||
|
|||||||
5
laravel/resources/views/errors/dberror.blade.php
Normal file
5
laravel/resources/views/errors/dberror.blade.php
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
@extends('layout')
|
||||||
|
|
||||||
|
@section('contenu')
|
||||||
|
<h2>Une erreur est survenue. Veuillez réessayer ultérieurement</h2>
|
||||||
|
@endsection
|
||||||
@@ -22,6 +22,7 @@
|
|||||||
@endforeach
|
@endforeach
|
||||||
</p>
|
</p>
|
||||||
<p>{{ $billet->BIL_CONTENU }}</p>
|
<p>{{ $billet->BIL_CONTENU }}</p>
|
||||||
|
<a href="{{ route('commenter', ['id'=>$billet->id]) }}"><h3 class="titre-billet">Ecrire un commentaire</h3></a>
|
||||||
</article>
|
</article>
|
||||||
@endforeach
|
@endforeach
|
||||||
@endsection
|
@endsection
|
||||||
40
laravel/resources/views/vCommenter.blade.php
Normal file
40
laravel/resources/views/vCommenter.blade.php
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
@extends('layout')
|
||||||
|
@section('contenu')
|
||||||
|
@php
|
||||||
|
$today = date('Y-m-d');
|
||||||
|
@endphp
|
||||||
|
<h2>Commenter le billet : {{ $billet->BIL_TITRE }}</h2>
|
||||||
|
<form action="{{ route('commentaires.store') }}" method="POST">
|
||||||
|
@csrf
|
||||||
|
<p><i>Complétez le formulaire. Les champs marqué par </i><em>*</em> sont <em>obligatoires</em></p>
|
||||||
|
<fieldset>
|
||||||
|
<legend>Entrez votre commentaire :</legend>
|
||||||
|
<div>
|
||||||
|
<label for="COM_AUTEUR">Nom : <em>*</em></label>
|
||||||
|
<input type="text" @error('COM_AUTEUR') is-invalid @enderror name="COM_AUTEUR" placeholder="Entrez votre nom"></input>
|
||||||
|
@error('COM_AUTEUR')
|
||||||
|
<div><em>{{ $message }}</em></div>
|
||||||
|
@enderror
|
||||||
|
</div>
|
||||||
|
<br>
|
||||||
|
<div>
|
||||||
|
<label for="COM_CONTENU">Message : <em>*</em></label>
|
||||||
|
<textarea name="COM_CONTENU" @error('COM_CONTENU') is-invalid @enderror name="COM_CONTENU" placeholder="Votre commentaire :"></textarea>
|
||||||
|
@error('COM_CONTENU')
|
||||||
|
<div><em>{{ $message }}</em></div>
|
||||||
|
@enderror
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<input type="hidden" name="COM_DATE" value="{{ $today }}"></input>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<input type="hidden" name="created_at" value="{{ $today }}"></input>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<input type="hidden" name="billet_id" value="{{ $idBillet }}"></input>
|
||||||
|
</div>
|
||||||
|
<br>
|
||||||
|
<button type="submit">Envoyer !</button>
|
||||||
|
</fieldset>
|
||||||
|
</form>
|
||||||
|
@endsection
|
||||||
5
laravel/resources/views/vConfirmStore.blade.php
Normal file
5
laravel/resources/views/vConfirmStore.blade.php
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
@extends('layout')
|
||||||
|
|
||||||
|
@section('contenu')
|
||||||
|
<h2>Merci ! Nous avons bien enregistré votre commentaire.</h2>
|
||||||
|
@endsection
|
||||||
@@ -2,9 +2,13 @@
|
|||||||
|
|
||||||
use App\Http\Controllers\BilletController;
|
use App\Http\Controllers\BilletController;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
use App\Http\Controllers\CommentaireController;
|
||||||
|
|
||||||
Route::get('/', function () {
|
Route::get('/', fn() => view('welcome'));
|
||||||
return view('welcome');
|
|
||||||
});
|
|
||||||
|
|
||||||
Route::resource('billets', BilletController::class);
|
Route::resource('billets', BilletController::class);
|
||||||
|
|
||||||
|
Route::resource('commentaires', CommentaireController::class);
|
||||||
|
|
||||||
|
// Route to create a comment
|
||||||
|
Route::get('commenter/{id}', [CommentaireController::class, 'create'])->name('commenter');
|
||||||
Reference in New Issue
Block a user