50 lines
1.4 KiB
PHP
50 lines
1.4 KiB
PHP
<?php namespace AhmadFatoni\ApiGenerator\Controllers\API;
|
|
|
|
use Cms\Classes\Controller;
|
|
use BackendMenu;
|
|
|
|
use Illuminate\Http\Request;
|
|
use AhmadFatoni\ApiGenerator\Helpers\Helpers;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use RainLab\Blog\Models\Post;
|
|
class postsController extends Controller
|
|
{
|
|
protected $Post;
|
|
|
|
protected $helpers;
|
|
|
|
public function __construct(Post $Post, Helpers $helpers)
|
|
{
|
|
parent::__construct();
|
|
$this->Post = $Post;
|
|
$this->helpers = $helpers;
|
|
}
|
|
|
|
public function index(){
|
|
|
|
$data = $this->Post::with(['categories'])->listFrontEnd([
|
|
'page' => input('page'),
|
|
'sort' => input('sort')??'published_at',
|
|
'perPage' => input('count'),
|
|
'search' => trim(input('search')),
|
|
'category' => input('category'),
|
|
'date' => input('date'),
|
|
'published' => true
|
|
]);
|
|
return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
|
|
}
|
|
|
|
public function show($id){
|
|
|
|
$data = $this->Post::with('categories')->find($id);
|
|
|
|
if ($data){
|
|
return $this->helpers->apiArrayResponseBuilder(200, 'success', [$data]);
|
|
} else {
|
|
$this->helpers->apiArrayResponseBuilder(404, 'not found', ['error' => 'Resource id=' . $id . ' could not be found']);
|
|
}
|
|
|
|
}
|
|
|
|
}
|