123 lines
3.1 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Http\Controllers\Api\Banking;
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\Banking\Account as Request;
2022-06-01 10:15:55 +03:00
use App\Http\Resources\Banking\Account as Resource;
2019-11-16 10:21:14 +03:00
use App\Jobs\Banking\CreateAccount;
use App\Jobs\Banking\DeleteAccount;
use App\Jobs\Banking\UpdateAccount;
2017-09-14 22:21:00 +03:00
use App\Models\Banking\Account;
class Accounts 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()
{
$accounts = Account::collect();
2022-06-01 10:15:55 +03:00
return Resource::collection($accounts);
2017-09-14 22:21:00 +03:00
}
/**
* Display the specified resource.
*
2019-11-16 10:21:14 +03:00
* @param $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 number
if (is_numeric($id)) {
$account = Account::find($id);
} else {
$account = Account::where('number', $id)->first();
}
2022-06-01 10:15:55 +03:00
return new Resource($account);
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
$account = $this->dispatch(new CreateAccount($request));
2017-09-14 22:21:00 +03:00
2022-06-01 10:15:55 +03:00
return $this->created(route('api.accounts.show', $account->id), new Resource($account));
2017-09-14 22:21:00 +03:00
}
/**
* Update the specified resource in storage.
*
* @param $account
* @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(Account $account, Request $request)
{
2019-11-16 10:21:14 +03:00
try {
$account = $this->dispatch(new UpdateAccount($account, $request));
2022-06-01 10:15:55 +03:00
return new Resource($account->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 Account $account
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
public function enable(Account $account)
{
$account = $this->dispatch(new UpdateAccount($account, request()->merge(['enabled' => 1])));
2022-06-01 10:15:55 +03:00
return new Resource($account->fresh());
2019-11-16 10:21:14 +03:00
}
/**
* Disable the specified resource in storage.
*
* @param Account $account
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
public function disable(Account $account)
{
try {
$account = $this->dispatch(new UpdateAccount($account, request()->merge(['enabled' => 0])));
2017-09-14 22:21:00 +03:00
2022-06-01 10:15:55 +03:00
return new Resource($account->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 Account $account
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\Response
2017-09-14 22:21:00 +03:00
*/
public function destroy(Account $account)
{
2019-11-16 10:21:14 +03:00
try {
$this->dispatch(new DeleteAccount($account));
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
}
}