New endpoints for transactions.

This commit is contained in:
James Cole 2018-12-04 19:36:54 +01:00
parent c0d6d0e28e
commit 64a3e46cbe
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
5 changed files with 68 additions and 101 deletions

View File

@ -35,6 +35,8 @@ use FireflyIII\Helpers\Filter\PositiveAmountFilter;
use FireflyIII\Models\Transaction; use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Transformers\AttachmentTransformer;
use FireflyIII\Transformers\PiggyBankEventTransformer;
use FireflyIII\Transformers\TransactionTransformer; use FireflyIII\Transformers\TransactionTransformer;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
@ -75,6 +77,44 @@ class TransactionController extends Controller
); );
} }
/**
* @param Request $request
* @param Transaction $transaction
*
* @return JsonResponse
*/
public function attachments(Request $request, Transaction $transaction): JsonResponse
{
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
$attachments = $transaction->transactionJournal->attachments()->get();
$resource = new FractalCollection($attachments, new AttachmentTransformer($this->parameters), 'attachments');
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
/**
* @param Request $request
* @param Transaction $transaction
*
* @return JsonResponse
*/
public function piggyBankEvents(Request $request, Transaction $transaction): JsonResponse
{
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl));
$events= $transaction->transactionJournal->piggyBankEvents()->get();
$resource = new FractalCollection($events, new PiggyBankEventTransformer($this->parameters), 'piggy_bank_events');
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
/** /**
* Remove the specified resource from storage. * Remove the specified resource from storage.
* *
@ -135,7 +175,6 @@ class TransactionController extends Controller
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
} }
/** /**
* Show a single transaction. * Show a single transaction.
* *

View File

@ -29,11 +29,12 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
/** /**
* Class PiggyBankEvent. * Class PiggyBankEvent.
* *
* @property PiggyBank $piggyBank * @property PiggyBank $piggyBank
* @property int $transaction_journal_id * @property int $transaction_journal_id
* @property int $piggy_bank_id * @property int $piggy_bank_id
* @property int $id * @property int $id
* @property Carbon date * @property Carbon date
* @property TransactionJournal transactionJournal
*/ */
class PiggyBankEvent extends Model class PiggyBankEvent extends Model
{ {

View File

@ -26,8 +26,6 @@ namespace FireflyIII\Transformers;
use FireflyIII\Models\Attachment; use FireflyIII\Models\Attachment;
use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface; use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item;
use League\Fractal\TransformerAbstract; use League\Fractal\TransformerAbstract;
use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\ParameterBag;
@ -36,19 +34,6 @@ use Symfony\Component\HttpFoundation\ParameterBag;
*/ */
class AttachmentTransformer extends TransformerAbstract class AttachmentTransformer extends TransformerAbstract
{ {
/**
* List of resources possible to include
*
* @var array
*/
protected $availableIncludes = ['user'];
/**
* List of resources to automatically include
*
* @var array
*/
protected $defaultIncludes = ['user'];
/** @var ParameterBag */ /** @var ParameterBag */
protected $parameters; protected $parameters;
@ -68,20 +53,6 @@ class AttachmentTransformer extends TransformerAbstract
$this->repository = app(AttachmentRepositoryInterface::class); $this->repository = app(AttachmentRepositoryInterface::class);
} }
/**
* Attach the user.
*
* @codeCoverageIgnore
*
* @param Attachment $attachment
*
* @return Item
*/
public function includeUser(Attachment $attachment): Item
{
return $this->item($attachment->user, new UserTransformer($this->parameters), 'users');
}
/** /**
* Transform attachment. * Transform attachment.
* *
@ -97,7 +68,8 @@ class AttachmentTransformer extends TransformerAbstract
'id' => (int)$attachment->id, 'id' => (int)$attachment->id,
'updated_at' => $attachment->updated_at->toAtomString(), 'updated_at' => $attachment->updated_at->toAtomString(),
'created_at' => $attachment->created_at->toAtomString(), 'created_at' => $attachment->created_at->toAtomString(),
'attachable_type' => $attachment->attachable_type, 'attachable_id' => $attachment->attachable_id,
'attachable_type' => str_replace('FireflyIII\\Models\\','',$attachment->attachable_type),
'md5' => $attachment->md5, 'md5' => $attachment->md5,
'filename' => $attachment->filename, 'filename' => $attachment->filename,
'download_uri' => route('api.v1.attachments.download', [$attachment->id]), 'download_uri' => route('api.v1.attachments.download', [$attachment->id]),

View File

@ -24,12 +24,9 @@ declare(strict_types=1);
namespace FireflyIII\Transformers; namespace FireflyIII\Transformers;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
use FireflyIII\Models\PiggyBankEvent; use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use Illuminate\Support\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\TransformerAbstract; use League\Fractal\TransformerAbstract;
use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\ParameterBag;
@ -38,19 +35,6 @@ use Symfony\Component\HttpFoundation\ParameterBag;
*/ */
class PiggyBankEventTransformer extends TransformerAbstract class PiggyBankEventTransformer extends TransformerAbstract
{ {
/**
* List of resources possible to include
*
* @var array
*/
protected $availableIncludes = ['piggy_bank', 'transaction'];
/**
* List of resources to automatically include
*
* @var array
*/
protected $defaultIncludes = [];
/** @var ParameterBag */ /** @var ParameterBag */
protected $parameters; protected $parameters;
@ -66,50 +50,6 @@ class PiggyBankEventTransformer extends TransformerAbstract
$this->parameters = $parameters; $this->parameters = $parameters;
} }
/**
* Include piggy bank into end result.
*
* @codeCoverageIgnore
*
* @param PiggyBankEvent $event
*
* @return Item
*/
public function includePiggyBank(PiggyBankEvent $event): Item
{
return $this->item($event->piggyBank, new PiggyBankTransformer($this->parameters), 'piggy_banks');
}
/**
* Include transaction into end result.
*
* @codeCoverageIgnore
*
* @param PiggyBankEvent $event
*
* @return Item
*/
public function includeTransaction(PiggyBankEvent $event): Item
{
$journal = $event->transactionJournal;
$pageSize = (int)app('preferences')->getForUser($journal->user, 'listPageSize', 50)->data;
// journals always use collector and limited using URL parameters.
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
$collector->setUser($journal->user);
$collector->withOpposingAccount()->withCategoryInformation()->withCategoryInformation();
$collector->setAllAssetAccounts();
$collector->setJournals(new Collection([$journal]));
if (null !== $this->parameters->get('start') && null !== $this->parameters->get('end')) {
$collector->setRange($this->parameters->get('start'), $this->parameters->get('end'));
}
$collector->setLimit($pageSize)->setPage($this->parameters->get('page'));
$transactions = $collector->getTransactions();
return $this->item($transactions->first(), new TransactionTransformer($this->parameters), 'transactions');
}
/** /**
* Convert piggy bank event. * Convert piggy bank event.
* *
@ -125,6 +65,8 @@ class PiggyBankEventTransformer extends TransformerAbstract
$accountRepos->setUser($account->user); $accountRepos->setUser($account->user);
$currencyId = (int)$accountRepos->getMetaValue($account, 'currency_id'); $currencyId = (int)$accountRepos->getMetaValue($account, 'currency_id');
$journal = $event->transactionJournal;
$transactionId = null;
$decimalPlaces = 2; $decimalPlaces = 2;
if ($currencyId > 0) { if ($currencyId > 0) {
/** @var CurrencyRepositoryInterface $repository */ /** @var CurrencyRepositoryInterface $repository */
@ -134,13 +76,24 @@ class PiggyBankEventTransformer extends TransformerAbstract
/** @noinspection NullPointerExceptionInspection */ /** @noinspection NullPointerExceptionInspection */
$decimalPlaces = $currency->decimal_places; $decimalPlaces = $currency->decimal_places;
} }
if (0 === $currencyId) {
$currency = app('amount')->getDefaultCurrencyByUser($account->user);
}
if (null !== $journal) {
$transactionId = $journal->transactions()->first()->id;
}
$data = [ $data = [
'id' => (int)$event->id, 'id' => (int)$event->id,
'updated_at' => $event->updated_at->toAtomString(), 'updated_at' => $event->updated_at->toAtomString(),
'created_at' => $event->created_at->toAtomString(), 'created_at' => $event->created_at->toAtomString(),
'amount' => round($event->amount, $decimalPlaces), 'amount' => round($event->amount, $decimalPlaces),
'links' => [ 'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_dp' => $currency->decimal_places,
'transaction_id' => $transactionId,
'links' => [
[ [
'rel' => 'self', 'rel' => 'self',
'uri' => '/piggy_bank_events/' . $event->id, 'uri' => '/piggy_bank_events/' . $event->id,

View File

@ -253,6 +253,8 @@ Route::group(
Route::get('', ['uses' => 'TransactionController@index', 'as' => 'index']); Route::get('', ['uses' => 'TransactionController@index', 'as' => 'index']);
Route::post('', ['uses' => 'TransactionController@store', 'as' => 'store']); Route::post('', ['uses' => 'TransactionController@store', 'as' => 'store']);
Route::get('{transaction}', ['uses' => 'TransactionController@show', 'as' => 'show']); Route::get('{transaction}', ['uses' => 'TransactionController@show', 'as' => 'show']);
Route::get('{transaction}/attachments', ['uses' => 'TransactionController@attachments', 'as' => 'attachments']);
Route::get('{transaction}/piggy_bank_events', ['uses' => 'TransactionController@piggyBankEvents', 'as' => 'piggy_bank_events']);
Route::put('{transaction}', ['uses' => 'TransactionController@update', 'as' => 'update']); Route::put('{transaction}', ['uses' => 'TransactionController@update', 'as' => 'update']);
Route::delete('{transaction}', ['uses' => 'TransactionController@delete', 'as' => 'delete']); Route::delete('{transaction}', ['uses' => 'TransactionController@delete', 'as' => 'delete']);
} }