initial project
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers\Frontend;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
|
||||
class SlideshowController extends BaseController
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$slideshowModel = new \App\Models\SlideshowModel();
|
||||
|
||||
$allowedOrderFields = [
|
||||
'order', 'created_at', 'updated_at', 'title', 'status'
|
||||
];
|
||||
|
||||
$orderBy = $this->request->getGet('order_by') ?? 'order';
|
||||
$sort = strtolower($this->request->getGet('sort')) === 'desc' ? 'DESC' : 'ASC';
|
||||
$page = (int) ($this->request->getGet('page') ?? 1);
|
||||
$limit = (int) ($this->request->getGet('limit') ?? 20);
|
||||
|
||||
if (!in_array($orderBy, $allowedOrderFields)) {
|
||||
$orderBy = 'order';
|
||||
}
|
||||
|
||||
$builder = $slideshowModel->orderBy($orderBy, $sort)->where('deleted_at', null);
|
||||
|
||||
if ($limit > 0) {
|
||||
$offset = ($page - 1) * $limit;
|
||||
$slides = $builder->findAll($limit, $offset);
|
||||
$total = $slideshowModel->where('deleted_at', null)->countAllResults();
|
||||
} else {
|
||||
$slides = $builder->findAll();
|
||||
$total = count($slides);
|
||||
}
|
||||
|
||||
return $this->response->setJSON([
|
||||
'status' => 200,
|
||||
'result' => $slides,
|
||||
'pagination' => [
|
||||
'page' => $page,
|
||||
'limit' => $limit,
|
||||
'total' => $total
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user