Update API for tags.

This commit is contained in:
James Cole 2018-12-07 07:41:12 +01:00
parent 01e789f5ce
commit 3e84f9664f
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
9 changed files with 101 additions and 113 deletions

View File

@ -100,7 +100,7 @@ class Controller extends BaseController
// some date fields:
$dates = ['start', 'end', 'date'];
foreach ($dates as $field) {
$date = request()->get($field);
$date = request()->query->get($field);
$obj = null;
if (null !== $date) {
try {

View File

@ -30,6 +30,7 @@ use FireflyIII\Models\Tag;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\Support\Http\Api\Transactions;
use FireflyIII\Transformers\TagTransformer;
use FireflyIII\Transformers\TransactionTransformer;
use FireflyIII\User;
@ -47,6 +48,8 @@ use League\Fractal\Resource\Collection as FractalCollection;
*/
class TagController extends Controller
{
use Transactions;
/** @var TagRepositoryInterface The tag repository */
private $repository;

View File

@ -35,6 +35,7 @@ use FireflyIII\Helpers\Filter\PositiveAmountFilter;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Support\Http\Api\Transactions;
use FireflyIII\Transformers\AttachmentTransformer;
use FireflyIII\Transformers\PiggyBankEventTransformer;
use FireflyIII\Transformers\TransactionTransformer;
@ -53,6 +54,7 @@ use League\Fractal\Serializer\JsonApiSerializer;
*/
class TransactionController extends Controller
{
use Transactions;
/** @var JournalRepositoryInterface The journal repository */
private $repository;
@ -299,41 +301,4 @@ class TransactionController extends Controller
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
/**
* All the types you can request.
*
* @param string $type
*
* @return array
*/
private function mapTypes(string $type): array
{
$types = [
'all' => [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER, TransactionType::OPENING_BALANCE,
TransactionType::RECONCILIATION,],
'withdrawal' => [TransactionType::WITHDRAWAL,],
'withdrawals' => [TransactionType::WITHDRAWAL,],
'expense' => [TransactionType::WITHDRAWAL,],
'expenses' => [TransactionType::WITHDRAWAL,],
'income' => [TransactionType::DEPOSIT,],
'deposit' => [TransactionType::DEPOSIT,],
'deposits' => [TransactionType::DEPOSIT,],
'transfer' => [TransactionType::TRANSFER,],
'transfers' => [TransactionType::TRANSFER,],
'opening_balance' => [TransactionType::OPENING_BALANCE,],
'reconciliation' => [TransactionType::RECONCILIATION,],
'reconciliations' => [TransactionType::RECONCILIATION,],
'special' => [TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,],
'specials' => [TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,],
'default' => [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER,],
];
$return = $types['default'];
if (isset($types[$type])) {
$return = $types[$type];
}
return $return;
}
}

View File

@ -57,7 +57,7 @@ class TagRequest extends Request
'description' => $this->string('description'),
'latitude' => '' === $this->string('latitude') ? null : $this->string('latitude'),
'longitude' => '' === $this->string('longitude') ? null : $this->string('longitude'),
'zoomLevel' => $this->integer('zoom_level'),
'zoom_level' => $this->integer('zoom_level'),
];
return $data;

View File

@ -34,6 +34,11 @@ use Log;
*/
class TagFactory
{
/** @var Collection */
private $tags;
/** @var User */
private $user;
/**
* Constructor.
*/
@ -44,11 +49,6 @@ class TagFactory
}
}
/** @var Collection */
private $tags;
/** @var User */
private $user;
/**
* @param array $data
*
@ -56,6 +56,10 @@ class TagFactory
*/
public function create(array $data): ?Tag
{
$zoomLevel = 0 === (int)$data['zoom_level'] ? null : (int)$data['zoom_level'];
$latitude = 0.0 === (float)$data['latitude'] ? null : (float)$data['latitude'];
$longitude = 0.0 === (float)$data['longitude'] ? null : (int)$data['longitude'];
return Tag::create(
[
'user_id' => $this->user->id,
@ -63,9 +67,9 @@ class TagFactory
'tagMode' => 'nothing',
'date' => $data['date'],
'description' => $data['description'],
'latitude' => $data['latitude'],
'longitude ' => $data['longitude'],
'zoomLevel' => $data['zoom_level'],
'latitude' => $latitude,
'longitude ' => $longitude,
'zoomLevel' => $zoomLevel,
]
);
}

View File

@ -22,6 +22,7 @@ declare(strict_types=1);
namespace FireflyIII\Models;
use Carbon\Carbon;
use Crypt;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Model;
@ -44,6 +45,8 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @property string description
* @property string amount_sum
* @property string tagMode
* @property Carbon created_at
* @property Carbon updated_at
*/
class Tag extends Model
{

View File

@ -24,6 +24,7 @@ namespace FireflyIII\Repositories\Tag;
use Carbon\Carbon;
use DB;
use FireflyIII\Factory\TagFactory;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
use FireflyIII\Helpers\Filter\InternalTransferFilter;
use FireflyIII\Models\Tag;
@ -251,18 +252,11 @@ class TagRepository implements TagRepositoryInterface
*/
public function store(array $data): Tag
{
$tag = new Tag;
$tag->tag = $data['tag'];
$tag->date = $data['date'];
$tag->description = $data['description'];
$tag->latitude = $data['latitude'];
$tag->longitude = $data['longitude'];
$tag->zoomLevel = $data['zoomLevel'];
$tag->tagMode = 'nothing';
$tag->user()->associate($this->user);
$tag->save();
/** @var TagFactory $factory */
$factory = new TagFactory;
$factory->setUser($this->user);
return $factory->create($data);
return $tag;
}
/**

View File

@ -0,0 +1,69 @@
<?php
/**
* Transactions.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\Support\Http\Api;
use FireflyIII\Models\TransactionType;
/**
* Trait Transactions
*/
trait Transactions
{
/**
* All the types you can request.
*
* @param string $type
*
* @return array
*/
protected function mapTypes(string $type): array
{
$types = [
'all' => [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER, TransactionType::OPENING_BALANCE,
TransactionType::RECONCILIATION,],
'withdrawal' => [TransactionType::WITHDRAWAL,],
'withdrawals' => [TransactionType::WITHDRAWAL,],
'expense' => [TransactionType::WITHDRAWAL,],
'expenses' => [TransactionType::WITHDRAWAL,],
'income' => [TransactionType::DEPOSIT,],
'deposit' => [TransactionType::DEPOSIT,],
'deposits' => [TransactionType::DEPOSIT,],
'transfer' => [TransactionType::TRANSFER,],
'transfers' => [TransactionType::TRANSFER,],
'opening_balance' => [TransactionType::OPENING_BALANCE,],
'reconciliation' => [TransactionType::RECONCILIATION,],
'reconciliations' => [TransactionType::RECONCILIATION,],
'special' => [TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,],
'specials' => [TransactionType::OPENING_BALANCE, TransactionType::RECONCILIATION,],
'default' => [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER,],
];
$return = $types['default'];
if (isset($types[$type])) {
$return = $types[$type];
}
return $return;
}
}

View File

@ -36,18 +36,6 @@ use Symfony\Component\HttpFoundation\ParameterBag;
*/
class TagTransformer extends TransformerAbstract
{
/**
* List of resources possible to include
*
* @var array
*/
protected $availableIncludes = ['user', 'transactions'];
/**
* List of resources to automatically include
*
* @var array
*/
protected $defaultIncludes = [];
/** @var ParameterBag */
protected $parameters;
@ -64,49 +52,11 @@ class TagTransformer extends TransformerAbstract
$this->parameters = $parameters;
}
/**
* Include any transactions.
*
* @param Tag $tag
*
* @codeCoverageIgnore
* @return FractalCollection
*/
public function includeTransactions(Tag $tag): FractalCollection
{
$pageSize = (int)app('preferences')->getForUser($tag->user, 'listPageSize', 50)->data;
// journals always use collector and limited using URL parameters.
$collector = app(TransactionCollectorInterface::class);
$collector->setUser($tag->user);
$collector->withOpposingAccount()->withCategoryInformation()->withCategoryInformation();
$collector->setAllAssetAccounts();
$collector->setTag($tag);
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->collection($transactions, new TransactionTransformer($this->parameters), 'transactions');
}
/**
* Include the user.
*
* @param Tag $tag
*
* @codeCoverageIgnore
* @return Item
*/
public function includeUser(Tag $tag): Item
{
return $this->item($tag->user, new UserTransformer($this->parameters), 'users');
}
/**
* Transform a tag.
*
* TODO add spent, earned, tranferred, etc.
*
* @param Tag $tag
*
* @return array
@ -121,9 +71,9 @@ class TagTransformer extends TransformerAbstract
'tag' => $tag->tag,
'date' => $date,
'description' => '' === $tag->description ? null : $tag->description,
'latitude' => (float)$tag->latitude,
'longitude' => (float)$tag->longitude,
'zoom_level' => (int)$tag->zoomLevel,
'latitude' => null === $tag->latitude ? null : (float)$tag->latitude,
'longitude' => null === $tag->longitude? null : (float)$tag->longitude,
'zoom_level' => null === $tag->zoomLevel ? null : (int)$tag->zoomLevel,
'links' => [
[
'rel' => 'self',