File "StoreLocationsApiController.php"

Full Path: /home/clickysoft/public_html/jmapi5.clickysoft.net/app/Http/Controllers/Api/V1/Admin/StoreLocationsApiController.php
File size: 3.24 KB
MIME-type: text/x-php
Charset: utf-8

<?php

namespace App\Http\Controllers\Api\V1\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StoreStoreLocationRequest;
use App\Http\Requests\Admin\UpdateStoreLocationRequest;
use App\Http\Resources\Admin\StoreLocationEditResource;
use App\Http\Resources\Admin\StoreLocationResource;
use App\Models\StoreLocation;
use Gate;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class StoreLocationsApiController extends Controller
{
    public function index(Request $request)
    {
        abort_if(Gate::denies('store_location_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $noPaging = $request->get('no_paging') === "true";

        $locations = StoreLocation::when($request->filled('search'), function ($query) use ($request) {
            $search = addslashes($request->get('search'));
            $query->whereRaw("title like '%".$search."%' OR address like '%".$search."%'");
        })->orderBy('created_at', 'DESC');

        $locations = $noPaging ? $locations->where("status", 1)->get() : $locations->paginate(50);

        StoreLocationResource::withoutWrapping();
        return StoreLocationResource::collection($locations);
    }

    public function available_locations()
    {
        $locations = StoreLocation::active()->orderBy('created_at', 'DESC')->get();
        StoreLocationResource::withoutWrapping();
        return StoreLocationResource::collection($locations);
    }

    public function store(StoreStoreLocationRequest $request)
    {
        $store_location = StoreLocation::create($request->validated());

        return (new StoreLocationResource($store_location))
            ->response()
            ->setStatusCode(Response::HTTP_CREATED);
    }

    public function show(StoreLocation $store_location)
    {
        abort_if(Gate::denies('store_location_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        return new StoreLocationEditResource($store_location);
    }

    public function update(UpdateStoreLocationRequest $request, StoreLocation $store_location)
    {
        $store_location->update($request->validated());

        return (new StoreLocationResource($store_location))
            ->response()
            ->setStatusCode(Response::HTTP_CREATED);
    }

    public function toggleStatus(StoreLocation $store_location)
    {
        $store_location->status = !$store_location->status;
        $store_location->save();
        return response(['message' => 'Store location status updated successfully'], Response::HTTP_OK);
    }

    public function destroy(StoreLocation $store_location)
    {
        abort_if(Gate::denies('store_location_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        try {
            $store_location->delete();
            return response(['message' => 'Store location deleted successfully'], Response::HTTP_OK);
        }catch (\Exception $e){
            \Log::channel('db_errors')->info('Record Deletion Error : Store Location -> '.$store_location->id);
            \Log::channel('db_errors')->info($e->getMessage());
            return response([
                'message' => "Record not deleted.",
                'errors' => ["error" => ["Unable to delete store location."]]
            ], Response::HTTP_INTERNAL_SERVER_ERROR);
        }
    }
}