File "ProductApiController.php"
Full Path: /home/clickysoft/public_html/jmapi5.clickysoft.net/app/Http/Controllers/Api/V1/User/ProductApiController.php
File size: 4.43 KB
MIME-type: text/x-php
Charset: utf-8
<?php
namespace App\Http\Controllers\Api\V1\User;
use App\Http\Controllers\Controller;
use App\Http\Resources\User\ProductResource;
use App\Http\Resources\User\ProductResourceWithAttributes;
use App\Http\Resources\User\ProductResourceWithAttributesTest;
use App\Http\Resources\User\VariationResource;
use App\Models\Product;
use App\Models\Variation;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class ProductApiController extends Controller
{
public function index(Request $request)
{
$product_min = Product::where('status', 1)->min("price_from");
$product_max = Product::where('status', 1)->max("price_to");
$products = Product::active()
->published()
->when($category = $request->get('category'), function ($query) use ($category) {
$query->where('category_id', $category);
})
->when($search = $request->get('search'), function ($query) use ($search) {
$query->where('name', 'LIKE' , '%'.$search.'%');
})
->when(isset($request->color_engraving), function ($query) use ($request) {
$query->where('engraving', 1);
$query->where('color_engraving', $request->get('color_engraving'));
})
->when($rating = $request->get('rating'), function ($query) use ($rating) {
$query->whereHas('reviews', function ($query) use ($rating) {
$query
->selectRaw('AVG(rating) AS avg_rating')
->groupBy('product_id')
->havingRaw('ROUND(AVG(rating)) = ?', [$rating]);
});
})
->when($color = $request->get('color'), function ($query) use ($color) {
$query->whereHas('variations', function ($query) use ($color) {
$query->where('variation_id', $color);
});
})
->when($dimension = $request->get('dimension'), function ($query) use ($dimension) {
$query->whereHas('variations', function ($query) use ($dimension) {
$query->where('variation_id', $dimension);
});
});
if ((isset($request->min_price) && isset($request->max_price)) &&
($request->min_price >= 0 && $request->max_price >= $request->min_price))
{
$minPrice = $request->min_price;
$maxPrice = $request->max_price;
$products = $products->where(function ($query) use ($minPrice, $maxPrice) {
/*$query->where('price_from', '>=', $minPrice)
->orWhere('price_to', '<=', $maxPrice);*/
$query->whereBetween('price_from', [$minPrice, $maxPrice])
->orWhereBetween('price_to', [$minPrice, $maxPrice]);
});
}
$products = $products->orderBy('name')->paginate(20)->appends(request()->query());
ProductResource::withoutWrapping();
return ProductResource::collection($products)->additional([
'product_min_price' => $product_min,
'product_max_price' => $product_max,
]);
}
public function show($product)
{
$product = Product::where('id', $product)->orWhere('slug', $product)->first();
if (!$product)
return response()->json([
'message' => "Record not found.",
'errors' => ["error" => ["Record not found."]]
], Response::HTTP_NOT_FOUND);
return (new ProductResourceWithAttributes($product));
}
public function show2($product)
{
$product = Product::where('id', $product)->orWhere('slug', $product)->first();
if (!$product)
return response()->json([
'message' => "Record not found.",
'errors' => ["error" => ["Record not found."]]
], Response::HTTP_NOT_FOUND);
return (new ProductResourceWithAttributesTest($product));
}
public function getColorVariations()
{
$colors = Variation::where('type', 'color')->orderBy('value')->get();
VariationResource::withoutWrapping();
return VariationResource::collection($colors);
}
public function getDimensionVariations()
{
$colors = Variation::where('type', 'dimension')->orderBy('value')->get();
VariationResource::withoutWrapping();
return VariationResource::collection($colors);
}
}