Expand API.

This commit is contained in:
James Cole
2020-11-15 14:02:29 +01:00
parent 20e7948d99
commit eb616f36ea
6 changed files with 152 additions and 27 deletions

View File

@@ -0,0 +1,92 @@
<?php
/*
* IndexController.php
* Copyright (c) 2020 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Api\V1\Controllers\BudgetLimit;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\DateRequest;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Transformers\BudgetLimitTransformer;
use FireflyIII\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Pagination\LengthAwarePaginator;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection as FractalCollection;
/**
* Class IndexController
*/
class IndexController extends Controller
{
private BudgetLimitRepositoryInterface $blRepository;
private BudgetRepositoryInterface $repository;
/**
* IndexController constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
/** @var User $user */
$user = auth()->user();
$this->repository = app(BudgetRepositoryInterface::class);
$this->blRepository = app(BudgetLimitRepositoryInterface::class);
$this->repository->setUser($user);
$this->blRepository->setUser($user);
return $next($request);
}
);
}
/**
* Return all budget limits in a range.
*
* @return JsonResponse
*/
public function index(DateRequest $request): JsonResponse
{
$dates = $request->getAll();
$manager = $this->getManager();
$manager->parseIncludes('budget');
$budgetLimits = $this->blRepository->getAllBudgetLimits($dates['start'], $dates['end']);
$budgetLimits = $budgetLimits->slice(0, 5);
/** @var BudgetLimitTransformer $transformer */
$transformer = app(BudgetLimitTransformer::class);
$transformer->setParameters($this->parameters);
$paginator = new LengthAwarePaginator($budgetLimits, $budgetLimits->count(), 1000, $this->parameters->get('page'));
$paginator->setPath(route('api.v1.budget_limits.index') . $this->buildParams());
$resource = new FractalCollection($budgetLimits, $transformer, 'budget_limits');
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
}

View File

