mirror of https://github.com/4xmen/xshop.git
feat: Implement product listing API with caching , sorting , filters and search
- Add ProductController with index method to provide product listings - Implement caching for product listings based on request URI - Add sorting functionality for products by various criteria (new, old, most_view, less_view, most_buy, less_buy) - Implement filtering by category using slug - Add search functionality to filter products by name - Implement price range filtering using min_price and max_price parameters - Include related category data in the product resource response - Set default pagination to 20 items per page with optional customization via per_page parameterpull/45/head
parent
a8099343ad
commit
dcead2a865
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Web;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\ProductResource;
|
||||
use App\Models\Category;
|
||||
use App\Models\Product;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class ProductController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
|
||||
$cacheKey = 'products_' . md5($request->getUri());
|
||||
|
||||
$data = Cache::remember($cacheKey, now()->addMinutes(env('CACHE_LIFE_TIME', 10)), function () use ($request) {
|
||||
$product = Product::query();
|
||||
/**
|
||||
* Product Sort by keys
|
||||
*/
|
||||
if (isset($request['sort']) && !is_null($request['sort'])) {
|
||||
if ($request['sort'] === 'new')
|
||||
$product = $product->orderByDesc('created_at');
|
||||
if ($request['sort'] === 'old')
|
||||
$product = $product->orderBy('created_at');
|
||||
if ($request['sort'] === 'most_view')
|
||||
$product = $product->orderByDesc('view');
|
||||
if ($request['sort'] === 'less_view')
|
||||
$product = $product->orderBy('view');
|
||||
if ($request['sort'] === 'most_buy')
|
||||
$product = $product->orderByDesc('sell');
|
||||
if ($request['sort'] === 'less_buy')
|
||||
$product = $product->orderBy('sell');
|
||||
}
|
||||
if (isset($request['category']) && !is_null($request['category']))
|
||||
$product = $product->where('category_id', Category::firstWhere('slug', $request['category'])->id);
|
||||
|
||||
if (isset($request['search']) && !is_null($request['search']))
|
||||
$product = $product->where('name', 'LIKE', "%$request->search%");
|
||||
|
||||
if (isset($request['min_price']) &&
|
||||
isset($request['max_price']) &&
|
||||
!is_null($request['min_price']) &&
|
||||
!is_null($request['max_price'])
|
||||
) {
|
||||
$product = $product->whereBetween('buy_price', [
|
||||
intval($request->min_price),
|
||||
intval($request->max_price)
|
||||
]);
|
||||
}
|
||||
$request->merge([
|
||||
'loadCategory' => true
|
||||
]);
|
||||
return [
|
||||
'products' => ProductResource::collection($product->paginate($request->input('per_page', 20)))->resource->toArray(),
|
||||
];
|
||||
});
|
||||
return success($data);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue