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;
/** /**

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()
@@ -69,6 +71,7 @@ class BudgetLimit extends Model
'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,8 +82,8 @@ 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
{ {

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.
* *
@@ -55,6 +74,7 @@ class BudgetLimitTransformer extends AbstractTransformer
$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(),
@@ -68,6 +88,8 @@ class BudgetLimitTransformer extends AbstractTransformer
'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();
} }
/** /**

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.',],