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

86 lines
2.8 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;
}
2021-12-08 16:24:11 +00:00
//postes list
2021-04-08 12:38:39 +00:00
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'),
2022-12-14 15:55:13 +00:00
'typePost' => input('typePost'),
2022-12-19 09:40:00 +00:00
'postGroup' => input('group'),
2021-05-11 12:39:09 +00:00
'published' => true,
2022-12-29 11:38:21 +00:00
'featured' => input('featured') == true ? 1 : 0,
2022-12-30 20:38:23 +00:00
'select' => ['id','title','slug','published_at', 'more_photo', DB::raw("IF(featured_image<>'',concat('$path',featured_image),featured_image) as main_image")]
2021-04-13 09:07:38 +00:00
]);
2021-06-18 08:36:24 +00:00
if($data){
$data->each(function ($item, $key) {
$item->url = $this->pageUrl('post',['id'=>$item->id,'slug'=>$item->slug]);
});
}
2021-04-08 12:38:39 +00:00
return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
}
2021-12-08 16:24:11 +00:00
//posts item
2021-05-11 09:47:33 +00:00
public function show($locale,$id){
2021-04-08 12:38:39 +00:00
2021-06-17 12:32:43 +00:00
$post = $this->Post::find($id,['id','content_html','author','slug']);
2021-05-12 09:40:08 +00:00
if(!is_null($post)) {
2021-10-11 06:37:52 +00:00
$post->url = $this->pageUrl('post',['id'=>$post->id,'slug'=>$post->slug]);
2021-05-12 09:40:08 +00:00
$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();
2021-05-19 06:41:34 +00:00
$views = $row->views + rand(1,10);;
2021-05-12 10:05:27 +00:00
$obj->update(['views' => $views]);
$post['views'] = $row->views;
2021-05-12 09:40:08 +00:00
}
2021-05-19 06:41:34 +00:00
else {
Db::table('vdomah_blogviews_views')->insert([
'post_id' => $post->getKey(),
'views' => rand(1,10)
]);
}
2021-05-12 09:40:08 +00:00
return $this->helpers->apiArrayResponseBuilder(200, 'success', [$post]);
2021-10-11 06:37:52 +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
}