From 65177db6e3164d554dfad4c292b597505d914312 Mon Sep 17 00:00:00 2001 From: merdan Date: Tue, 11 May 2021 17:39:09 +0500 Subject: [PATCH] api --- .../apigenerator/controllers/api/postsController.php | 12 +++++++----- plugins/rainlab/blog/models/Post.php | 11 ++++++++--- plugins/rainlab/blog/models/post/fields.yaml | 4 ++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/plugins/ahmadfatoni/apigenerator/controllers/api/postsController.php b/plugins/ahmadfatoni/apigenerator/controllers/api/postsController.php index 24f255e83..08ba2fa1f 100644 --- a/plugins/ahmadfatoni/apigenerator/controllers/api/postsController.php +++ b/plugins/ahmadfatoni/apigenerator/controllers/api/postsController.php @@ -2,7 +2,8 @@ use Cms\Classes\Controller; use BackendMenu; - +use DB; +use Config; use Illuminate\Http\Request; use AhmadFatoni\ApiGenerator\Helpers\Helpers; use Illuminate\Support\Facades\Validator; @@ -21,15 +22,16 @@ class postsController extends Controller } public function index(){ - - $data = $this->Post::with(['categories'])->listFrontEnd([ + $path = Config::get('app.cdn'); + $data = $this->Post::with(['categories:id,name'])->listFrontEnd([ 'page' => input('page'), - 'sort' => input('sort')??'published_at', + 'sort' => input('sort')??'published_at desc', 'perPage' => input('count'), 'search' => trim(input('search')), 'category' => input('category'), 'date' => input('date'), - 'published' => true + 'published' => true, + 'select' => ['id','title','published_at',DB::raw("IF(featured_image<>'',concat('$path',image),image) as main_image")] ]); return $this->helpers->apiArrayResponseBuilder(200, 'success', $data); } diff --git a/plugins/rainlab/blog/models/Post.php b/plugins/rainlab/blog/models/Post.php index 09b2d8c25..94002e369 100644 --- a/plugins/rainlab/blog/models/Post.php +++ b/plugins/rainlab/blog/models/Post.php @@ -264,14 +264,15 @@ class Post extends Model extract(array_merge([ 'page' => 1, 'perPage' => 30, - 'sort' => 'created_at', + 'sort' => 'published_at', 'categories' => null, 'exceptCategories' => null, 'category' => null, 'search' => '', - 'date' => '', + 'date' => '', 'published' => true, - 'exceptPost' => null + 'exceptPost' => null, + 'select' => null ], $options)); $searchableFields = ['title', 'slug', 'excerpt']; @@ -375,6 +376,10 @@ class Post extends Model }); } + if($select){ + $query->select($select); + } + return $query->paginate($perPage, $page); } diff --git a/plugins/rainlab/blog/models/post/fields.yaml b/plugins/rainlab/blog/models/post/fields.yaml index 6d507d1fe..08f9d11f5 100644 --- a/plugins/rainlab/blog/models/post/fields.yaml +++ b/plugins/rainlab/blog/models/post/fields.yaml @@ -26,8 +26,8 @@ secondaryTabs: fields: content: tab: rainlab.blog::lang.post.tab_edit -# type: RainLab\Blog\FormWidgets\BlogMarkdown - type: richeditor + type: RainLab\Blog\FormWidgets\BlogMarkdown +# type: richeditor cssClass: field-slim blog-post-preview stretch: true mode: split