GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
File: /var/www/api.ayokah.co.uk/app/Http/Controllers/Frontend/ProductController.php
<?php

namespace App\Http\Controllers\Frontend;

use App\Models\City;
use App\Models\Order;
use App\Models\Product;
use App\Models\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Validation\ValidationException;

class ProductController extends Controller
{


    public function allProduct(Request $request)
    {
        $query = $request->input('query');

        try {
            $products = Product::where('name', 'like', '%' . $query . '%')
                ->get(['uuid', 'name']);

            if ($products->isNotEmpty()) {
                return response()->json(['status' => 'success', 'message' => 'Products found', 'data' => $products], 200);
            } else {
                return response()->json(['status' => 'error', 'message' => 'No products found', 'data' => []], 404);
            }
        } catch (\Throwable $e) {
            Log::error($e->getMessage());
            return response()->json(['status' => 'error', 'message' => 'Unable to retrieve product list'], 500);
        }
    }

    public function index()
    {
        try {
            $products = Cache::remember('products_index', now()->addHours(1), function () {
                return Product::latest()->paginate(20);
            });

            if ($products->isNotEmpty()) {
                return response()->json(['message' => 'Products found', 'products' => $products], 200);
            } else {
                return response()->json(['error' => 'No products found'], 404);
            }
        } catch (\Throwable $e) {
            Log::error($e->getMessage());
            return response()->json(['error' => 'Unable to retrieve product list'], 500);
        }
    }
    public function shopProduct()
    {
        try {
            $shop_products = Cache::remember('shop_product', now()->addHours(1), function () {
                return Product::latest()->inRandomOrder()->paginate(40);
            });

            if ($shop_products->isNotEmpty()) {
                return response()->json(['message' => 'Products found', 'products' => $shop_products], 200);
            } else {
                return response()->json(['error' => 'No products found'], 404);
            }
        } catch (\Throwable $e) {
            Log::error($e->getMessage());
            return response()->json(['error' => 'Unable to retrieve product list'], 500);
        }
    }

    public function list()
    {
        try {
            $products = Cache::remember('products_list', now()->addHours(1), function () {
                return Product::latest()->limit(12)->get();
            });

            if ($products->isNotEmpty()) {
                return response()->json(['message' => 'Products found', 'products' => $products], 200);
            } else {
                return response()->json(['error' => 'No products found'], 404);
            }
        } catch (\Throwable $e) {
            Log::error($e->getMessage());
            return response()->json(['error' => 'Unable to retrieve product list'], 500);
        }
    }

    public function daily()
    {
        try {
            $dailys = Cache::remember('daily_products', now()->addHours(1), function () {
                return Product::whereIn('category_id', function ($query) {
                    $query->select('category_id')
                        ->from('products')
                        ->groupBy('category_id')
                        ->havingRaw('COUNT(*) > 1');
                })->inRandomOrder()->get();
            });

            if ($dailys->isNotEmpty()) {
                return response()->json(['message' => 'Daily Products found', 'dailys' => $dailys], 200);
            } else {
                return response()->json(['error' => 'No products found'], 404);
            }
        } catch (\Throwable $e) {
            Log::error($e->getMessage());
            return response()->json(['error' => 'Unable to retrieve product list'], 500);
        }
    }

    public function deals()
    {
        try {
            $deals = Cache::remember('deal_products', now()->addHours(1), function () {
                return Product::with('coupons')->whereNotNull('duration')->inRandomOrder()->limit(4)->get();
            });

            if ($deals->isNotEmpty()) {
                return response()->json(['message' => 'Daily Products found', 'deals' => $deals], 200);
            } else {
                return response()->json(['message' => 'No products found', 'deals' => []], 404);
            }
        } catch (\Throwable $e) {
            Log::error($e->getMessage());
            return response()->json(['message' => 'No deal products found', 'deals' => []], 404);
        }
    }

