mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Expand transformers to include other objects.
This commit is contained in:
parent
db02fefcf4
commit
9b3abd3b19
@ -81,6 +81,8 @@ class BillController extends Controller
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function index(Request $request)
|
||||
@ -128,7 +130,7 @@ class BillController extends Controller
|
||||
|
||||
|
||||
$manager = new Manager();
|
||||
$manager->parseIncludes(['attachments']);
|
||||
$manager->parseIncludes(['attachments', 'notes', 'transactionJournals', 'user']);
|
||||
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||
|
||||
@ -138,11 +140,9 @@ class BillController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
* @param BillRequest $request
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function store(BillRequest $request)
|
||||
{
|
||||
@ -158,7 +158,6 @@ class BillController extends Controller
|
||||
$bill = $this->repository->store($request->getAll());
|
||||
|
||||
$manager = new Manager();
|
||||
$manager->parseIncludes(['attachments']);
|
||||
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||
|
||||
@ -169,12 +168,10 @@ class BillController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
* @param BillRequest $request
|
||||
* @param Bill $bill
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \FireflyIII\Models\Bill $bill
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function update(BillRequest $request, Bill $bill)
|
||||
{
|
||||
@ -190,7 +187,6 @@ class BillController extends Controller
|
||||
$end = new Carbon($request->get('end'));
|
||||
}
|
||||
$manager = new Manager();
|
||||
$manager->parseIncludes(['attachments']);
|
||||
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||
|
||||
|
@ -25,6 +25,7 @@ namespace FireflyIII\Transformers;
|
||||
|
||||
|
||||
use FireflyIII\Models\Attachment;
|
||||
use League\Fractal\Resource\Item;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
/**
|
||||
@ -32,6 +33,31 @@ use League\Fractal\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 = [];
|
||||
|
||||
/**
|
||||
* @param Attachment $attachment
|
||||
*
|
||||
* @return Item
|
||||
*/
|
||||
public function includeUser(Attachment $attachment): Item
|
||||
{
|
||||
$user = $attachment->user()->first();
|
||||
|
||||
return $this->item($user, new UserTransformer, 'user');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Attachment $attachment
|
||||
*
|
||||
|
@ -27,7 +27,9 @@ use Carbon\Carbon;
|
||||
use FireflyIII\Models\Bill;
|
||||
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
||||
use Illuminate\Support\Collection;
|
||||
use League\Fractal\Resource\Item;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
use League\Fractal\Resource\Collection as FractalCollection;
|
||||
|
||||
/**
|
||||
* Class BillTransformer
|
||||
@ -39,7 +41,7 @@ class BillTransformer extends TransformerAbstract
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $availableIncludes = ['attachments', 'notes'];
|
||||
protected $availableIncludes = ['attachments', 'notes', 'transactionJournals', 'user'];
|
||||
/**
|
||||
* List of resources to automatically include
|
||||
*
|
||||
@ -66,9 +68,21 @@ class BillTransformer extends TransformerAbstract
|
||||
/**
|
||||
* @param Bill $bill
|
||||
*
|
||||
* @return \League\Fractal\Resource\Collection
|
||||
* @return \League\Fractal\Resource\Item
|
||||
*/
|
||||
public function includeAttachments(Bill $bill)
|
||||
public function includeUser(Bill $bill): Item
|
||||
{
|
||||
$user = $bill->user()->first();
|
||||
|
||||
return $this->item($user, new UserTransformer, 'user');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Bill $bill
|
||||
*
|
||||
* @return FractalCollection
|
||||
*/
|
||||
public function includeAttachments(Bill $bill): FractalCollection
|
||||
{
|
||||
$attachments = $bill->attachments()->get();
|
||||
|
||||
@ -78,15 +92,27 @@ class BillTransformer extends TransformerAbstract
|
||||
/**
|
||||
* @param Bill $bill
|
||||
*
|
||||
* @return \League\Fractal\Resource\Collection
|
||||
* @return FractalCollection
|
||||
*/
|
||||
public function includeNotes(Bill $bill)
|
||||
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
|
||||
{
|
||||
$journals = $bill->transactionJournals()->get();
|
||||
|
||||
return $this->collection($journals, new JournalTransformer, 'transaction_journal');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Bill $bill
|
||||
*
|
||||
@ -119,8 +145,7 @@ class BillTransformer extends TransformerAbstract
|
||||
],
|
||||
];
|
||||
|
||||
// todo: attachments, journals, notes
|
||||
|
||||
// todo updated at, created at
|
||||
|
||||
return $data;
|
||||
|
||||
|
88
app/Transformers/JournalTransformer.php
Normal file
88
app/Transformers/JournalTransformer.php
Normal file
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/**
|
||||
* JournalTransformer.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\TransactionJournal;
|
||||
use FireflyIII\Repositories\Journal\JournalTaskerInterface;
|
||||
use League\Fractal\Resource\Collection as FractalCollection;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
/**
|
||||
* Class JournalTransformer
|
||||
*/
|
||||
class JournalTransformer extends TransformerAbstract
|
||||
{
|
||||
|
||||
/**
|
||||
* List of resources possible to include
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $availableIncludes = ['attachments', 'notes', 'transactions', 'user', 'tags', 'budget', 'category', 'bill', 'meta', 'piggy_bank_events'];
|
||||
/**
|
||||
* List of resources to automatically include
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $defaultIncludes = ['transactions',];
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return FractalCollection
|
||||
*/
|
||||
public function includeTransactions(TransactionJournal $journal): FractalCollection
|
||||
{
|
||||
$tasker = app(JournalTaskerInterface::class);
|
||||
$tasker->setUser($journal->user);
|
||||
$transactions = $tasker->getTransactionsOverview($journal);
|
||||
|
||||
return $this->collection($transactions, new TransactionTransformer, 'transaction');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform(TransactionJournal $journal): array
|
||||
{
|
||||
return [
|
||||
'id' => (int)$journal->id,
|
||||
'type' => $journal->transactionType->type,
|
||||
'description' => $journal->description,
|
||||
'date' => $journal->date->format('Y-m-d'),
|
||||
'order' => $journal->order,
|
||||
'completed' => $journal->completed,
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/transaction_journal/' . $journal->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
68
app/Transformers/TransactionTransformer.php
Normal file
68
app/Transformers/TransactionTransformer.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* TransactionTransformer.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 League\Fractal\TransformerAbstract;
|
||||
|
||||
/**
|
||||
* Class TransactionTransformer
|
||||
*/
|
||||
class TransactionTransformer extends TransformerAbstract
|
||||
{
|
||||
/**
|
||||
* @param array $original
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform(array $original): 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' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/transaction/' . $id,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
// todo source account, dest account, budget, category
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
}
|
54
app/Transformers/UserTransformer.php
Normal file
54
app/Transformers/UserTransformer.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
/**
|
||||
* UserTransformer.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\User;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
/**
|
||||
* Class UserTransformer
|
||||
*/
|
||||
class UserTransformer extends TransformerAbstract
|
||||
{
|
||||
/**
|
||||
* @param User $user
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform(User $user): array
|
||||
{
|
||||
|
||||
return [
|
||||
'id' => (int)$user->id,
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/user/' . $user->id,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user