2024-09-26 14:47:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace kernel\modules\post\controllers;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-09-26 16:49:01 +03:00
|
|
|
use JetBrains\PhpStorm\NoReturn;
|
2024-10-22 11:09:35 +03:00
|
|
|
use kernel\App;
|
|
|
|
use kernel\helpers\Debug;
|
2024-09-26 14:47:13 +03:00
|
|
|
use kernel\modules\post\models\Post;
|
2024-10-22 11:09:35 +03:00
|
|
|
use kernel\Request;
|
2024-09-26 14:47:13 +03:00
|
|
|
use kernel\RestController;
|
|
|
|
|
|
|
|
class PostRestController extends RestController
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->model = new Post();
|
|
|
|
}
|
|
|
|
|
2024-10-07 11:31:16 +03:00
|
|
|
protected function expand(): array
|
2024-09-26 14:47:13 +03:00
|
|
|
{
|
2024-10-07 11:31:16 +03:00
|
|
|
return ["user"];
|
2024-09-26 14:47:13 +03:00
|
|
|
}
|
2024-09-26 16:49:01 +03:00
|
|
|
|
2024-10-22 11:09:35 +03:00
|
|
|
public function actionIndex(): void
|
|
|
|
{
|
|
|
|
$request = new Request();
|
|
|
|
$page = $request->get('page') ?? 1;
|
|
|
|
$perPage = $request->get('per_page') ?? 10;
|
|
|
|
$query = $this->model->query();
|
|
|
|
if (App::$user){
|
|
|
|
$query->where("user_id", App::$user->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-26 14:47:13 +03:00
|
|
|
}
|