finished api

This commit is contained in:
Clément
2025-06-04 19:50:52 +02:00
parent 1551f6226e
commit f488f7e01d
32 changed files with 1078 additions and 30 deletions

View 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)
{
//
}
}

View 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)
{
//
}
}

View 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)
{
//
}
}

View 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 [
//
];
}
}

View 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);
}
}

View 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 [
//
];
}
}

View 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 [
//
];
}
}

View 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')),
];
}
}

View 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,
];
}
}

View 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,
];
}
}

View 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,
];
}
}

View 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);
}
}

View 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);
}
}

View File

@@ -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.

View 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;
}
}

View 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;
}
}