127 lines
3.3 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\Currency as Request;
2022-06-01 10:15:55 +03:00
use App\Http\Resources\Setting\Currency as Resource;
2019-11-16 10:21:14 +03:00
use App\Jobs\Setting\CreateCurrency;
use App\Jobs\Setting\DeleteCurrency;
use App\Jobs\Setting\UpdateCurrency;
2017-09-14 22:21:00 +03:00
use App\Models\Setting\Currency;
class Currencies 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()
{
$currencies = Currency::collect();
2022-06-01 10:15:55 +03:00
return Resource::collection($currencies);
2017-09-14 22:21:00 +03:00
}
/**
* Display the specified resource.
*
2019-11-16 10:21:14 +03:00
* @param int|string $id
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2017-09-14 22:21:00 +03:00
*/
2019-11-16 10:21:14 +03:00
public function show($id)
2017-09-14 22:21:00 +03:00
{
2019-11-16 10:21:14 +03:00
// Check if we're querying by id or code
if (is_numeric($id)) {
$currency = Currency::find($id);
} else {
$currency = Currency::where('code', $id)->first();
}
if (! $currency instanceof Currency) {
return $this->errorInternal('No query results for model [' . Currency::class . '] ' . $id);
}
2022-06-01 10:15:55 +03:00
return new Resource($currency);
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
$currency = $this->dispatch(new CreateCurrency($request));
2017-09-14 22:21:00 +03:00
2022-06-01 10:15:55 +03:00
return $this->created(route('api.currencies.show', $currency->id), new Resource($currency));
2017-09-14 22:21:00 +03:00
}
/**
* Update the specified resource in storage.
*
* @param $currency
* @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(Currency $currency, Request $request)
{
2019-11-16 10:21:14 +03:00
try {
$currency = $this->dispatch(new UpdateCurrency($currency, $request));
2022-06-01 10:15:55 +03:00
return new Resource($currency->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 Currency $currency
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
public function enable(Currency $currency)
{
$currency = $this->dispatch(new UpdateCurrency($currency, request()->merge(['enabled' => 1])));
2022-06-01 10:15:55 +03:00
return new Resource($currency->fresh());
2019-11-16 10:21:14 +03:00
}
/**
* Disable the specified resource in storage.
*
* @param Currency $currency
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
public function disable(Currency $currency)
{
try {
$currency = $this->dispatch(new UpdateCurrency($currency, request()->merge(['enabled' => 0])));
2017-09-14 22:21:00 +03:00
2022-06-01 10:15:55 +03:00
return new Resource($currency->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 Currency $currency
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\Response
2017-09-14 22:21:00 +03:00
*/
public function destroy(Currency $currency)
{
2019-11-16 10:21:14 +03:00
try {
$this->dispatch(new DeleteCurrency($currency));
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
}
}