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

namespace App\Http\Controllers\Admin;

use App\Models\Package;
use App\Models\Category;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use App\Models\SellerCategory;
use App\Http\Controllers\Controller;
use App\Models\NewsFlash;

class AdminSettingController extends Controller
{
    //store and update, using the id for update
    public function category(Request $request)
    {
        try {
            // Validate the incoming request data
            $validatedData = $request->validate([
                'name' => ['required', 'unique:seller_categories,name,' . $request->id], // Ensure name is unique except for the current category (for updating)
                'image' => ['required', 'mimes:jpg,jpeg,png'],
            ]);
        } catch (\Illuminate\Validation\ValidationException $e) {
            // Return validation error response if validation fails
            return response()->json(['error' => $e->errors()], 422);
        }

        // Check if the request contains an 'id', indicating an update operation
        if ($request->has('id')) {
            // Find the category by its ID
            $category = SellerCategory::findOrFail($request->id);
        } else {
            // Create a new category instance if no 'id' is provided
            $category = new SellerCategory();
        }
        $category->name = $validatedData['name'];

        // Generate slug based on the category name
        $category->slug = Str::slug($validatedData['name']);

        // Handle image upload if present
        if ($request->hasFile('image')) {
            // Store the image and update the category record with the file path
            $imagePath = $request->file('image')->store('categoryLogo', 'public');
            $category->image = '/storage/' . $imagePath;
        }

        // Save the category record to the database
        $category->save();

        // Return success response with the newly created category data
        return response()->json(['message' => 'Category created successfully', 'category' => $category], 200);
    }

    //store and update, using the id for update
    public function package(Request $request)
    {
        try {
            // Validate the incoming request data
            $validatedData = $request->validate([
                'name' => ['required', 'unique:packages,name,' . $request->id], // Ensure name is unique except for the current category (for updating)
                'description' => ['required', 'string'],
                'limit' => ['required', 'numeric'],
                'amount' => ['required', 'numeric'],
                'link' => ['required', 'url'],
            ]);
        } catch (\Illuminate\Validation\ValidationException $e) {
            return response()->json(['error' => $e->errors()], 422);
        }
        // Check if the request contains an 'id', indicating an update operation
        if ($request->has('id')) {
            // Find the package by its ID
            $package = Package::findOrFail($request->id);
        } else {
            // Create a new package instance if no 'id' is provided
            $package = new Package();
        }
        $package->fill($validatedData);

        // $package->name = $validatedData['name'];
        // $package->description = $validatedData['description'];
        // $package->amount = $validatedData['amount'];
        // $package->link = $validatedData['link'];

        // Save the category record to the database
        $package->save();

        // Return success response with the newly created category data
        return response()->json(['message' => 'Package created successfully', 'package' => $package], 200);
    }

    public function productCategory()
    {
        $categories = Category::with('children')->whereNull('parent_id')->get();

        return response()->json(['categories' => $categories], 200);
    }

    public function addProductCategory(Request $request) //Product Category
    {
        try {
            // Validate the incoming request data
            $validatedData = $request->validate([
                'name' => ['required', 'string', 'unique:categories,id'],
                'parent_id' => ['nullable', 'exists:categories,id'],
                'description' => ['required', 'string'],
                'image' => ['nullable', 'image', 'max:512', 'mimes:jpg,jpeg,png'],
            ]);
        } catch (\Illuminate\Validation\ValidationException $e) {
            // Return validation error response if validation fails
            return response()->json(['error' => $e->errors()], 422);
        }

        // Create a new category instance
        if ($request->has('id')) {
            // Find the category by its ID
            $category = Category::findOrFail($request->id);
        } else {
            // Create a new category instance if no 'id' is provided
            $category = new Category();
        }
        $category->fill($validatedData);

        // Generate and assign slug based on name
        $category->slug = Str::slug($validatedData['name']);

        // Handle category image upload if present in the request
        if ($request->hasFile('image')) {
            $category->image = '/storage/' . $request->file('image')->store('categoryImages', 'public');
        }

        // Save the new category
        $category->save();
        // Return success response with the newly created category data
        return response()->json(['message' => 'Category created successfully', 'category' => $category], 200);
    }

   
}