123 lines
3.2 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;
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;
2017-10-16 21:02:32 +03:00
use App\Transformers\Setting\Currency as Transformer;
2017-09-14 22:21:00 +03:00
class Currencies extends ApiController
{
/**
* Display a listing of the resource.
*
* @return \Dingo\Api\Http\Response
*/
public function index()
{
$currencies = Currency::collect();
return $this->response->paginator($currencies, new Transformer());
}
/**
* Display the specified resource.
*
2019-11-16 10:21:14 +03:00
* @param int|string $id
2017-09-14 22:21:00 +03:00
* @return \Dingo\Api\Http\Response
*/
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();
}
2017-09-14 22:21:00 +03:00
return $this->response->item($currency, new Transformer());
}
/**
* Store a newly created resource in storage.
*
* @param $request
* @return \Dingo\Api\Http\Response
*/
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
2019-11-16 10:21:14 +03:00
return $this->response->created(url('api/currencies/' . $currency->id));
2017-09-14 22:21:00 +03:00
}
/**
* Update the specified resource in storage.
*
* @param $currency
* @param $request
* @return \Dingo\Api\Http\Response
*/
public function update(Currency $currency, Request $request)
{
2019-11-16 10:21:14 +03:00
try {
$currency = $this->dispatch(new UpdateCurrency($currency, $request));
return $this->item($currency->fresh(), new Transformer());
} catch(\Exception $e) {
$this->response->errorUnauthorized($e->getMessage());
}
}
/**
* Enable the specified resource in storage.
*
* @param Currency $currency
* @return \Dingo\Api\Http\Response
*/
public function enable(Currency $currency)
{
$currency = $this->dispatch(new UpdateCurrency($currency, request()->merge(['enabled' => 1])));
return $this->item($currency->fresh(), new Transformer());
}
/**
* Disable the specified resource in storage.
*
* @param Currency $currency
* @return \Dingo\Api\Http\Response
*/
public function disable(Currency $currency)
{
try {
$currency = $this->dispatch(new UpdateCurrency($currency, request()->merge(['enabled' => 0])));
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
return $this->item($currency->fresh(), new Transformer());
} catch(\Exception $e) {
$this->response->errorUnauthorized($e->getMessage());
}
2017-09-14 22:21:00 +03:00
}
/**
* Remove the specified resource from storage.
*
* @param Currency $currency
* @return \Dingo\Api\Http\Response
*/
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
2019-11-16 10:21:14 +03:00
return $this->response->noContent();
} catch(\Exception $e) {
$this->response->errorUnauthorized($e->getMessage());
}
2017-09-14 22:21:00 +03:00
}
}