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->parseIncludes(['attachments', 'notes', 'transactionJournals', 'user']);
$manager->parseIncludes(['attachments', 'journals', 'user']);
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$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());
}
@ -155,18 +155,18 @@ class BillController extends Controller
$end = new Carbon($request->get('end'));
}
$bill = $this->repository->store($request->getAll());
$bill = $this->repository->store($request->getAll());
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$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());
}
/**
* @param BillRequest $request
* @param Bill $bill
@ -186,11 +186,12 @@ class BillController extends Controller
if (null !== $request->get('end')) {
$end = new Carbon($request->get('end'));
}
$manager = new Manager();
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
$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());

View File

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

View File

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

View File

@ -25,11 +25,12 @@ namespace FireflyIII\Transformers;
use Carbon\Carbon;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Note;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Support\Collection;
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item;
use League\Fractal\TransformerAbstract;
use League\Fractal\Resource\Collection as FractalCollection;
/**
* Class BillTransformer
@ -41,13 +42,13 @@ class BillTransformer extends TransformerAbstract
*
* @var array
*/
protected $availableIncludes = ['attachments', 'notes', 'transactionJournals', 'user'];
protected $availableIncludes = ['attachments', 'journals', 'user'];
/**
* List of resources to automatically include
*
* @var array
*/
protected $defaultIncludes = ['notes',];
protected $defaultIncludes = [];
/** @var Carbon */
private $end = null;
/** @var Carbon */
@ -65,18 +66,6 @@ class BillTransformer extends TransformerAbstract
$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
*
@ -86,7 +75,7 @@ class BillTransformer extends TransformerAbstract
{
$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
*/
public function includeNotes(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
public function includeJournals(Bill $bill): FractalCollection
{
$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);
$data = [
'id' => (int)$bill->id,
'updated_at' => $bill->updated_at->toAtomString(),
'created_at' => $bill->created_at->toAtomString(),
'name' => $bill->name,
'match' => explode(',', $bill->match),
'amount_min' => round($bill->amount_min, 2),
@ -137,15 +128,19 @@ class BillTransformer extends TransformerAbstract
'pay_dates' => $payDates,
'paid_dates' => $paidData['paid_dates'],
'next_expected_match' => $paidData['next_expected_match'],
'notes' => null,
'links' => [
[
'rel' => 'self',
'uri' => '/bill/' . $bill->id,
'uri' => '/bills/' . $bill->id,
],
],
];
// todo updated at, created at
/** @var Note $note */
$note = $bill->notes()->first();
if (!is_null($note)) {
$data['notes'] = $note->text;
}
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;
use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalTaskerInterface;
use League\Fractal\Resource\Collection as FractalCollection;
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
*/
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
*
* @var array
*/
protected $defaultIncludes = ['transactions',];
protected $defaultIncludes = ['transactions'];
/**
* @param TransactionJournal $journal
@ -55,13 +55,12 @@ class JournalTransformer extends TransformerAbstract
*/
public function includeTransactions(TransactionJournal $journal): FractalCollection
{
$tasker = app(JournalTaskerInterface::class);
$tasker->setUser($journal->user);
$transactions = $tasker->getTransactionsOverview($journal);
$set = $journal->transactions()->where('amount', '<', 0)->get(['transactions.*']);
return $this->collection($transactions, new TransactionTransformer, 'transaction');
return $this->collection($set, new TransactionTransformer, 'transactions');
}
/**
* @param TransactionJournal $journal
*
@ -69,20 +68,30 @@ class JournalTransformer extends TransformerAbstract
*/
public function transform(TransactionJournal $journal): array
{
return [
$data = [
'id' => (int)$journal->id,
'updated_at' => $journal->updated_at->toAtomString(),
'created_at' => $journal->created_at->toAtomString(),
'type' => $journal->transactionType->type,
'description' => $journal->description,
'date' => $journal->date->format('Y-m-d'),
'order' => $journal->order,
'completed' => $journal->completed,
'notes' => null,
'links' => [
[
'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;
use FireflyIII\Models\Transaction;
use League\Fractal\TransformerAbstract;
/**
@ -32,37 +33,41 @@ use League\Fractal\TransformerAbstract;
class TransactionTransformer extends TransformerAbstract
{
/**
* @param array $original
* @param Transaction $transaction
*
* @return array
*/
public function transform(array $original): array
public function transform(Transaction $transaction): array
{
$id = $original['source_id'];
$foreignAmount = null;
if (!is_null($original['foreign_source_amount'])) {
$foreignAmount = round($original['foreign_source_amount'], $original['foreign_currency_dp']);
}
$return = [
'id' => $id,
'amount' => round($original['source_amount'], $original['transaction_currency_dp']),
'currency_id' => $original['transaction_currency_id'],
'currency_code' => $original['transaction_currency_code'],
'foreign_amount' => $foreignAmount,
'foreign_currency_id' => $original['foreign_currency_id'],
'foreign_currency_code' => $original['foreign_currency_code'],
'description' => $original['description'],
'links' => [
$opposing = Transaction
::where('transaction_journal_id', $transaction->transaction_journal_id)
->where('identifier', $transaction->identifier)
->where('amount', $transaction->amount * -1)
->whereNull('deleted_at')
->first(['transactions.*']);
$data = [
'id' => (int)$transaction->id,
'updated_at' => $transaction->updated_at->toAtomString(),
'created_at' => $transaction->created_at->toAtomString(),
'source_id' => (int)$transaction->account_id,
'destination_id' => $opposing->account_id,
'description' => $transaction->description,
'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',
'uri' => '/transaction/' . $id,
'uri' => '/transactions/' . $transaction->id,
],
],
];
// todo source account, dest account, budget, category
return $return;
return $data;
}
}

View File

@ -41,11 +41,16 @@ class UserTransformer extends TransformerAbstract
{
return [
'id' => (int)$user->id,
'links' => [
'id' => (int)$user->id,
'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',
'uri' => '/user/' . $user->id,
'uri' => '/users/' . $user->id,
],
],
];

View File

@ -20,7 +20,7 @@
*/
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:
Route::get('', ['uses' => 'BillController@index', 'as' => 'index']);