ORIENT/plugins/ahmadfatoni/apigenerator/controllers/api/postsController.php

65 lines
2.0 KiB
PHP
Raw Normal View History

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-12 09:29:47 +00:00
$path = Config::get('app.cdn').Config::get('cms.storage.media.path');
2021-05-11 12:39:09 +00:00
$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-12 09:44:19 +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-05-12 09:44:19 +00:00
$post = $this->Post::find($id,['id','content_html']);
2021-04-08 12:38:39 +00:00
2021-05-12 09:40:08 +00:00
if(!is_null($post)) {
$obj = Db::table('vdomah_blogviews_views')
->where('post_id', $post->getKey());
if ($obj->count() > 0) {
2021-05-12 10:05:27 +00:00
$row = $obj->first();
$views = ++$row->views;
$obj->update(['views' => $views]);
$post['views'] = $row->views;
2021-05-12 09:40:08 +00:00
}
}
if ($post){
return $this->helpers->apiArrayResponseBuilder(200, 'success', [$post]);
2021-04-08 12:38:39 +00:00
} 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
}