igfs/app/modules/module_shop/controllers/ModuleShopRestController.php

108 lines
3.0 KiB
PHP
Raw Normal View History

2024-10-16 17:54:33 +03:00
<?php
namespace app\modules\module_shop\controllers;
use app\modules\module_shop\models\ModuleShop;
2024-10-25 15:47:11 +03:00
use JetBrains\PhpStorm\NoReturn;
use kernel\helpers\Debug;
2024-10-23 16:16:47 +03:00
use kernel\Request;
2024-10-16 17:54:33 +03:00
use kernel\RestController;
class ModuleShopRestController extends RestController
{
public function __construct()
{
$this->model = new ModuleShop();
}
2024-10-25 15:47:11 +03:00
#[NoReturn] public function actionIndex(): void
2024-10-23 16:16:47 +03:00
{
$request = new Request();
$page = $request->get('page') ?? 1;
$perPage = $request->get('per_page') ?? 10;
$query = $this->model->query();
2024-10-25 15:47:11 +03:00
$query->orderBy('created_at', 'DESC');
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 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);
2024-10-23 16:16:47 +03:00
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);
}
2024-10-25 15:47:11 +03:00
#[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);
}
2024-11-14 01:56:07 +03:00
public function actionInstall($id): void
2024-11-08 14:48:52 +03:00
{
$model = $this->model->where("id", $id)->first();
$model->installations++;
$model->save();
2024-11-14 01:56:07 +03:00
$this->renderApi($model->toArray());
2024-11-08 14:48:52 +03:00
}
2024-10-16 17:54:33 +03:00
}