Make sure bills API is consistent.

This commit is contained in:
James Cole 2018-02-11 07:46:34 +01:00
parent 9b3abd3b19
commit 2ef1022c92
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
9 changed files with 116 additions and 159 deletions

View File

@ -130,11 +130,11 @@ class BillController extends Controller
$manager = new Manager(); $manager = new Manager();
$manager->parseIncludes(['attachments', 'notes', 'transactionJournals', 'user']); $manager->parseIncludes(['attachments', 'journals', 'user']);
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl)); $manager->setSerializer(new JsonApiSerializer($baseUrl));
$resource = new Item($bill, new BillTransformer($start, $end), 'bill'); $resource = new Item($bill, new BillTransformer($start, $end), 'bills');
return Response::json($manager->createData($resource)->toArray()); return Response::json($manager->createData($resource)->toArray());
} }
@ -155,18 +155,18 @@ class BillController extends Controller
$end = new Carbon($request->get('end')); $end = new Carbon($request->get('end'));
} }
$bill = $this->repository->store($request->getAll()); $bill = $this->repository->store($request->getAll());
$manager = new Manager(); $manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl)); $manager->setSerializer(new JsonApiSerializer($baseUrl));
$resource = new Item($bill, new BillTransformer($start, $end), 'bill'); $resource = new Item($bill, new BillTransformer($start, $end), 'bills');
return Response::json($manager->createData($resource)->toArray()); return Response::json($manager->createData($resource)->toArray());
} }
/** /**
* @param BillRequest $request * @param BillRequest $request
* @param Bill $bill * @param Bill $bill
@ -186,11 +186,12 @@ class BillController extends Controller
if (null !== $request->get('end')) { if (null !== $request->get('end')) {
$end = new Carbon($request->get('end')); $end = new Carbon($request->get('end'));
} }
$manager = new Manager(); $manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$manager->setSerializer(new JsonApiSerializer($baseUrl)); $manager->setSerializer(new JsonApiSerializer($baseUrl));
$resource = new Item($bill, new BillTransformer($start, $end), 'bill'); $resource = new Item($bill, new BillTransformer($start, $end), 'bills');
return Response::json($manager->createData($resource)->toArray()); return Response::json($manager->createData($resource)->toArray());

View File

@ -44,18 +44,18 @@ class BillRequest extends Request
public function getAll(): array public function getAll(): array
{ {
$data = [ $data = [
'name' => $this->string('name'), 'name' => $this->string('name'),
'match' => $this->string('match'), 'match' => $this->string('match'),
'amount_min' => $this->string('amount_min'), 'amount_min' => $this->string('amount_min'),
'amount_max' => $this->string('amount_max'), 'amount_max' => $this->string('amount_max'),
'currency_id' => $this->integer('currency_id'), //'currency_id' => $this->integer('currency_id'),
'currency_code' => $this->string('currency_code'), //'currency_code' => $this->string('currency_code'),
'date' => $this->date('date'), 'date' => $this->date('date'),
'repeat_freq' => $this->string('repeat_freq'), 'repeat_freq' => $this->string('repeat_freq'),
'skip' => $this->integer('skip'), 'skip' => $this->integer('skip'),
'automatch' => $this->boolean('automatch'), 'automatch' => $this->boolean('automatch'),
'active' => $this->boolean('active'), 'active' => $this->boolean('active'),
'notes' => $this->string('notes'), 'notes' => $this->string('notes'),
]; ];
return $data; return $data;
@ -67,18 +67,18 @@ class BillRequest extends Request
public function rules() public function rules()
{ {
$rules = [ $rules = [
'name' => 'required|between:1,255|uniqueObjectForUser:bills,name', 'name' => 'required|between:1,255|uniqueObjectForUser:bills,name',
'match' => 'required|between:1,255|uniqueObjectForUser:bills,match', 'match' => 'required|between:1,255|uniqueObjectForUser:bills,match',
'amount_min' => 'required|numeric|more:0', 'amount_min' => 'required|numeric|more:0',
'amount_max' => 'required|numeric|more:0', 'amount_max' => 'required|numeric|more:0',
'currency_id' => 'numeric|exists:transaction_currencies,id|required_without:currency_code', //'currency_id' => 'numeric|exists:transaction_currencies,id|required_without:currency_code',
'currency_code' => 'min:3|max:3|exists:transaction_currencies,code|required_without:currency_id', //'currency_code' => 'min:3|max:3|exists:transaction_currencies,code|required_without:currency_id',
'date' => 'required|date', 'date' => 'required|date',
'repeat_freq' => 'required|in:weekly,monthly,quarterly,half-year,yearly', 'repeat_freq' => 'required|in:weekly,monthly,quarterly,half-year,yearly',
'skip' => 'required|between:0,31', 'skip' => 'required|between:0,31',
'automatch' => 'required|boolean', 'automatch' => 'required|boolean',
'active' => 'required|boolean', 'active' => 'required|boolean',
'notes' => 'between:1,65536', 'notes' => 'between:1,65536',
]; ];
switch ($this->method()) { switch ($this->method()) {
default: default:

View File

@ -67,6 +67,8 @@ class AttachmentTransformer extends TransformerAbstract
{ {
return [ return [
'id' => (int)$attachment->id, 'id' => (int)$attachment->id,
'updated_at' => $attachment->updated_at->toAtomString(),
'created_at' => $attachment->created_at->toAtomString(),
'attachable_type' => $attachment->attachable_type, 'attachable_type' => $attachment->attachable_type,
'md5' => $attachment->md5, 'md5' => $attachment->md5,
'filename' => $attachment->filename, 'filename' => $attachment->filename,

View File

@ -25,11 +25,12 @@ namespace FireflyIII\Transformers;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Models\Bill; use FireflyIII\Models\Bill;
use FireflyIII\Models\Note;
use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item; use League\Fractal\Resource\Item;
use League\Fractal\TransformerAbstract; use League\Fractal\TransformerAbstract;
use League\Fractal\Resource\Collection as FractalCollection;
/** /**
* Class BillTransformer * Class BillTransformer
@ -41,13 +42,13 @@ class BillTransformer extends TransformerAbstract
* *
* @var array * @var array
*/ */
protected $availableIncludes = ['attachments', 'notes', 'transactionJournals', 'user']; protected $availableIncludes = ['attachments', 'journals', 'user'];
/** /**
* List of resources to automatically include * List of resources to automatically include
* *
* @var array * @var array
*/ */
protected $defaultIncludes = ['notes',]; protected $defaultIncludes = [];
/** @var Carbon */ /** @var Carbon */
private $end = null; private $end = null;
/** @var Carbon */ /** @var Carbon */
@ -65,18 +66,6 @@ class BillTransformer extends TransformerAbstract
$this->end = $end; $this->end = $end;
} }
/**
* @param Bill $bill
*
* @return \League\Fractal\Resource\Item
*/
public function includeUser(Bill $bill): Item
{
$user = $bill->user()->first();
return $this->item($user, new UserTransformer, 'user');
}
/** /**
* @param Bill $bill * @param Bill $bill
* *
@ -86,7 +75,7 @@ class BillTransformer extends TransformerAbstract
{ {
$attachments = $bill->attachments()->get(); $attachments = $bill->attachments()->get();
return $this->collection($attachments, new AttachmentTransformer, 'attachment'); return $this->collection($attachments, new AttachmentTransformer, 'attachments');
} }
/** /**
@ -94,23 +83,23 @@ class BillTransformer extends TransformerAbstract
* *
* @return FractalCollection * @return FractalCollection
*/ */
public function includeNotes(Bill $bill): FractalCollection public function includeJournals(Bill $bill): FractalCollection
{
$notes = $bill->notes()->get();
return $this->collection($notes, new NoteTransformer, 'note');
}
/**
* @param Bill $bill
*
* @return FractalCollection
*/
public function includeTransactionJournals(Bill $bill): FractalCollection
{ {
$journals = $bill->transactionJournals()->get(); $journals = $bill->transactionJournals()->get();
return $this->collection($journals, new JournalTransformer, 'transaction_journal'); return $this->collection($journals, new TransactionJournalTransformer, 'journals');
}
/**
* @param Bill $bill
*
* @return \League\Fractal\Resource\Item
*/
public function includeUser(Bill $bill): Item
{
$user = $bill->user()->first();
return $this->item($user, new UserTransformer, 'users');
} }
/** /**
@ -124,6 +113,8 @@ class BillTransformer extends TransformerAbstract
$payDates = $this->payDates($bill); $payDates = $this->payDates($bill);
$data = [ $data = [
'id' => (int)$bill->id, 'id' => (int)$bill->id,
'updated_at' => $bill->updated_at->toAtomString(),
'created_at' => $bill->created_at->toAtomString(),
'name' => $bill->name, 'name' => $bill->name,
'match' => explode(',', $bill->match), 'match' => explode(',', $bill->match),
'amount_min' => round($bill->amount_min, 2), 'amount_min' => round($bill->amount_min, 2),
@ -137,15 +128,19 @@ class BillTransformer extends TransformerAbstract
'pay_dates' => $payDates, 'pay_dates' => $payDates,
'paid_dates' => $paidData['paid_dates'], 'paid_dates' => $paidData['paid_dates'],
'next_expected_match' => $paidData['next_expected_match'], 'next_expected_match' => $paidData['next_expected_match'],
'notes' => null,
'links' => [ 'links' => [
[ [
'rel' => 'self', 'rel' => 'self',
'uri' => '/bill/' . $bill->id, 'uri' => '/bills/' . $bill->id,
], ],
], ],
]; ];
/** @var Note $note */
// todo updated at, created at $note = $bill->notes()->first();
if (!is_null($note)) {
$data['notes'] = $note->text;
}
return $data; return $data;