    public function topSelling()
    {
        try {
            $top_sellings = Cache::remember('top_selling_products', now()->addHours(1), function () {
                $orderProductIds = Order::pluck('product_id')->toArray();
                return Product::whereIn('id', $orderProductIds)
                    ->inRandomOrder()
                    ->limit(4)
                    ->get();
            });

            if ($top_sellings->isNotEmpty()) {
                return response()->json(['message' => 'Top Selling Products found', 'top_sellings' => $top_sellings], 200);
            } else {
                return response()->json(['error' => 'No products found'], 404);
            }
        } catch (\Throwable $e) {
            Log::error($e->getMessage());
            return response()->json(['error' => 'No top selling products found'], 404);
        }
    }

    public function trending()
    {
        try {
            $trendings = Cache::remember('trending_products', now()->addHours(1), function () {
                return Product::oldest()->inRandomOrder()
                    ->limit(4)
                    ->get();
            });

            if ($trendings->isNotEmpty()) {
                return response()->json(['message' => 'Trending Products found', 'trendings' => $trendings], 200);
            } else {
                return response()->json(['error' => 'No products found'], 404);
            }
        } catch (\Throwable $e) {
            Log::error($e->getMessage());
            return response()->json(['error' => 'No trending products found'], 404);
        }
    }

    public function recent()
    {
        try {
            $recents = Cache::remember('recent_products', now()->addHours(1), function () {
                return Product::latest()->inRandomOrder()
                    ->limit(4)
                    ->get();
            });

            if ($recents->isNotEmpty()) {
                return response()->json(['message' => 'Recent Products found', 'recents' => $recents], 200);
            } else {
                return response()->json(['error' => 'No products found'], 404);
            }
        } catch (\Throwable $e) {
            Log::error($e->getMessage());
            return response()->json(['error' => 'No recent products found'], 404);
        }
    }

    public function rated()
    {
        try {
            $rateds = Cache::remember('rated_products', now()->addHours(1), function () {
                return Product::inRandomOrder()
                    ->limit(4)
                    ->get();
            });

            if ($rateds->isNotEmpty()) {
                return response()->json(['message' => 'Rated Products found', 'rateds' => $rateds], 200);
            } else {
                return response()->json(['error' => 'No products found'], 404);
            }
        } catch (\Throwable $e) {
            Log::error($e->getMessage());
            return response()->json(['error' => 'No recent products found'], 404);
        }
    }

    public function searchProduct(Request $request)
    {
        try {
            $keyword = $request->get('search');
            $products = Product::where('name', 'like', "%$keyword%")->get();
            return response()->json(['products' => $products]);
        } catch (\Throwable $th) {
            return response()->json(['message' => $th], 404);
        }
    }


    public function showProduct(Request $request)
    {
        try {
            $validatedData = $request->validate([
                'slug' => ['required', 'exists:products,slug'],
            ]);
        } catch (ValidationException $e) {
            return response()->json(['message' => $e->getMessage()], 422);
        }

        $product = Product::with(['seller', 'category'])->where('slug', $validatedData['slug'])->first();

        if ($product) {
            $cityId = $product->seller->city_id;
            $city = City::find($cityId);
            $cityName = $city ? $city->name : 'Unknown City';
            return response()->json(['product' => $product, 'city' => $cityName], 200);
        } else {
            return response()->json(['message' => 'No product found'], 404);
        }
    }

    public function relatedProduct(Request $request)
    {
        try {
            $validatedData = $request->validate([
                'category_id' => ['required', 'exists:categories,id'],
                'product_id' => ['required', 'exists:products,id'],
            ]);
        } catch (ValidationException $e) {
            return response()->json(['error' => $e->errors()], 422);
        }
        $related_products = Product::where('category_id', $validatedData['category_id'])
            ->where('id', '!=', $validatedData['product_id'])
            ->inRandomOrder()
            ->limit(8)
            ->get();
        if ($related_products->isNotEmpty()) {
            return response()->json(['related' => $related_products], 200);
        } else {
            return response()->json(['message' => 'No product is found in this category'], 404);
        }
    }
}