api ms group by slug, views, ins
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user