<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Requests\BusinessCategory\CreateCategoryRequest;
use App\Http\Requests\BusinessCategory\UpdateCategoryRequest;
use App\Http\Resources\BusinessCategory\BusinessCategoryResource;
use App\Model\BusinessCategory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class BusinessCategoryController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$status=request()->query('status');
$businesses=BusinessCategory::when($status
!== null,function($query) use($status){
$query->where('status',$status);
})->orderBy('id','desc')->get();
return responseSuccess(BusinessCategoryResource::
collection($businesses));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(CreateCategoryRequest $request)
{
$data=[
'title'=>$request['title'],
];
$business=DB::transaction(function() use($data,$request){
$business=BusinessCategory::create($data);
if($request->image){
$business->addMedia($request['image'])
->toMediaCollection('categoryimage');
}
return $business;
});
if($business!=null){
return responseSuccess($business,201,'
Business category is created successfully!');
}
else {
return responseError('Internal Server Error',500);
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$business=BusinessCategory::find($id);
return responseSuccess(new BusinessCategoryResource($business));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(UpdateCategoryRequest $request, $id)
{
$business=BusinessCategory::find($id);
if(is_null($business)){
return responseError('Internal Server Error',500);
}
else {
$business=DB::transaction(function() use($business,$request){
$business->title=$request['title'];
$business->save();
return $business;
});
}
if(is_null($business)){
return responseError('Internal Server Error',500);
}
else {
return responseSuccess($business,200,
'You have updated the data successfully!');
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$data=BusinessCategory::find($id);
if(is_null($data)){
return responseError('The category is not found',500);
}
else {
DB::transaction(function() use($data){
$data->delete();
return $data;
});
if(!is_null($data)){
return responseSuccess($data,201,'You have deleted the data successfully');
}
else {
return responseError('Error Occurred!');
}
}
}
}