116 lines
2.7 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Http\Controllers\Api\Settings;
2019-11-16 10:21:14 +03:00
use App\Abstracts\Http\ApiController;
2017-09-14 22:21:00 +03:00
use App\Http\Requests\Setting\Tax as Request;
2022-06-01 10:15:55 +03:00
use App\Http\Resources\Setting\Tax as Resource;
2019-11-16 10:21:14 +03:00
use App\Jobs\Setting\CreateTax;
use App\Jobs\Setting\DeleteTax;
use App\Jobs\Setting\UpdateTax;
2017-09-14 22:21:00 +03:00
use App\Models\Setting\Tax;
class Taxes extends ApiController
{
/**
* Display a listing of the resource.
*
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2017-09-14 22:21:00 +03:00
*/
public function index()
{
$taxes = Tax::collect();
2022-06-01 10:15:55 +03:00
return Resource::collection($taxes);
2017-09-14 22:21:00 +03:00
}
/**
* Display the specified resource.
*
* @param Tax $tax
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2017-09-14 22:21:00 +03:00
*/
public function show(Tax $tax)
{
2022-06-01 10:15:55 +03:00
return new Resource($tax);
2017-09-14 22:21:00 +03:00
}
/**
* Store a newly created resource in storage.
*
* @param $request
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2017-09-14 22:21:00 +03:00
*/
public function store(Request $request)
{
2019-11-16 10:21:14 +03:00
$tax = $this->dispatch(new CreateTax($request));
2017-09-14 22:21:00 +03:00
2022-06-01 10:15:55 +03:00
return $this->created(route('api.taxes.show', $tax->id), new Resource($tax));
2017-09-14 22:21:00 +03:00
}
/**
* Update the specified resource in storage.
*
* @param $tax
* @param $request
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2017-09-14 22:21:00 +03:00
*/
public function update(Tax $tax, Request $request)
{
2019-11-16 10:21:14 +03:00
try {
$tax = $this->dispatch(new UpdateTax($tax, $request));
2022-06-01 10:15:55 +03:00
return new Resource($tax->fresh());
2019-11-16 10:21:14 +03:00
} catch(\Exception $e) {
2022-06-01 10:15:55 +03:00
$this->errorUnauthorized($e->getMessage());
2019-11-16 10:21:14 +03:00
}
}
/**
* Enable the specified resource in storage.
*
* @param Tax $tax
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
public function enable(Tax $tax)
{
$tax = $this->dispatch(new UpdateTax($tax, request()->merge(['enabled' => 1])));
2022-06-01 10:15:55 +03:00
return new Resource($tax->fresh());
2019-11-16 10:21:14 +03:00
}
/**
* Disable the specified resource in storage.
*
* @param Tax $tax
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
public function disable(Tax $tax)
{
try {
$tax = $this->dispatch(new UpdateTax($tax, request()->merge(['enabled' => 0])));
2017-09-14 22:21:00 +03:00
2022-06-01 10:15:55 +03:00
return new Resource($tax->fresh());
2019-11-16 10:21:14 +03:00
} catch(\Exception $e) {
2022-06-01 10:15:55 +03:00
$this->errorUnauthorized($e->getMessage());
2019-11-16 10:21:14 +03:00
}
2017-09-14 22:21:00 +03:00
}
/**
* Remove the specified resource from storage.
*
* @param Tax $tax
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\Response
2017-09-14 22:21:00 +03:00
*/
public function destroy(Tax $tax)
{
2019-11-16 10:21:14 +03:00
try {
$this->dispatch(new DeleteTax($tax));
2017-09-14 22:21:00 +03:00
2022-06-01 10:15:55 +03:00
return $this->noContent();
2019-11-16 10:21:14 +03:00
} catch(\Exception $e) {
2022-06-01 10:15:55 +03:00
$this->errorUnauthorized($e->getMessage());
2019-11-16 10:21:14 +03:00
}
2017-09-14 22:21:00 +03:00
}
}