From 29c744c0ec9a4a3ae0c6a95addb642c2151d5db1 Mon Sep 17 00:00:00 2001 From: OMGiTzPomPom Date: Fri, 10 Mar 2023 17:43:26 +0100 Subject: [PATCH] td --- app/Http/Controllers/CategoriesController.php | 63 +++++++++++++++++++ app/Http/Controllers/ClientsController.php | 6 +- app/Http/Controllers/CommandesController.php | 63 +++++++++++++++++++ app/Http/Controllers/Controller.php | 23 +++++++ app/Http/Controllers/ProduitsController.php | 63 +++++++++++++++++++ app/Models/Categories.php | 9 ++- app/Models/Clients.php | 8 ++- app/Models/Commandes.php | 14 ++++- app/Models/Lignes_Commandes.php | 7 ++- app/Models/Model.php | 43 +++++++++++++ app/Models/Produits.php | 9 ++- routes/api.php | 9 ++- 12 files changed, 305 insertions(+), 12 deletions(-) create mode 100644 app/Http/Controllers/CategoriesController.php create mode 100644 app/Http/Controllers/CommandesController.php create mode 100644 app/Http/Controllers/ProduitsController.php create mode 100644 app/Models/Model.php diff --git a/app/Http/Controllers/CategoriesController.php b/app/Http/Controllers/CategoriesController.php new file mode 100644 index 0000000..7ad8037 --- /dev/null +++ b/app/Http/Controllers/CategoriesController.php @@ -0,0 +1,63 @@ +json(['Clients' => Clients::all(), 'status' => 'OK'], 200); + public function index(Request $request, Bool $paginate = true) { + return parent::index($request, $paginate); } /** diff --git a/app/Http/Controllers/CommandesController.php b/app/Http/Controllers/CommandesController.php new file mode 100644 index 0000000..b539e16 --- /dev/null +++ b/app/Http/Controllers/CommandesController.php @@ -0,0 +1,63 @@ +route()->getName()); + $model = $route[1]; + $class = 'App\\Models\\' .ucfirst($model); + + $errors = []; + + $instances = $class::fetchAll($request, $paginate); + + foreach ($instances as $i) { + $i->setDataTree(); + } + + return response()->json([$model => $instances, 'status' => 'OK'], 200); + } catch (\Exception $e) { + Log::debug($e->getMessage() . ' ' .$e->getFile() . ' ' .$e->getLine()); + return response()->json(['status' => 'ERROR', 'message' => $e->getMessage(), 'errors' => $errors], 404); + } + } } diff --git a/app/Http/Controllers/ProduitsController.php b/app/Http/Controllers/ProduitsController.php new file mode 100644 index 0000000..1b8e66f --- /dev/null +++ b/app/Http/Controllers/ProduitsController.php @@ -0,0 +1,63 @@ +belongsToMany('App\Models\Produits', 'produits_categories', 'categories_id', 'products_id'); } + public function setDataTree() { + foreach ($this->Produits as $p) { + $p->setCalculatedFields(); + } + } + + + } diff --git a/app/Models/Clients.php b/app/Models/Clients.php index 7bd5bdf..13c287f 100644 --- a/app/Models/Clients.php +++ b/app/Models/Clients.php @@ -3,7 +3,6 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; -use Illuminate\Database\Eloquent\Model; class Clients extends Model { @@ -23,4 +22,11 @@ class Clients extends Model { return $this->hasMany('App\Models\Commandes', 'clients_id', 'id'); } + + public function setDataTree() { + foreach($this->Commandes as $commande) { + $commande->setCalculatedFields(); + $commande->setDataTree(); + } + } } diff --git a/app/Models/Commandes.php b/app/Models/Commandes.php index aa0265f..4cbdf78 100644 --- a/app/Models/Commandes.php +++ b/app/Models/Commandes.php @@ -3,7 +3,6 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; -use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; @@ -28,6 +27,17 @@ class Commandes extends Model public function LignesCommandes(): HasMany { - return $this->hasMany('App\Models\Lignes_Commandes', 'comandes_id', 'id'); + return $this->hasMany('App\Models\Lignes_Commandes', 'commandes_id', 'id'); } + + public function setDataTree() { + if($this->clients_id > 0) { + $this->Client->setCalculatedFields(); + } + + foreach($this->LignesCommandes as $lc) { + $lc->setCalculatedFields(); + } + } + } diff --git a/app/Models/Lignes_Commandes.php b/app/Models/Lignes_Commandes.php index 7b5f0a9..0aa9004 100644 --- a/app/Models/Lignes_Commandes.php +++ b/app/Models/Lignes_Commandes.php @@ -3,7 +3,6 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; -use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; class Lignes_Commandes extends Model @@ -29,4 +28,10 @@ class Lignes_Commandes extends Model return $this->belongsTo('App\Models\Commandes', 'commandes_id', 'id'); } + public function setDataTree(){ + foreach($this->Produits as $produit){ + $produit->setCalculatedFields(); + } + } + } diff --git a/app/Models/Model.php b/app/Models/Model.php new file mode 100644 index 0000000..b28975e --- /dev/null +++ b/app/Models/Model.php @@ -0,0 +1,43 @@ +setCalculatedFields(); + } + */ + return $instance; + } + + public static function fetchAll(Request $request, Bool $paginate = true) { + $class = get_called_class(); + if($paginate) { + $instances = $class::paginate(24); + } else { + $instances = $class::get(); + } + + foreach ($instances as $i) { + $i->setCalculatedFields(); + } + + return $instances; + } + + public function setCalculatedFields() { + // + } +} diff --git a/app/Models/Produits.php b/app/Models/Produits.php index b3d01b1..ff49d05 100644 --- a/app/Models/Produits.php +++ b/app/Models/Produits.php @@ -3,7 +3,6 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; -use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; @@ -30,4 +29,12 @@ class Produits extends Model return $this->hasMany('App\Models\Lignes_Commandes', 'produits_id', 'id'); } + public function setDataTree(){ + foreach($this->Categories as $categorie){ + $categorie->setCalculatedFields(); + } + } + + + } diff --git a/routes/api.php b/routes/api.php index ff9afb0..c96e5a3 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,6 +1,9 @@ get('/user', function (Request $request) { return $request->user(); }); -Route::apiResource('/clients', ClientsController::class); +Route::apiResource('/clients', ClientsController::class, ['as' => 'api']); +Route::apiResource('/categories', CategoriesController::class, ['as' => 'api']); +Route::apiResource('/commandes', CommandesController::class, ['as' => 'api']); +Route::apiResource('/produits', ProduitsController::class, ['as' => 'api']); -//Route::get('/clients', [ClientsController::class, 'index']);