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/Admin/SellerController.php
<?php

namespace App\Http\Controllers\Admin;

use App\Models\Seller;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;
use Illuminate\Support\Carbon;


class SellerController extends Controller
{
    public function index()
    {
        try {
            $sellers = Seller::latest()->paginate(10);
            if ($sellers->isNotEmpty()) {
                return response()->json(['sellers' => $sellers], 200);
            } else {
                return response()->json(['message' => 'No seller found'], 404);
            }
        } catch (\Throwable $th) {
            Log::error($th->getMessage());
            return response()->json(['error' => 'Oops. Something went wrong. Pls, try again later'], 500);
        }
    }

    public function list()
    {
        try {
            $sellers = Seller::select('name', 'slug', 'id', 'logo')->latest()->paginate(10);
            if ($sellers->isNotEmpty()) {
                return response()->json(['sellers' => $sellers], 200);
            } else {
                return response()->json(['message' => 'Seller store not found'], 404);
            }
        } catch (\Throwable $th) {
            Log::error($th->getMessage());
            return response()->json(['error' => 'Unable to list seller store'], 500);
        }
    }


    public function paid()
    {
        try {
            $sellers = Seller::select('name', 'slug', 'id', 'logo')
            ->whereHas('payment', function ($query) {
                $query->where('due_date', '>', Carbon::now());
            })
                ->latest()
                ->paginate(10);

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

    // public function update(Request $request)
    // {
    //     try {
    //         try {
    //             $validatedData = $request->validate([
    //                 'brand_name' => ['required', 'unique:news_flashes,title,' . $request->id],
    //                 'category_id' => ['required', 'unique:news_flashes,title,' . $request->id],
    //                 'address' => ['required', 'unique:news_flashes,title,' . $request->id],
    //                 'description' => ['required', 'unique:news_flashes,title,' . $request->id],
    //                 'reg_no' => ['required', 'unique:news_flashes,title,' . $request->id],
    //                 'phone' => ['required', 'unique:news_flashes,title,' . $request->id],
    //                 'facebook' => ['required', 'unique:news_flashes,title,' . $request->id],
    //                 'instagram' => ['required', 'unique:news_flashes,title,' . $request->id],
    //             ]);
    //         } catch (\Illuminate\Validation\ValidationException $e) {
    //             return response()->json(['error' => $e->errors()], 422);
    //         }
    //         $seller = Seller::findOrFail($request->seller_id);

    //         if ($seller) {
    //             $seller->brand_name = $validatedData('brand_name');
    //             $seller->category_id = $validatedData('category_id');
    //             $seller->address = $validatedData('address');
    //             $seller->description = $validatedData('description');
    //             $seller->reg_no = $validatedData('reg_no');
    //             $seller->phone = $validatedData('phone');
    //             $seller->facebook = $validatedData('facebook');
    //             $seller->instagram = $validatedData('instagram');
    //             return response()->json(['seller' => $seller], 200);
    //         } else {
    //             return response()->json(['message' => 'Seller not found'], 404);
    //         }
    //     } catch (\Throwable $e) {
    //         Log::error($e->getMessage());
    //         return response()->json(['error' => 'Oops. Unable to update seller record'], 500);
    //     }
    // }
    public function update(Request $request)
    {
        try {
            $validatedData = $this->validateSellerUpdateRequest($request);
        } catch (\Illuminate\Validation\ValidationException $e) {
            return response()->json(['error' => $e->errors()], 422);
        }

        $seller = Seller::findOrFail($validatedData['id']);
        $seller->fill($validatedData);
        $seller->slug = Str::slug($validatedData['brand_name']);

        $this->updateUserRoleToSeller($validatedData['user_id']);

        if ($request->hasFile('logo')) {
            $seller->logo = '/storage/' . $request->file('logo')->store('sellerLogo', 'public');
        }

        $seller->save();

        return response()->json(['message' => 'Seller account updated successfully', 'seller' => $seller], 200);
    }

    protected function validateSellerUpdateRequest(Request $request)
    {
        return $request->validate([
            'id' => ['required', 'exists:sellers'],
            'user_id' => ['required'],
            'brand_name' => ['required', 'string'],
            'category_id' => ['required', 'string'],
            'address' => ['required', 'string'],
            'description' => ['required', 'max:217'],
            'phone' => ['required', 'string'],
            'logo' => ['nullable', 'image', 'max:512', 'mimes:jpg,jpeg,png'],
            'reg_no' => ['required', 'string', 'unique:sellers,reg_no'],
            'facebook' => [
                'required', 'url', 'max:100'
            ],
            'instagram' => ['required', 'url', 'max:100'],
        ]);
    }
}