improve orient api routes
This commit is contained in:
parent
3246e84f96
commit
091b2e9881
|
|
@ -1,88 +1,76 @@
|
|||
<?php namespace Tps\Tps\Controllers\API;
|
||||
|
||||
use Cms\Classes\Controller;
|
||||
use DB;
|
||||
use Config;
|
||||
use Illuminate\Http\Request;
|
||||
use AhmadFatoni\ApiGenerator\Helpers\Helpers;
|
||||
use Tps\Reklama\Models\Reklama;
|
||||
use Tps\Reklama\Models\Statistika;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class AdvertisementsController extends Controller
|
||||
class AdvertisementsController extends BaseApiController
|
||||
{
|
||||
protected $Advertisement;
|
||||
|
||||
protected $helpers;
|
||||
|
||||
public function __construct(Reklama $Advertisement, Helpers $helpers)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->Reklama = $Advertisement;
|
||||
$this->helpers = $helpers;
|
||||
}
|
||||
|
||||
public function index(){
|
||||
|
||||
|
||||
$path = Config::get('app.cdn').Config::get('cms.storage.media.path');
|
||||
|
||||
$dataInc = $this->Reklama
|
||||
//categories list
|
||||
$data = Reklama::select('id','title', 'url', DB::raw("IF(media_mobile<>'',concat('$path',media_mobile),media_mobile) as media_mobile"))
|
||||
->where('active',1)
|
||||
->orderBy('order_app', 'ASC')
|
||||
->paginate(5);
|
||||
|
||||
$this->incrementView($dataInc);
|
||||
|
||||
//categories list
|
||||
$data = $this->Reklama->select('id','title', 'url', DB::raw("IF(media_mobile<>'',concat('$path',media_mobile),media_mobile) as media_mobile"))
|
||||
->where('active',1)
|
||||
->orderBy('order_app', 'ASC')
|
||||
->paginate(5)
|
||||
->toArray();
|
||||
|
||||
return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
|
||||
$this->incrementView($data);
|
||||
|
||||
return $this->apiArrayResponseBuilder(200, 'success', $data->toArray());
|
||||
}
|
||||
|
||||
|
||||
public function getByGroup(Request $request){
|
||||
|
||||
$data = $request->all();
|
||||
|
||||
|
||||
$path = Config::get('app.cdn').Config::get('cms.storage.media.path');
|
||||
|
||||
|
||||
$dataInc = $this->Reklama->select('id','title', 'url', 'enable_stats', DB::raw("IF(media<>'',concat('$path',media),media) as media"))
|
||||
->where('active',1)
|
||||
->where('group_id', $data["group_id"])
|
||||
->paginate(15);
|
||||
|
||||
$this->incrementView($dataInc);
|
||||
|
||||
//categories list
|
||||
$data = $this->Reklama->select('id','title', 'url', 'enable_stats', DB::raw("IF(media<>'',concat('$path',media),media) as media"))
|
||||
->where('active',1)
|
||||
->where('group_id', $data["group_id"])
|
||||
->paginate(15)
|
||||
->toArray();
|
||||
|
||||
|
||||
->where('group_id', $request["group_id"])
|
||||
->paginate(15);
|
||||
|
||||
$this->incrementView($data);
|
||||
|
||||
return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
|
||||
return $this->apiArrayResponseBuilder(200, 'success', $data->toArray());
|
||||
}
|
||||
|
||||
|
||||
public function incrementView($data){
|
||||
|
||||
foreach ($data as $adv){
|
||||
if($adv->enable_stats){
|
||||
$statistika = Statistika::firstOrCreate(['item_id' => $adv->id,'date' => Carbon::today()]);
|
||||
$statistika->increment('view');
|
||||
public function incrementView($data)
|
||||
{
|
||||
$today = Carbon::today();
|
||||
|
||||
// Collect IDs of advertisements with enabled stats
|
||||
$advIds = [];
|
||||
foreach ($data as $adv) {
|
||||
if ($adv->enable_stats) {
|
||||
$advIds[] = $adv->id;
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch existing statistics for today in batch
|
||||
$statistics = Statistika::whereIn('item_id', $advIds)
|
||||
->where('date', $today)
|
||||
->get();
|
||||
|
||||
// Increment view counts
|
||||
foreach ($data as $adv) {
|
||||
if ($adv->enable_stats) {
|
||||
$statistika = $statistics->where('item_id', $adv->id)->first();
|
||||
if (!$statistika) {
|
||||
// Create new if not exists
|
||||
Statistika::create(['item_id' => $adv->id, 'date' => $today, 'view' => 1]);
|
||||
} else {
|
||||
// Increment existing statistic
|
||||
$statistika->increment('view');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,39 +1,25 @@
|
|||
<?php namespace Tps\Tps\Controllers\API;
|
||||
|
||||
use Cms\Classes\Controller;
|
||||
use AhmadFatoni\ApiGenerator\Helpers\Helpers;
|
||||
use Tps\Tps\Models\Afisha;
|
||||
|
||||
class AfishaController extends Controller
|
||||
class AfishaController extends BaseApiController
|
||||
{
|
||||
protected $Afisha;
|
||||
|
||||
protected $helpers;
|
||||
|
||||
public function __construct(Afisha $Afisha, Helpers $helpers)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->Afisha = $Afisha;
|
||||
$this->helpers = $helpers;
|
||||
}
|
||||
|
||||
public function index(){
|
||||
|
||||
$data = $this->Afisha::with(['translations:model_id,locale,attribute_data'])->paginate(10);
|
||||
$data = Afisha::with(['translations:model_id,locale,attribute_data'])->paginate(10);
|
||||
|
||||
return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
|
||||
return $this->apiArrayResponseBuilder(200, 'success', $data);
|
||||
}
|
||||
|
||||
|
||||
public function show($id){
|
||||
|
||||
$data = $this->Afisha::find($id);
|
||||
$data = Afisha::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']);
|
||||
}
|
||||
return $this->apiArrayResponseBuilder(200, 'success', [$data]);
|
||||
}
|
||||
|
||||
$this->apiArrayResponseBuilder(404, 'not found', ['error' => 'Resource id=' . $id . ' could not be found']);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
<?php namespace Tps\Tps\Controllers\API;
|
||||
|
||||
use Cms\Classes\Controller;
|
||||
|
||||
class BaseApiController extends Controller{
|
||||
private function apiArrayResponseBuilder($statusCode = null, $message = null, $data = [])
|
||||
{
|
||||
$arr = [
|
||||
'status_code' => (isset($statusCode)) ? $statusCode : 500,
|
||||
'message' => (isset($message)) ? $message : 'error'
|
||||
];
|
||||
if (count($data) > 0) {
|
||||
$arr['data'] = $data;
|
||||
}
|
||||
|
||||
return response()->json($arr, $arr['status_code']);
|
||||
//return $arr;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,43 +1,29 @@
|
|||
<?php namespace Tps\Tps\Controllers\API;
|
||||
|
||||
use Cms\Classes\Controller;
|
||||
|
||||
use AhmadFatoni\ApiGenerator\Helpers\Helpers;
|
||||
use RainLab\Blog\Models\Category;
|
||||
class CategoriesController extends Controller
|
||||
class CategoriesController extends BaseApiController
|
||||
{
|
||||
protected $Category;
|
||||
|
||||
protected $helpers;
|
||||
|
||||
public function __construct(Category $Category, Helpers $helpers)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->Category = $Category;
|
||||
$this->helpers = $helpers;
|
||||
}
|
||||
|
||||
public function index(){
|
||||
|
||||
//categories list
|
||||
$data = $this->Category->select('id','name')
|
||||
$data = Category::select('id','name')
|
||||
->with(['translations:model_id,locale,attribute_data'])
|
||||
->where('status',1)
|
||||
->get()
|
||||
->toArray();
|
||||
|
||||
return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
|
||||
return $this->apiArrayResponseBuilder(200, 'success', $data);
|
||||
}
|
||||
|
||||
//categories item
|
||||
public function show($id){
|
||||
|
||||
$data = $this->Category::find($id);
|
||||
$data = Category::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']);
|
||||
$this->apiArrayResponseBuilder(404, 'not found', ['error' => 'Resource id=' . $id . ' could not be found']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,28 +1,11 @@
|
|||
<?php namespace Tps\Tps\Controllers\API;
|
||||
|
||||
use Cms\Classes\Controller;
|
||||
use AhmadFatoni\ApiGenerator\Helpers\Helpers;
|
||||
use RainLab\Blog\Models\CategoryGroup;
|
||||
use RainLab\Blog\Models\Post;
|
||||
class CategoriesV2Controller extends Controller
|
||||
class CategoriesV2Controller extends BaseApiController
|
||||
{
|
||||
protected $Category;
|
||||
|
||||
protected $Post;
|
||||
|
||||
protected $helpers;
|
||||
|
||||
public function __construct(Post $Post, CategoryGroup $CategoryGroup, Helpers $helpers)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->CategoryGroup = $CategoryGroup;
|
||||
$this->Post = $Post;
|
||||
$this->helpers = $helpers;
|
||||
}
|
||||
|
||||
public function index(){
|
||||
|
||||
$data = $this->CategoryGroup::select('id','name','type', 'slug', 'note')
|
||||
$data = CategoryGroup::select('id','name','type', 'slug', 'note')
|
||||
->with(['translations:model_id,locale,attribute_data'])
|
||||
->with(['category_items' => function($query) {
|
||||
$query
|
||||
|
|
@ -33,7 +16,7 @@ class CategoriesV2Controller extends Controller
|
|||
}])
|
||||
->get();
|
||||
|
||||
return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
|
||||
return $this->apiArrayResponseBuilder(200, 'success', $data);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,28 +1,15 @@
|
|||
<?php namespace Tps\Tps\Controllers\API;
|
||||
|
||||
use Cms\Classes\Controller;
|
||||
use Config;
|
||||
use AhmadFatoni\ApiGenerator\Helpers\Helpers;
|
||||
|
||||
use Tps\Tps\Models\Media;
|
||||
class MediaController extends Controller
|
||||
|
||||
class MediaController extends BaseApiController
|
||||
{
|
||||
protected $Media;
|
||||
|
||||
protected $helpers;
|
||||
|
||||
public function __construct(Media $Media, Helpers $helpers)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->Media = $Media;
|
||||
$this->helpers = $helpers;
|
||||
}
|
||||
|
||||
public function index(){
|
||||
$path = Config::get('app.cdn').Config::get('cms.storage.media.path');
|
||||
|
||||
$type = input('type');
|
||||
$data = $this->Media::where('type', $type)
|
||||
$data = Media::where('type', $type)
|
||||
->where('status', 1)
|
||||
->with(['translations:model_id,locale,attribute_data'])
|
||||
->orderBy('created_at', 'DESC')
|
||||
|
|
@ -41,7 +28,7 @@ class MediaController extends Controller
|
|||
//dd($data->getItems());
|
||||
|
||||
|
||||
return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
|
||||
return $this->apiArrayResponseBuilder(200, 'success', $data);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,23 +1,10 @@
|
|||
<?php namespace Tps\Tps\Controllers\API;
|
||||
|
||||
use Cms\Classes\Controller;
|
||||
use DB;
|
||||
use Config;
|
||||
use AhmadFatoni\ApiGenerator\Helpers\Helpers;
|
||||
use RainLab\Blog\Models\Post;
|
||||
class PostController extends Controller
|
||||
class PostController extends BaseApiController
|
||||
{
|
||||
protected $Post;
|
||||
|
||||
protected $helpers;
|
||||
|
||||
public function __construct(Post $Post, Helpers $helpers)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->Post = $Post;
|
||||
$this->helpers = $helpers;
|
||||
}
|
||||
|
||||
//postes list
|
||||
public function index(){
|
||||
$path = Config::get('app.cdn').Config::get('cms.storage.media.path');
|
||||
|
|
@ -38,7 +25,7 @@ class PostController extends Controller
|
|||
$filter['featured'] = true;
|
||||
}
|
||||
|
||||
$data = $this->Post::with(['categories:id,name'])->listFrontEnd($filter);
|
||||
$data = Post::with(['categories:id,name'])->listFrontEnd($filter);
|
||||
|
||||
if($data){
|
||||
$data->each(function ($item, $key) {
|
||||
|
|
@ -46,15 +33,17 @@ class PostController extends Controller
|
|||
});
|
||||
}
|
||||
|
||||
return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
|
||||
return $this->apiArrayResponseBuilder(200, 'success', $data);
|
||||
}
|
||||
|
||||
//posts item
|
||||
public function show($locale,$id){
|
||||
$path = Config::get('app.cdn').Config::get('cms.storage.media.path');
|
||||
//$post = $this->Post::find($id,['id','content_html','author','slug', 'afisha_phone', 'afisha_address', 'afisha_phone_new']);
|
||||
$post = $this->Post::where('id', $id)->select('id', 'content_html', 'author', 'title','slug','published_at', 'more_photo','afisha_phone', 'afisha_address', 'afisha_phone_new', DB::raw("IF(featured_image<>'',concat('$path',featured_image),featured_image) as main_image"))->with(['categories:id,name'])->first();
|
||||
|
||||
$post = Post::where('id', $id)
|
||||
->select('id', 'content_html', 'author', 'title','slug','published_at', 'more_photo','afisha_phone', 'afisha_address', 'afisha_phone_new', DB::raw("IF(featured_image<>'',concat('$path',featured_image),featured_image) as main_image"))
|
||||
->with(['categories:id,name'])
|
||||
->first();
|
||||
|
||||
if(!is_null($post)) {
|
||||
|
||||
|
|
@ -78,10 +67,10 @@ class PostController extends Controller
|
|||
'views' => rand(1,10)
|
||||
]);
|
||||
}
|
||||
return $this->helpers->apiArrayResponseBuilder(200, 'success', [$post]);
|
||||
return $this->apiArrayResponseBuilder(200, 'success', [$post]);
|
||||
}
|
||||
else {
|
||||
return $this->helpers->apiArrayResponseBuilder(404, 'not found', ['error' => 'Resource id=' . $id . ' could not be found']);
|
||||
return $this->apiArrayResponseBuilder(404, 'not found', ['error' => 'Resource id=' . $id . ' could not be found']);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue