You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
4.5 KiB
132 lines
4.5 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\CentralLogics\CategoryLogic;
|
|
use App\CentralLogics\Helpers;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Category;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class CategoryController extends Controller
|
|
{
|
|
public function get_categories(Request $request)
|
|
{
|
|
try {
|
|
$name= $request->query('name');
|
|
$categories = Category::withCount('products')->with(['childes' => function($query) {
|
|
$query->withCount('products');
|
|
}])
|
|
->where(['position'=>0,'status'=>1])
|
|
|
|
->when($name, function($q)use($name){
|
|
$key = explode(' ', $name);
|
|
$q->where(function($q)use($key){
|
|
foreach ($key as $value){
|
|
$q->orWhere('name', 'like', '%'.$value.'%')->orWhere('slug', 'like', '%'.$value.'%');
|
|
}
|
|
return $q;
|
|
});
|
|
})
|
|
->orderBy('priority','desc')->get();
|
|
return response()->json(Helpers::category_data_formatting($categories, true), 200);
|
|
} catch (\Exception $e) {
|
|
return response()->json([$e->getMessage()], 200);
|
|
}
|
|
}
|
|
|
|
public function get_childes($id)
|
|
{
|
|
try {
|
|
$categories = Category::when(is_numeric($id),function ($qurey) use($id){
|
|
$qurey->where(['parent_id' => $id,'status'=>1]);
|
|
})
|
|
->when(!is_numeric($id),function ($qurey) use($id){
|
|
$qurey->where(['slug' => $id,'status'=>1]);
|
|
})
|
|
->orderBy('priority','desc')->get();
|
|
return response()->json(Helpers::category_data_formatting($categories, true), 200);
|
|
} catch (\Exception $e) {
|
|
return response()->json([], 200);
|
|
}
|
|
}
|
|
|
|
public function get_products($id, Request $request)
|
|
{
|
|
if (!$request->hasHeader('zoneId')) {
|
|
$errors = [];
|
|
array_push($errors, ['code' => 'zoneId', 'message' => translate('messages.zone_id_required')]);
|
|
return response()->json([
|
|
'errors' => $errors
|
|
], 403);
|
|
}
|
|
$validator = Validator::make($request->all(), [
|
|
'limit' => 'required',
|
|
'offset' => 'required',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json(['errors' => Helpers::error_processor($validator)], 403);
|
|
}
|
|
|
|
$zone_id= json_decode($request->header('zoneId'), true);
|
|
|
|
$type = $request->query('type', 'all');
|
|
|
|
$data = CategoryLogic::products($id, $zone_id, $request['limit'], $request['offset'], $type);
|
|
$data['products'] = Helpers::product_data_formatting($data['products'] , true, false, app()->getLocale());
|
|
|
|
if(auth('api')->user() !== null){
|
|
$customer_id =auth('api')->user()->id;
|
|
Helpers::visitor_log('category',$customer_id,$id,false);
|
|
}
|
|
|
|
return response()->json($data, 200);
|
|
}
|
|
|
|
|
|
public function get_restaurants($id, Request $request)
|
|
{
|
|
if (!$request->hasHeader('zoneId')) {
|
|
$errors = [];
|
|
array_push($errors, ['code' => 'zoneId', 'message' => translate('messages.zone_id_required')]);
|
|
return response()->json([
|
|
'errors' => $errors
|
|
], 403);
|
|
}
|
|
$validator = Validator::make($request->all(), [
|
|
'limit' => 'required',
|
|
'offset' => 'required',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json(['errors' => Helpers::error_processor($validator)], 403);
|
|
}
|
|
|
|
$zone_id= json_decode($request->header('zoneId'), true);
|
|
|
|
$type = $request->query('type', 'all');
|
|
|
|
$data = CategoryLogic::restaurants($id, $zone_id, $request['limit'], $request['offset'], $type);
|
|
$data['restaurants'] = Helpers::restaurant_data_formatting($data['restaurants'] , true);
|
|
|
|
// if(auth('api')->user() !== null){
|
|
// $customer_id =auth('api')->user()->id;
|
|
// Helpers::visitor_log('category',$customer_id,$id,false);
|
|
// }
|
|
|
|
return response()->json($data, 200);
|
|
}
|
|
|
|
|
|
|
|
public function get_all_products($id,Request $request)
|
|
{
|
|
try {
|
|
return response()->json(Helpers::product_data_formatting(CategoryLogic::all_products($id), true, false, app()->getLocale()), 200);
|
|
} catch (\Exception $e) {
|
|
return response()->json([], 200);
|
|
}
|
|
}
|
|
}
|
|
|