| Current Path : /home/users/unlimited/www/dabbawali.sizzlingcafe.co.in/app/Services/ |
| Current File : /home/users/unlimited/www/dabbawali.sizzlingcafe.co.in/app/Services/ItemAddonService.php |
<?php
namespace App\Services;
use Exception;
use App\Models\Item;
use Illuminate\Support\Facades\Log;
use App\Http\Requests\PaginateRequest;
use App\Http\Requests\ItemAddonRequest;
use App\Models\ItemAddon;
class ItemAddonService
{
public $itemExtra;
protected $itemExtraFilter = [
'item_id',
'name',
'price',
'status'
];
/**
* @throws Exception
*/
public function list(PaginateRequest $request, Item $item)
{
try {
$requests = $request->all();
$method = $request->get('paginate', 0) == 1 ? 'paginate' : 'get';
$methodValue = $request->get('paginate', 0) == 1 ? $request->get('per_page', 10) : '*';
$orderColumn = $request->get('order_column') ?? 'id';
$orderType = $request->get('order_type') ?? 'desc';
return ItemAddon::with('item', 'addonItem')->where(['item_id' => $item->id])->where(function ($query) use ($requests) {
foreach ($requests as $key => $request) {
if (in_array($key, $this->itemExtraFilter)) {
$query->where($key, 'like', '%' . $request . '%');
}
}
})->orderBy($orderColumn, $orderType)->$method(
$methodValue
);
} catch (Exception $exception) {
Log::info($exception->getMessage());
throw new Exception($exception->getMessage(), 422);
}
}
/**
* @throws Exception
*/
public function store(ItemAddonRequest $request, Item $item)
{
try {
return ItemAddon::create($request->validated() + ['item_id' => $item->id]);
} catch (Exception $exception) {
Log::info($exception->getMessage());
throw new Exception($exception->getMessage(), 422);
}
}
/**
* @throws Exception
*/
public function destroy(Item $item, ItemAddon $itemExtra)
{
try {
if ($item->id == $itemExtra->item_id) {
$itemExtra->delete();
} else {
throw new Exception(trans('all.item_match'), 422);
}
} catch (Exception $exception) {
Log::info($exception->getMessage());
throw new Exception($exception->getMessage(), 422);
}
}
}