finished api
This commit is contained in:
69
laravel/app/Http/Controllers/BilletController.php
Normal file
69
laravel/app/Http/Controllers/BilletController.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Billet;
|
||||
use App\Http\Resources\BilletResource;
|
||||
use App\Http\Resources\BilletsResource;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
|
||||
class BilletController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
// Le résultat de la requête est retourné directement en JSON.
|
||||
// return Billet::all();
|
||||
return BilletsResource::collection(Billet::all());
|
||||
} catch (\Illuminate\Database\QueryException $e) {
|
||||
Log::channel('projectLog')->error('Erreur accès base de données');
|
||||
return response()->json([
|
||||
'message' => 'Ressource indisponible.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show($id) {
|
||||
try {
|
||||
return new BilletResource(Billet::with('commentaires', 'commentaires.user')->findOrFail($id));
|
||||
}
|
||||
catch (\Illuminate\Database\QueryException $e) {
|
||||
Log::error('Erreur accès base de données');
|
||||
return response()->json([
|
||||
'message' => 'Ressource indisponible.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
59
laravel/app/Http/Controllers/CommentaireController.php
Normal file
59
laravel/app/Http/Controllers/CommentaireController.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\StoreCommentairesRequest;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Commentaire;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class CommentaireController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(StoreCommentairesRequest $request)
|
||||
{
|
||||
try {
|
||||
$commentaire = Commentaire::create($request->validated());
|
||||
return response()->json($commentaire, 201);
|
||||
} catch (\Illuminate\Database\QueryException $e) {
|
||||
Log::channel('projectError')->error('Erreur accès base de données');
|
||||
return response()->json([
|
||||
'message' => $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
66
laravel/app/Http/Controllers/UserController.php
Normal file
66
laravel/app/Http/Controllers/UserController.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\UserResource;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
// Validate the request data
|
||||
$validatedData = $request->validate([
|
||||
'name' => 'required|string|max:50',
|
||||
'email' => 'required|email|max:50|unique:users',
|
||||
'password' => 'required|string|min:8|',
|
||||
]);
|
||||
|
||||
// Create a new user
|
||||
$user = \App\Models\User::create([
|
||||
'name' => $validatedData['name'],
|
||||
'email' => $validatedData['email'],
|
||||
'password' => bcrypt($validatedData['password']),
|
||||
]);
|
||||
$token = $user->createToken('auth_token')->plainTextToken;
|
||||
return response()->json([
|
||||
'access_token' => $token,
|
||||
'token_type' => 'Bearer',
|
||||
], 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Request $request)
|
||||
{
|
||||
return new UserResource($request->user());
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
28
laravel/app/Http/Requests/StoreBilletRequest.php
Normal file
28
laravel/app/Http/Requests/StoreBilletRequest.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class StoreBilletRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
}
|
||||
50
laravel/app/Http/Requests/StoreCommentairesRequest.php
Normal file
50
laravel/app/Http/Requests/StoreCommentairesRequest.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class StoreCommentairesRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'COM_DATE' => ['required', 'date'],
|
||||
'COM_CONTENU' => ['required', 'string', 'max:200'],
|
||||
'billet_id' => ['required', 'integer'],
|
||||
'user_id' => ['required', 'integer', Rule::exists('users', 'id')->where(function ($query) {
|
||||
$query->where('id', $this->user()->id);
|
||||
})],
|
||||
];
|
||||
}
|
||||
|
||||
public function failedValidation(\Illuminate\Contracts\Validation\Validator $validator) {
|
||||
throw new HttpResponseException(
|
||||
response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Validation failed',
|
||||
'data' => $validator->errors(),
|
||||
], 422)
|
||||
);
|
||||
}
|
||||
|
||||
public function validated($key = null, $default = null)
|
||||
{
|
||||
return parent::validated($key, $default);
|
||||
}
|
||||
}
|
||||
28
laravel/app/Http/Requests/UpdateBilletRequest.php
Normal file
28
laravel/app/Http/Requests/UpdateBilletRequest.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UpdateBilletRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
}
|
||||
28
laravel/app/Http/Requests/UpdateCommentairesRequest.php
Normal file
28
laravel/app/Http/Requests/UpdateCommentairesRequest.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UpdateCommentairesRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
}
|
||||
24
laravel/app/Http/Resources/BilletResource.php
Normal file
24
laravel/app/Http/Resources/BilletResource.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class BilletResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'Date' => $this->BIL_DATE,
|
||||
'Titre' => $this->BIL_TITRE,
|
||||
'Contenu' => $this->BIL_CONTENU,
|
||||
'Commentaires' => CommentaireResource::collection($this->whenLoaded('commentaires')),
|
||||
];
|
||||
}
|
||||
}
|
||||
23
laravel/app/Http/Resources/BilletsResource.php
Normal file
23
laravel/app/Http/Resources/BilletsResource.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class BilletsResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'Date' => $this->BIL_DATE,
|
||||
'Titre' => $this->BIL_TITRE,
|
||||
'Contenu' => $this->BIL_CONTENU,
|
||||
];
|
||||
}
|
||||
}
|
||||
23
laravel/app/Http/Resources/CommentaireResource.php
Normal file
23
laravel/app/Http/Resources/CommentaireResource.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class CommentaireResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'Date' => $this->COM_DATE,
|
||||
'Auteur' => $this->user->name,
|
||||
'Contenu' => $this->COM_CONTENU,
|
||||
];
|
||||
}
|
||||
}
|
||||
24
laravel/app/Http/Resources/UserResource.php
Normal file
24
laravel/app/Http/Resources/UserResource.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class UserResource extends JsonResource
|
||||
{
|
||||
public static $wrap = 'user';
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->getKey(),
|
||||
'nom' => $this->name,
|
||||
'email' => $this->email,
|
||||
];
|
||||
}
|
||||
}
|
||||
31
laravel/app/Models/Billet.php
Normal file
31
laravel/app/Models/Billet.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Billet extends Model
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\BilletFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'BIL_DATE',
|
||||
'BIL_TITRE',
|
||||
'BIL_CONTENU',
|
||||
];
|
||||
|
||||
protected $hidden = [
|
||||
'id',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
];
|
||||
|
||||
// Un billet a plusieurs commentaires.
|
||||
// Cette fonction sera utile pour afficher les commentaires d'un billet sélectionné.
|
||||
public function commentaires()
|
||||
{
|
||||
return $this->hasMany(Commentaire::class);
|
||||
}
|
||||
}
|
||||
60
laravel/app/Models/Commentaire.php
Normal file
60
laravel/app/Models/Commentaire.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Created by Reliese Model.
|
||||
*/
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Class Commentaire
|
||||
*
|
||||
* @property int $id
|
||||
* @property Carbon $COM_DATE
|
||||
* @property string $COM_AUTEUR
|
||||
* @property string $COM_CONTENU
|
||||
* @property int $billet_id
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
*
|
||||
* @property Billet $billet
|
||||
*
|
||||
* @package App\Models
|
||||
*/
|
||||
class Commentaire extends Model
|
||||
{
|
||||
protected $table = 'commentaires';
|
||||
|
||||
protected $casts = [
|
||||
'COM_DATE' => 'datetime',
|
||||
'billet_id' => 'int'
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'COM_DATE',
|
||||
'COM_AUTEUR',
|
||||
'COM_CONTENU',
|
||||
'billet_id',
|
||||
'user_id'
|
||||
];
|
||||
|
||||
protected $hidden = [
|
||||
'id',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'billet_id'
|
||||
];
|
||||
|
||||
public function billet()
|
||||
{
|
||||
return $this->belongsTo(Billet::class);
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
@@ -6,11 +6,12 @@ namespace App\Models;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
use HasApiTokens, HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
|
||||
66
laravel/app/Policies/BilletPolicy.php
Normal file
66
laravel/app/Policies/BilletPolicy.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\Billet;
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Access\Response;
|
||||
|
||||
class BilletPolicy
|
||||
{
|
||||
/**
|
||||
* Determine whether the user can view any models.
|
||||
*/
|
||||
public function viewAny(User $user): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can view the model.
|
||||
*/
|
||||
public function view(User $user, Billet $billet): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can create models.
|
||||
*/
|
||||
public function create(User $user): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can update the model.
|
||||
*/
|
||||
public function update(User $user, Billet $billet): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can delete the model.
|
||||
*/
|
||||
public function delete(User $user, Billet $billet): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can restore the model.
|
||||
*/
|
||||
public function restore(User $user, Billet $billet): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can permanently delete the model.
|
||||
*/
|
||||
public function forceDelete(User $user, Billet $billet): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
66
laravel/app/Policies/CommentairesPolicy.php
Normal file
66
laravel/app/Policies/CommentairesPolicy.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\Commentaires;
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Access\Response;
|
||||
|
||||
class CommentairesPolicy
|
||||
{
|
||||
/**
|
||||
* Determine whether the user can view any models.
|
||||
*/
|
||||
public function viewAny(User $user): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can view the model.
|
||||
*/
|
||||
public function view(User $user, Commentaires $commentaires): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can create models.
|
||||
*/
|
||||
public function create(User $user): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can update the model.
|
||||
*/
|
||||
public function update(User $user, Commentaires $commentaires): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can delete the model.
|
||||
*/
|
||||
public function delete(User $user, Commentaires $commentaires): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can restore the model.
|
||||
*/
|
||||
public function restore(User $user, Commentaires $commentaires): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can permanently delete the model.
|
||||
*/
|
||||
public function forceDelete(User $user, Commentaires $commentaires): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user