api ms group by slug, views, ins
This commit is contained in:
parent
45d51c7119
commit
f7838ea665
@ -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");
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
});
|
||||
}
|
||||
|
@ -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' => 'Просмотры',
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -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]);
|
||||
});
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user