View File

@ -1,60 +0,0 @@
<?php
/**
* NoteTransformer.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Transformers;
use FireflyIII\Models\Note;
use League\CommonMark\CommonMarkConverter;
use League\Fractal\TransformerAbstract;
/**
* Class NoteTransformer
*/
class NoteTransformer extends TransformerAbstract
{
/**
* @param Note $note
*
* @return array
*/
public function transform(Note $note): array
{
$converter = new CommonMarkConverter;
return [
'id' => (int)$note->id,
'notable_type' => $note->noteable_type,
'title' => $note->title,
'text' => $note->text,
'markdown' => $converter->convertToHtml($note->text),
'links' => [
[
'rel' => 'self',
'uri' => '/note/' . $note->id,
],
],
];
}
}

View File

@ -24,15 +24,15 @@ declare(strict_types=1);
namespace FireflyIII\Transformers; namespace FireflyIII\Transformers;
use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalTaskerInterface;
use League\Fractal\Resource\Collection as FractalCollection; use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\TransformerAbstract; use League\Fractal\TransformerAbstract;
/** /**
* Class JournalTransformer * Class TransactionJournalTransformer
*/ */
class JournalTransformer extends TransformerAbstract class TransactionJournalTransformer extends TransformerAbstract
{ {
/** /**
@ -40,13 +40,13 @@ class JournalTransformer extends TransformerAbstract
* *
* @var array * @var array
*/ */
protected $availableIncludes = ['attachments', 'notes', 'transactions', 'user', 'tags', 'budget', 'category', 'bill', 'meta', 'piggy_bank_events']; protected $availableIncludes = ['attachments', 'transactions', 'user', 'tags', 'budget', 'category', 'bill', 'meta', 'piggy_bank_events'];
/** /**
* List of resources to automatically include * List of resources to automatically include
* *
* @var array * @var array
*/ */
protected $defaultIncludes = ['transactions',]; protected $defaultIncludes = ['transactions'];
/** /**
* @param TransactionJournal $journal * @param TransactionJournal $journal
@ -55,13 +55,12 @@ class JournalTransformer extends TransformerAbstract
*/ */
public function includeTransactions(TransactionJournal $journal): FractalCollection public function includeTransactions(TransactionJournal $journal): FractalCollection
{ {
$tasker = app(JournalTaskerInterface::class); $set = $journal->transactions()->where('amount', '<', 0)->get(['transactions.*']);
$tasker->setUser($journal->user);
$transactions = $tasker->getTransactionsOverview($journal);
return $this->collection($transactions, new TransactionTransformer, 'transaction'); return $this->collection($set, new TransactionTransformer, 'transactions');
} }
/** /**
* @param TransactionJournal $journal * @param TransactionJournal $journal
* *
@ -69,20 +68,30 @@ class JournalTransformer extends TransformerAbstract
*/ */
public function transform(TransactionJournal $journal): array public function transform(TransactionJournal $journal): array
{ {
return [ $data = [
'id' => (int)$journal->id, 'id' => (int)$journal->id,
'updated_at' => $journal->updated_at->toAtomString(),
'created_at' => $journal->created_at->toAtomString(),
'type' => $journal->transactionType->type, 'type' => $journal->transactionType->type,
'description' => $journal->description, 'description' => $journal->description,
'date' => $journal->date->format('Y-m-d'), 'date' => $journal->date->format('Y-m-d'),
'order' => $journal->order, 'order' => $journal->order,
'completed' => $journal->completed, 'completed' => $journal->completed,
'notes' => null,
'links' => [ 'links' => [
[ [
'rel' => 'self', 'rel' => 'self',
'uri' => '/transaction_journal/' . $journal->id, 'uri' => '/journals/' . $journal->id,
], ],
], ],
]; ];
/** @var Note $note */
$note = $journal->notes()->first();
if (!is_null($note)) {
$data['notes'] = $note->text;
}
return $data;
} }
} }

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Transformers; namespace FireflyIII\Transformers;
use FireflyIII\Models\Transaction;
use League\Fractal\TransformerAbstract; use League\Fractal\TransformerAbstract;
/** /**
@ -32,37 +33,41 @@ use League\Fractal\TransformerAbstract;
class TransactionTransformer extends TransformerAbstract class TransactionTransformer extends TransformerAbstract
{ {
/** /**
* @param array $original * @param Transaction $transaction
* *
* @return array * @return array
*/ */
public function transform(array $original): array public function transform(Transaction $transaction): array
{ {
$id = $original['source_id']; $opposing = Transaction
$foreignAmount = null; ::where('transaction_journal_id', $transaction->transaction_journal_id)
if (!is_null($original['foreign_source_amount'])) { ->where('identifier', $transaction->identifier)
$foreignAmount = round($original['foreign_source_amount'], $original['foreign_currency_dp']); ->where('amount', $transaction->amount * -1)
} ->whereNull('deleted_at')
$return = [ ->first(['transactions.*']);
'id' => $id,
'amount' => round($original['source_amount'], $original['transaction_currency_dp']), $data = [
'currency_id' => $original['transaction_currency_id'], 'id' => (int)$transaction->id,
'currency_code' => $original['transaction_currency_code'], 'updated_at' => $transaction->updated_at->toAtomString(),
'foreign_amount' => $foreignAmount, 'created_at' => $transaction->created_at->toAtomString(),
'foreign_currency_id' => $original['foreign_currency_id'], 'source_id' => (int)$transaction->account_id,
'foreign_currency_code' => $original['foreign_currency_code'], 'destination_id' => $opposing->account_id,
'description' => $original['description'], 'description' => $transaction->description,
'links' => [ 'currency_id' => (int)$transaction->transaction_currency_id,
'amount' => (float)$transaction->amount,
'foreign_currency_id' => is_null($transaction->foreign_currency_id) ? null : (int)$transaction->foreign_currency_id,
'foreign_amount' => is_null($transaction->foreign_amount) ? null : (float)$transaction->foreign_amount,
'identifier' => (int)$transaction->identifier,
'links' => [
[ [
'rel' => 'self', 'rel' => 'self',
'uri' => '/transaction/' . $id, 'uri' => '/transactions/' . $transaction->id,
], ],
], ],
]; ];
// todo source account, dest account, budget, category return $data;
return $return;
} }
} }

View File

@ -41,11 +41,16 @@ class UserTransformer extends TransformerAbstract
{ {
return [ return [
'id' => (int)$user->id, 'id' => (int)$user->id,
'links' => [ 'updated_at' => $user->updated_at->toAtomString(),
'created_at' => $user->created_at->toAtomString(),
'email' => $user->email,
'blocked' => intval($user->blocked) === 1,
'blocked_code' => $user->blocked_code,
'links' => [
[ [
'rel' => 'self', 'rel' => 'self',
'uri' => '/user/' . $user->id, 'uri' => '/users/' . $user->id,
], ],
], ],
]; ];

View File

@ -20,7 +20,7 @@
*/ */
Route::group( Route::group(
['middleware' => ['auth:api','bindings'], 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'bill', 'as' => 'api.v1.bills.'], function () { ['middleware' => ['auth:api','bindings'], 'namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'bills', 'as' => 'api.v1.bills.'], function () {
// Bills API routes: // Bills API routes:
Route::get('', ['uses' => 'BillController@index', 'as' => 'index']); Route::get('', ['uses' => 'BillController@index', 'as' => 'index']);