@@ -50,11 +50,8 @@ class BudgetLimitController extends Controller
{ {
use TransactionFilter; use TransactionFilter;
/** @var BudgetLimitRepositoryInterface */ private BudgetLimitRepositoryInterface $blRepository;
private $blRepository; private BudgetRepositoryInterface $repository;
/** @var BudgetRepositoryInterface The budget repository */
private $repository;
/** /**
@@ -105,9 +102,9 @@ class BudgetLimitController extends Controller
public function index(Request $request): JsonResponse public function index(Request $request): JsonResponse
{ {
$manager = $this->getManager(); $manager = $this->getManager();
$budgetId = (int) ($request->get('budget_id') ?? 0); $budgetId = (int)($request->get('budget_id') ?? 0);
$budget = $this->repository->findNull($budgetId); $budget = $this->repository->findNull($budgetId);
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
$this->parameters->set('budget_id', $budgetId); $this->parameters->set('budget_id', $budgetId);
$collection = new Collection; $collection = new Collection;
@@ -193,7 +190,7 @@ class BudgetLimitController extends Controller
*/ */
public function transactions(Request $request, BudgetLimit $budgetLimit): JsonResponse public function transactions(Request $request, BudgetLimit $budgetLimit): JsonResponse
{ {
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
$type = $request->get('type') ?? 'default'; $type = $request->get('type') ?? 'default';
$this->parameters->set('type', $type); $this->parameters->set('type', $type);
@@ -239,7 +236,7 @@ class BudgetLimitController extends Controller
* Update the specified resource in storage. * Update the specified resource in storage.
* *
* @param BudgetLimitUpdateRequest $request * @param BudgetLimitUpdateRequest $request
* @param BudgetLimit $budgetLimit * @param BudgetLimit $budgetLimit
* *
* @return JsonResponse * @return JsonResponse
*/ */

View File

@@ -43,6 +43,8 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @property string spent * @property string spent
* @property int $transaction_currency_id * @property int $transaction_currency_id
* @property TransactionCurrency $transactionCurrency * @property TransactionCurrency $transactionCurrency
* @property string $repeat_freq
* @property bool @auto_budget
* @method static Builder|BudgetLimit newModelQuery() * @method static Builder|BudgetLimit newModelQuery()
* @method static Builder|BudgetLimit newQuery() * @method static Builder|BudgetLimit newQuery()
* @method static Builder|BudgetLimit query() * @method static Builder|BudgetLimit query()
@@ -65,10 +67,11 @@ class BudgetLimit extends Model
*/ */
protected $casts protected $casts
= [ = [
'created_at' => 'datetime', 'created_at' => 'datetime',
'updated_at' => 'datetime', 'updated_at' => 'datetime',
'start_date' => 'date', 'start_date' => 'date',
'end_date' => 'date', 'end_date' => 'date',
'auto_budget' => 'boolean',
]; ];
/** @var array Fields that can be filled */ /** @var array Fields that can be filled */
@@ -79,13 +82,13 @@ class BudgetLimit extends Model
* *
* @param string $value * @param string $value
* *
* @throws NotFoundHttpException
* @return mixed * @return mixed
* @throws NotFoundHttpException
*/ */
public static function routeBinder(string $value): BudgetLimit public static function routeBinder(string $value): BudgetLimit
{ {
if (auth()->check()) { if (auth()->check()) {
$budgetLimitId = (int) $value; $budgetLimitId = (int)$value;
$budgetLimit = self::where('budget_limits.id', $budgetLimitId) $budgetLimit = self::where('budget_limits.id', $budgetLimitId)
->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id') ->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
->where('budgets.user_id', auth()->user()->id) ->where('budgets.user_id', auth()->user()->id)

View File

@@ -24,12 +24,31 @@ declare(strict_types=1);
namespace FireflyIII\Transformers; namespace FireflyIII\Transformers;
use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\BudgetLimit;
use League\Fractal\Resource\Item;
/** /**
* Class BudgetLimitTransformer * Class BudgetLimitTransformer
*/ */
class BudgetLimitTransformer extends AbstractTransformer class BudgetLimitTransformer extends AbstractTransformer
{ {
/** @var string[] */
protected $availableIncludes
= [
'budget',
];
/**
* Include Budget
*
* @param BudgetLimit $limit
*
* @return Item
*/
public function includeBudget(BudgetLimit $limit)
{
return $this->item($limit->budget, new BudgetTransformer,'budgets ');
}
/** /**
* Transform the note. * Transform the note.
* *
@@ -48,26 +67,29 @@ class BudgetLimitTransformer extends AbstractTransformer
$currencySymbol = null; $currencySymbol = null;
if (null !== $currency) { if (null !== $currency) {
$amount = $budgetLimit->amount; $amount = $budgetLimit->amount;
$currencyId = (int) $currency->id; $currencyId = (int)$currency->id;
$currencyName = $currency->name; $currencyName = $currency->name;
$currencyCode = $currency->code; $currencyCode = $currency->code;
$currencySymbol = $currency->symbol; $currencySymbol = $currency->symbol;
$currencyDecimalPlaces = $currency->decimal_places; $currencyDecimalPlaces = $currency->decimal_places;
} }
$amount = number_format((float) $amount, $currencyDecimalPlaces, '.', ''); $amount = number_format((float)$amount, $currencyDecimalPlaces, '.', '');
return [ return [
'id' => (int) $budgetLimit->id, 'id' => (int)$budgetLimit->id,
'created_at' => $budgetLimit->created_at->toAtomString(), 'created_at' => $budgetLimit->created_at->toAtomString(),
'updated_at' => $budgetLimit->updated_at->toAtomString(), 'updated_at' => $budgetLimit->updated_at->toAtomString(),
'start' => $budgetLimit->start_date->format('Y-m-d'), 'start' => $budgetLimit->start_date->format('Y-m-d'),
'end' => $budgetLimit->end_date->format('Y-m-d'), 'end' => $budgetLimit->end_date->format('Y-m-d'),
'budget_id' => (int) $budgetLimit->budget_id, 'budget_id' => (int)$budgetLimit->budget_id,
'currency_id' => $currencyId, 'currency_id' => $currencyId,
'currency_code' => $currencyCode, 'currency_code' => $currencyCode,
'currency_name' => $currencyName, 'currency_name' => $currencyName,
'currency_decimal_places' => $currencyName, 'currency_decimal_places' => $currencyName,
'currency_symbol' => $currencySymbol, 'currency_symbol' => $currencySymbol,
'amount' => $amount, 'amount' => $amount,
'repeat_freq' => $budgetLimit->repeat_freq,
'auto_budget' => $budgetLimit->auto_budget,
'links' => [ 'links' => [
[ [
'rel' => 'self', 'rel' => 'self',

View File

@@ -29,16 +29,15 @@ use FireflyIII\Models\Budget;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Symfony\Component\HttpFoundation\ParameterBag;
/** /**
* Class BudgetTransformer * Class BudgetTransformer
*/ */
class BudgetTransformer extends AbstractTransformer class BudgetTransformer extends AbstractTransformer
{ {
/** @var OperationsRepositoryInterface */ private OperationsRepositoryInterface $opsRepository;
private $opsRepository; private BudgetRepositoryInterface $repository;
/** @var BudgetRepositoryInterface */
private $repository;
/** /**
* BudgetTransformer constructor. * BudgetTransformer constructor.
@@ -49,6 +48,7 @@ class BudgetTransformer extends AbstractTransformer
{ {
$this->opsRepository = app(OperationsRepositoryInterface::class); $this->opsRepository = app(OperationsRepositoryInterface::class);
$this->repository = app(BudgetRepositoryInterface::class); $this->repository = app(BudgetRepositoryInterface::class);
$this->parameters = new ParameterBag();
} }
/** /**
@@ -66,7 +66,7 @@ class BudgetTransformer extends AbstractTransformer
$autoBudget = $this->repository->getAutoBudget($budget); $autoBudget = $this->repository->getAutoBudget($budget);
$spent = []; $spent = [];
if (null !== $start && null !== $end) { if (null !== $start && null !== $end) {
$spent = $this->beautify($this->opsRepository->sumExpenses($start, $end, null, new Collection([$budget]))); $spent = $this->beautify($this->opsRepository->sumExpenses($start, $end, null, new Collection([$budget])));
} }
$abCurrencyId = null; $abCurrencyId = null;
@@ -81,10 +81,10 @@ class BudgetTransformer extends AbstractTransformer
]; ];
if (null !== $autoBudget) { if (null !== $autoBudget) {
$abCurrencyId = (int) $autoBudget->transactionCurrency->id; $abCurrencyId = (int)$autoBudget->transactionCurrency->id;
$abCurrencyCode = $autoBudget->transactionCurrency->code; $abCurrencyCode = $autoBudget->transactionCurrency->code;
$abType = $types[$autoBudget->auto_budget_type]; $abType = $types[$autoBudget->auto_budget_type];
$abAmount = number_format((float) $autoBudget->amount, $autoBudget->transactionCurrency->decimal_places, '.', ''); $abAmount = number_format((float)$autoBudget->amount, $autoBudget->transactionCurrency->decimal_places, '.', '');
$abPeriod = $autoBudget->period; $abPeriod = $autoBudget->period;
} }
@@ -118,7 +118,7 @@ class BudgetTransformer extends AbstractTransformer
{ {
$return = []; $return = [];
foreach ($array as $data) { foreach ($array as $data) {
$data['sum'] = number_format((float) $data['sum'], (int) $data['currency_decimal_places'], '.', ''); $data['sum'] = number_format((float)$data['sum'], (int)$data['currency_decimal_places'], '.', '');
$return[] = $data; $return[] = $data;
} }

View File

@@ -175,6 +175,17 @@ Route::group(
} }
); );
Route::group(
['namespace' => 'FireflyIII\Api\V1\Controllers\BudgetLimit', 'prefix' => 'limits',
'as' => 'api.v1.limits.',],
static function () {
// Budget API routes:
Route::get('', ['uses' => 'IndexController@index', 'as' => 'index']);
}
);
Route::group( Route::group(
['namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'categories', ['namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'categories',
'as' => 'api.v1.categories.',], 'as' => 'api.v1.categories.',],