From f7838ea66576b5d5317fca3b0f3a8dba308b2118 Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Fri, 25 Oct 2024 15:47:11 +0300 Subject: [PATCH] api ms group by slug, views, ins --- app/modules/module_shop/ModuleShopModule.php | 3 + .../controllers/ModuleShopRestController.php | 61 ++++++++++++++++++- ..._10_15_135454_create_module_shop_table.php | 2 + app/modules/module_shop/models/ModuleShop.php | 6 +- app/modules/module_shop/routs/ms.php | 3 + 5 files changed, 72 insertions(+), 3 deletions(-) diff --git a/app/modules/module_shop/ModuleShopModule.php b/app/modules/module_shop/ModuleShopModule.php index f1e0e9a..a5c6be8 100644 --- a/app/modules/module_shop/ModuleShopModule.php +++ b/app/modules/module_shop/ModuleShopModule.php @@ -16,6 +16,9 @@ class ModuleShopModule extends Module $this->migrationService = new MigrationService(); } + /** + * @throws \Exception + */ public function init(): void { $this->migrationService->runAtPath("{APP}/modules/module_shop/migrations"); diff --git a/app/modules/module_shop/controllers/ModuleShopRestController.php b/app/modules/module_shop/controllers/ModuleShopRestController.php index 760c4ea..e9ab3d8 100644 --- a/app/modules/module_shop/controllers/ModuleShopRestController.php +++ b/app/modules/module_shop/controllers/ModuleShopRestController.php @@ -3,6 +3,8 @@ namespace app\modules\module_shop\controllers; use app\modules\module_shop\models\ModuleShop; +use JetBrains\PhpStorm\NoReturn; +use kernel\helpers\Debug; use kernel\Request; use kernel\RestController; @@ -14,20 +16,21 @@ class ModuleShopRestController extends RestController $this->model = new ModuleShop(); } - public function actionIndex(): void + #[NoReturn] public function actionIndex(): void { $request = new Request(); $page = $request->get('page') ?? 1; $perPage = $request->get('per_page') ?? 10; $query = $this->model->query(); + $query->orderBy('created_at', 'DESC'); + if ($page > 1) { $query->skip(($page - 1) * $perPage)->take($perPage); } else { $query->take($perPage); } - $query->groupBy("slug")->orderBy("id", "ASC"); $expand = $this->expand(); $expandParams = explode( ",", $request->get('expand') ?? ""); $finalExpand = array_intersect($expandParams, $expand); @@ -40,4 +43,58 @@ class ModuleShopRestController extends RestController $this->renderApi($res); } + #[NoReturn] public function actionIndexGroupBySlug(): void + { + $request = new Request(); + $page = $request->get('page') ?? 1; + $perPage = $request->get('per_page') ?? 10; + $query = $this->model->query(); + + $query->select('ms1.*') + ->from('module_shop as ms1') + ->leftJoin('module_shop as ms2', function ($join) { + $join->on('ms1.slug', '=', 'ms2.slug') + ->on('ms1.id', '<', 'ms2.id'); + }) + ->where('ms2.slug', '=', null); + + if ($page > 1) { + $query->skip(($page - 1) * $perPage)->take($perPage); + } else { + $query->take($perPage); + } + + $expand = $this->expand(); + $expandParams = explode( ",", $request->get('expand') ?? ""); + $finalExpand = array_intersect($expandParams, $expand); + if ($finalExpand) { + $res = $query->get()->load($finalExpand)->toArray(); + } else { + $res = $query->get()->toArray(); + } + + $this->renderApi($res); + } + + #[NoReturn] public function actionView($id): void + { + $expand = $this->expand(); + $request = new Request(); + $expandParams = explode( ",", $request->get('expand') ?? ""); + $model = $this->model->where("id", $id)->first(); + $model->views++; + + $finalExpand = array_intersect($expandParams, $expand); + if ($finalExpand){ + $model->load($finalExpand); + } + $res = []; + if ($model){ + $res = $model->toArray(); + } + + $model->save(); + $this->renderApi($res); + } + } \ No newline at end of file diff --git a/app/modules/module_shop/migrations/2024_10_15_135454_create_module_shop_table.php b/app/modules/module_shop/migrations/2024_10_15_135454_create_module_shop_table.php index 195bfc7..aaec4ab 100644 --- a/app/modules/module_shop/migrations/2024_10_15_135454_create_module_shop_table.php +++ b/app/modules/module_shop/migrations/2024_10_15_135454_create_module_shop_table.php @@ -20,6 +20,8 @@ return new class extends Migration { $table->string("dependence", 255)->nullable(true); $table->text("path_to_archive")->nullable(false); $table->integer("status")->default(1); + $table->integer("installations")->nullable(false)->default(0); + $table->integer("views")->nullable(false)->default(0); $table->timestamps(); }); } diff --git a/app/modules/module_shop/models/ModuleShop.php b/app/modules/module_shop/models/ModuleShop.php index dbc05b1..54b1a2e 100644 --- a/app/modules/module_shop/models/ModuleShop.php +++ b/app/modules/module_shop/models/ModuleShop.php @@ -12,6 +12,8 @@ use Illuminate\Database\Eloquent\Model; * @property string $description * @property string $author * @property int $status + * @property int $installations + * @property int $views * @property string $path_to_archive * @property string $dependence */ @@ -22,7 +24,7 @@ class ModuleShop extends Model protected $table = "module_shop"; - protected $fillable = ['name', 'slug', 'version', 'description', 'author', 'status', 'dependence']; + protected $fillable = ['name', 'slug', 'version', 'description', 'author', 'status', 'dependence', 'installations', 'views']; public static function labels(): array { @@ -34,6 +36,8 @@ class ModuleShop extends Model 'status' => 'Статус', 'slug' => 'Slug', 'dependence' => 'Зависимости', + 'installations' => 'Установки', + 'views' => 'Просмотры', ]; } diff --git a/app/modules/module_shop/routs/ms.php b/app/modules/module_shop/routs/ms.php index d64a2b2..1b16002 100644 --- a/app/modules/module_shop/routs/ms.php +++ b/app/modules/module_shop/routs/ms.php @@ -21,6 +21,9 @@ App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){ App::$collector->group(['before' => 'bearer'], function (CgRouteCollector $router){ + App::$collector->group(["prefix" => "module_shop"], function (CgRouteCollector $router){ + App::$collector->get('/gb_slug', [\app\modules\module_shop\controllers\ModuleShopRestController::class, 'actionIndexGroupBySlug']); + }); $router->rest("module_shop", [\app\modules\module_shop\controllers\ModuleShopRestController::class]); }); }); \ No newline at end of file