82 lines
2.1 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Http\Controllers\Api\Banking;
use App\Abstracts\Http\ApiController;
use App\Http\Requests\Banking\Transaction as Request;
2022-06-01 10:15:55 +03:00
use App\Http\Resources\Banking\Transaction as Resource;
2019-11-16 10:21:14 +03:00
use App\Jobs\Banking\CreateTransaction;
use App\Jobs\Banking\DeleteTransaction;
use App\Jobs\Banking\UpdateTransaction;
use App\Models\Banking\Transaction;
class Transactions extends ApiController
{
/**
* Display a listing of the resource.
*
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
public function index()
{
2020-06-07 12:11:37 +03:00
$transactions = Transaction::with('account', 'category', 'contact')->collect(['paid_at'=> 'desc']);
2019-11-16 10:21:14 +03:00
2022-06-01 10:15:55 +03:00
return Resource::collection($transactions);
2019-11-16 10:21:14 +03:00
}
/**
* Display the specified resource.
*
* @param Transaction $transaction
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
public function show(Transaction $transaction)
{
2022-06-01 10:15:55 +03:00
return new Resource($transaction);
2019-11-16 10:21:14 +03:00
}
/**
* Store a newly created resource in storage.
*
* @param $request
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
public function store(Request $request)
{
$transaction = $this->dispatch(new CreateTransaction($request));
2022-06-01 10:15:55 +03:00
return $this->created(route('api.transactions.show', $transaction->id), new Resource($transaction));
2019-11-16 10:21:14 +03:00
}
/**
* Update the specified resource in storage.
*
* @param $transaction
* @param $request
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
public function update(Transaction $transaction, Request $request)
{
$transaction = $this->dispatch(new UpdateTransaction($transaction, $request));
2022-06-01 10:15:55 +03:00
return new Resource($transaction->fresh());
2019-11-16 10:21:14 +03:00
}
/**
* Remove the specified resource from storage.
*
* @param Transaction $transaction
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\Response
2019-11-16 10:21:14 +03:00
*/
public function destroy(Transaction $transaction)
{
try {
$this->dispatch(new DeleteTransaction($transaction));
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
}
}
}