diff --git a/plugins/tps/tps/controllers/api/AdvertisementsController.php b/plugins/tps/tps/controllers/api/AdvertisementsController.php index ed787a1cc..c9e91e0bb 100644 --- a/plugins/tps/tps/controllers/api/AdvertisementsController.php +++ b/plugins/tps/tps/controllers/api/AdvertisementsController.php @@ -1,88 +1,76 @@ 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'); + } + } + } } - } diff --git a/plugins/tps/tps/controllers/api/AfishaController.php b/plugins/tps/tps/controllers/api/AfishaController.php index 1332d98fc..908c4372c 100644 --- a/plugins/tps/tps/controllers/api/AfishaController.php +++ b/plugins/tps/tps/controllers/api/AfishaController.php @@ -1,39 +1,25 @@ 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']); } } diff --git a/plugins/tps/tps/controllers/api/BaseApiController.php b/plugins/tps/tps/controllers/api/BaseApiController.php new file mode 100644 index 000000000..edca264c4 --- /dev/null +++ b/plugins/tps/tps/controllers/api/BaseApiController.php @@ -0,0 +1,20 @@ + (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; + + } +} \ No newline at end of file diff --git a/plugins/tps/tps/controllers/api/CategoriesController.php b/plugins/tps/tps/controllers/api/CategoriesController.php index c26a4a5f7..de42dc2b8 100644 --- a/plugins/tps/tps/controllers/api/CategoriesController.php +++ b/plugins/tps/tps/controllers/api/CategoriesController.php @@ -1,43 +1,29 @@ 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']); } } } diff --git a/plugins/tps/tps/controllers/api/CategoriesV2Controller.php b/plugins/tps/tps/controllers/api/CategoriesV2Controller.php index 7eea23f12..5b5b4dc4f 100644 --- a/plugins/tps/tps/controllers/api/CategoriesV2Controller.php +++ b/plugins/tps/tps/controllers/api/CategoriesV2Controller.php @@ -1,28 +1,11 @@ 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); } diff --git a/plugins/tps/tps/controllers/api/MediaController.php b/plugins/tps/tps/controllers/api/MediaController.php index c8ff2b42c..cf24abb84 100644 --- a/plugins/tps/tps/controllers/api/MediaController.php +++ b/plugins/tps/tps/controllers/api/MediaController.php @@ -1,28 +1,15 @@ 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); } diff --git a/plugins/tps/tps/controllers/api/PostsController.php b/plugins/tps/tps/controllers/api/PostsController.php index 68ed7c4e9..4c9b457d2 100644 --- a/plugins/tps/tps/controllers/api/PostsController.php +++ b/plugins/tps/tps/controllers/api/PostsController.php @@ -1,23 +1,10 @@ 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']); } }