2021-04-08 12:38:39 +00:00
|
|
|
<?php namespace AhmadFatoni\ApiGenerator\Controllers\API;
|
|
|
|
|
|
|
|
|
|
use Cms\Classes\Controller;
|
|
|
|
|
use BackendMenu;
|
2021-05-11 12:39:09 +00:00
|
|
|
use DB;
|
|
|
|
|
use Config;
|
2021-04-08 12:38:39 +00:00
|
|
|
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(){
|
2021-05-11 12:39:09 +00:00
|
|
|
$path = Config::get('app.cdn');
|
|
|
|
|
$data = $this->Post::with(['categories:id,name'])->listFrontEnd([
|
2021-04-13 09:07:38 +00:00
|
|
|
'page' => input('page'),
|
2021-05-11 12:39:09 +00:00
|
|
|
'sort' => input('sort')??'published_at desc',
|
2021-04-13 09:07:38 +00:00
|
|
|
'perPage' => input('count'),
|
|
|
|
|
'search' => trim(input('search')),
|
|
|
|
|
'category' => input('category'),
|
|
|
|
|
'date' => input('date'),
|
2021-05-11 12:39:09 +00:00
|
|
|
'published' => true,
|
2021-05-11 12:58:20 +00:00
|
|
|
'select' => ['id','title','published_at',DB::raw("IF(featured_image<>'',concat('$path',featured_image),featured_image) as main_image")]
|
2021-04-13 09:07:38 +00:00
|
|
|
]);
|
2021-04-08 12:38:39 +00:00
|
|
|
return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 09:47:33 +00:00
|
|
|
public function show($locale,$id){
|
2021-04-08 12:38:39 +00:00
|
|
|
|
2021-04-13 09:07:38 +00:00
|
|
|
$data = $this->Post::with('categories')->find($id);
|
2021-04-08 12:38:39 +00:00
|
|
|
|
|
|
|
|
if ($data){
|
|
|
|
|
return $this->helpers->apiArrayResponseBuilder(200, 'success', [$data]);
|
|
|
|
|
} else {
|
2021-05-11 09:42:47 +00:00
|
|
|
return $this->helpers->apiArrayResponseBuilder(404, 'not found', ['error' => 'Resource id=' . $id . ' could not be found']);
|
2021-04-08 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 09:07:38 +00:00
|
|
|
}
|