Clean up a variety of requests.

This commit is contained in:
James Cole
2025-11-02 14:39:34 +01:00
parent e9cf5111c9
commit 69b816d957
11 changed files with 133 additions and 76 deletions

View File

@@ -25,6 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers\Models\Account; namespace FireflyIII\Api\V1\Controllers\Models\Account;
use FireflyIII\Api\V1\Controllers\Controller; use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\Generic\PaginationDateRangeRequest;
use FireflyIII\Api\V1\Requests\PaginationRequest; use FireflyIII\Api\V1\Requests\PaginationRequest;
use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
@@ -110,7 +111,7 @@ class ListController extends Controller
// get list of piggy banks. Count it and split it. // get list of piggy banks. Count it and split it.
$collection = $this->repository->getPiggyBanks($account); $collection = $this->repository->getPiggyBanks($account);
$count = $collection->count(); $count = $collection->count();
$piggyBanks = $collection->slice(($page - 1) * $limit, $limit); $piggyBanks = $collection->slice($offset, $limit);
// enrich // enrich
/** @var User $admin */ /** @var User $admin */
@@ -136,16 +137,15 @@ class ListController extends Controller
/** /**
* Show all transaction groups related to the account. * Show all transaction groups related to the account.
*/ */
public function transactions(PaginationRequest $request, Account $account): JsonResponse public function transactions(PaginationDateRangeRequest $request, Account $account): JsonResponse
{ {
[ [
'limit' => $limit, 'limit' => $limit,
'offset' => $offset,
'page' => $page, 'page' => $page,
'start' => $start,
'end' => $end,
'types' => $types,
] = $request->attributes->all(); ] = $request->attributes->all();
$type = $request->get('type') ?? 'default';
$types = $this->mapTransactionTypes($type);
$manager = $this->getManager(); $manager = $this->getManager();
/** @var User $admin */ /** @var User $admin */
@@ -154,15 +154,12 @@ class ListController extends Controller
// use new group collector: // use new group collector:
/** @var GroupCollectorInterface $collector */ /** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class); $collector = app(GroupCollectorInterface::class);
$collector->setUser($admin)->setAccounts(new Collection()->push($account)) $collector->setUser($admin)->setAccounts(new Collection()->push($account))->withAPIInformation()->setLimit($limit)->setPage($page)->setTypes($types);
->withAPIInformation()->setLimit($limit)->setPage($page)->setTypes($types) if (null !== $start) {
; $collector->setStart($start);
if (null !== $this->parameters->get('start')) {
$collector->setStart($this->parameters->get('start'));
} }
if (null !== $this->parameters->get('end')) { if (null !== $end) {
$collector->setEnd($this->parameters->get('end')); $collector->setEnd($end);
} }
$paginator = $collector->getPaginatedGroups(); $paginator = $collector->getPaginatedGroups();

View File

@@ -81,7 +81,6 @@ class StoreController extends Controller
/** @var AccountTransformer $transformer */ /** @var AccountTransformer $transformer */
$transformer = app(AccountTransformer::class); $transformer = app(AccountTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($account, $transformer, self::RESOURCE_KEY); $resource = new Item($account, $transformer, self::RESOURCE_KEY);

View File

@@ -87,7 +87,6 @@ class UpdateController extends Controller
/** @var AccountTransformer $transformer */ /** @var AccountTransformer $transformer */
$transformer = app(AccountTransformer::class); $transformer = app(AccountTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($account, $transformer, self::RESOURCE_KEY); $resource = new Item($account, $transformer, self::RESOURCE_KEY);
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);

View File

@@ -26,6 +26,7 @@ namespace FireflyIII\Api\V1\Controllers\Models\Attachment;
use FireflyIII\Api\V1\Controllers\Controller; use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Middleware\ApiDemoUser; use FireflyIII\Api\V1\Middleware\ApiDemoUser;
use FireflyIII\Api\V1\Requests\PaginationRequest;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Attachment; use FireflyIII\Models\Attachment;
use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface; use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;
@@ -120,8 +121,14 @@ class ShowController extends Controller
* *
* Display a listing of the resource. * Display a listing of the resource.
*/ */
public function index(): JsonResponse public function index(PaginationRequest $request): JsonResponse
{ {
[
'limit' => $limit,
'offset' => $offset,
'page' => $page,
] = $request->attributes->all();
if (true === auth()->user()->hasRole('demo')) { if (true === auth()->user()->hasRole('demo')) {
Log::channel('audit')->warning(sprintf('Demo user tries to access attachment API in %s', __METHOD__)); Log::channel('audit')->warning(sprintf('Demo user tries to access attachment API in %s', __METHOD__));
@@ -130,21 +137,17 @@ class ShowController extends Controller
$manager = $this->getManager(); $manager = $this->getManager();
// types to get, page size:
$pageSize = $this->parameters->get('limit');
// get list of attachments. Count it and split it. // get list of attachments. Count it and split it.
$collection = $this->repository->get(); $collection = $this->repository->get();
$count = $collection->count(); $count = $collection->count();
$attachments = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); $attachments = $collection->slice($offset, $limit);
// make paginator: // make paginator:
$paginator = new LengthAwarePaginator($attachments, $count, $pageSize, $this->parameters->get('page')); $paginator = new LengthAwarePaginator($attachments, $count, $limit, $page);
$paginator->setPath(route('api.v1.attachments.index').$this->buildParams()); $paginator->setPath(route('api.v1.attachments.index').$this->buildParams());
/** @var AttachmentTransformer $transformer */ /** @var AttachmentTransformer $transformer */
$transformer = app(AttachmentTransformer::class); $transformer = app(AttachmentTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($attachments, $transformer, 'attachments'); $resource = new FractalCollection($attachments, $transformer, 'attachments');
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
@@ -169,7 +172,6 @@ class ShowController extends Controller
/** @var AttachmentTransformer $transformer */ /** @var AttachmentTransformer $transformer */
$transformer = app(AttachmentTransformer::class); $transformer = app(AttachmentTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($attachment, $transformer, 'attachments'); $resource = new Item($attachment, $transformer, 'attachments');

View File

@@ -87,7 +87,6 @@ class StoreController extends Controller
/** @var AttachmentTransformer $transformer */ /** @var AttachmentTransformer $transformer */
$transformer = app(AttachmentTransformer::class); $transformer = app(AttachmentTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($attachment, $transformer, 'attachments'); $resource = new Item($attachment, $transformer, 'attachments');

View File

@@ -81,7 +81,6 @@ class UpdateController extends Controller
/** @var AttachmentTransformer $transformer */ /** @var AttachmentTransformer $transformer */
$transformer = app(AttachmentTransformer::class); $transformer = app(AttachmentTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($attachment, $transformer, 'attachments'); $resource = new Item($attachment, $transformer, 'attachments');

View File

@@ -25,6 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers\Models\AvailableBudget; namespace FireflyIII\Api\V1\Controllers\Models\AvailableBudget;
use FireflyIII\Api\V1\Controllers\Controller; use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\Generic\PaginationDateRangeRequest;
use FireflyIII\Models\AvailableBudget; use FireflyIII\Models\AvailableBudget;
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface; use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
use FireflyIII\Support\JsonApi\Enrichments\AvailableBudgetEnrichment; use FireflyIII\Support\JsonApi\Enrichments\AvailableBudgetEnrichment;
@@ -67,19 +68,21 @@ class ShowController extends Controller
* *
* Display a listing of the resource. * Display a listing of the resource.
*/ */
public function index(): JsonResponse public function index(PaginationDateRangeRequest $request): JsonResponse
{ {
$manager = $this->getManager(); $manager = $this->getManager();
[
// types to get, page size: 'limit' => $limit,
$pageSize = $this->parameters->get('limit'); 'offset' => $offset,
$start = $this->parameters->get('start'); 'page' => $page,
$end = $this->parameters->get('end'); 'start' => $start,
'end' => $end,
] = $request->attributes->all();
// get list of available budgets. Count it and split it. // get list of available budgets. Count it and split it.
$collection = $this->abRepository->getAvailableBudgetsByDate($start, $end); $collection = $this->abRepository->getAvailableBudgetsByDate($start, $end);
$count = $collection->count(); $count = $collection->count();
$availableBudgets = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); $availableBudgets = $collection->slice($offset, $limit);
// enrich // enrich
/** @var User $admin */ /** @var User $admin */
@@ -89,12 +92,11 @@ class ShowController extends Controller
$availableBudgets = $enrichment->enrich($availableBudgets); $availableBudgets = $enrichment->enrich($availableBudgets);
// make paginator: // make paginator:
$paginator = new LengthAwarePaginator($availableBudgets, $count, $pageSize, $this->parameters->get('page')); $paginator = new LengthAwarePaginator($availableBudgets, $count, $limit, $page);
$paginator->setPath(route('api.v1.available-budgets.index').$this->buildParams()); $paginator->setPath(route('api.v1.available-budgets.index').$this->buildParams());
/** @var AvailableBudgetTransformer $transformer */ /** @var AvailableBudgetTransformer $transformer */
$transformer = app(AvailableBudgetTransformer::class); $transformer = app(AvailableBudgetTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($availableBudgets, $transformer, 'available_budgets'); $resource = new FractalCollection($availableBudgets, $transformer, 'available_budgets');
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
@@ -116,15 +118,12 @@ class ShowController extends Controller
/** @var AvailableBudgetTransformer $transformer */ /** @var AvailableBudgetTransformer $transformer */
$transformer = app(AvailableBudgetTransformer::class); $transformer = app(AvailableBudgetTransformer::class);
$transformer->setParameters($this->parameters);
// enrich // enrich
/** @var User $admin */ /** @var User $admin */
$admin = auth()->user(); $admin = auth()->user();
$enrichment = new AvailableBudgetEnrichment(); $enrichment = new AvailableBudgetEnrichment();
$enrichment->setUser($admin); $enrichment->setUser($admin);
// $enrichment->setStart($start);
// $enrichment->setEnd($end);
$availableBudget = $enrichment->enrichSingle($availableBudget); $availableBudget = $enrichment->enrichSingle($availableBudget);

View File

@@ -25,6 +25,8 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers\Models\Bill; namespace FireflyIII\Api\V1\Controllers\Models\Bill;
use FireflyIII\Api\V1\Controllers\Controller; use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\Generic\PaginationDateRangeRequest;
use FireflyIII\Api\V1\Requests\PaginationRequest;
use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\Bill; use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface;
@@ -71,22 +73,25 @@ class ListController extends Controller
* *
* Display a listing of the resource. * Display a listing of the resource.
*/ */
public function attachments(Bill $bill): JsonResponse public function attachments(PaginationRequest $request, Bill $bill): JsonResponse
{ {
[
'limit' => $limit,
'offset' => $offset,
'page' => $page,
] = $request->attributes->all();
$manager = $this->getManager(); $manager = $this->getManager();
$pageSize = $this->parameters->get('limit');
$collection = $this->repository->getAttachments($bill); $collection = $this->repository->getAttachments($bill);
$count = $collection->count(); $count = $collection->count();
$attachments = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); $attachments = $collection->slice($offset, $limit);
// make paginator: // make paginator:
$paginator = new LengthAwarePaginator($attachments, $count, $pageSize, $this->parameters->get('page')); $paginator = new LengthAwarePaginator($attachments, $count, $limit, $page);
$paginator->setPath(route('api.v1.bills.attachments', [$bill->id]).$this->buildParams()); $paginator->setPath(route('api.v1.bills.attachments', [$bill->id]).$this->buildParams());
/** @var AttachmentTransformer $transformer */ /** @var AttachmentTransformer $transformer */
$transformer = app(AttachmentTransformer::class); $transformer = app(AttachmentTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($attachments, $transformer, 'attachments'); $resource = new FractalCollection($attachments, $transformer, 'attachments');
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
@@ -100,25 +105,25 @@ class ListController extends Controller
* *
* List all of them. * List all of them.
*/ */
public function rules(Bill $bill): JsonResponse public function rules(PaginationRequest $request, Bill $bill): JsonResponse
{ {
[
'limit' => $limit,
'offset' => $offset,
'page' => $page,
] = $request->attributes->all();
$manager = $this->getManager(); $manager = $this->getManager();
// types to get, page size:
$pageSize = $this->parameters->get('limit');
// get list of budgets. Count it and split it.
$collection = $this->repository->getRulesForBill($bill); $collection = $this->repository->getRulesForBill($bill);
$count = $collection->count(); $count = $collection->count();
$rules = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); $rules = $collection->slice($offset, $limit);
// make paginator: // make paginator:
$paginator = new LengthAwarePaginator($rules, $count, $pageSize, $this->parameters->get('page')); $paginator = new LengthAwarePaginator($rules, $count, $limit, $page);
$paginator->setPath(route('api.v1.bills.rules', [$bill->id]).$this->buildParams()); $paginator->setPath(route('api.v1.bills.rules', [$bill->id]).$this->buildParams());
/** @var RuleTransformer $transformer */ /** @var RuleTransformer $transformer */
$transformer = app(RuleTransformer::class); $transformer = app(RuleTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($rules, $transformer, 'rules'); $resource = new FractalCollection($rules, $transformer, 'rules');
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
@@ -131,13 +136,16 @@ class ListController extends Controller
* *
* Show all transactions. * Show all transactions.
*/ */
public function transactions(Request $request, Bill $bill): JsonResponse public function transactions(PaginationDateRangeRequest $request, Bill $bill): JsonResponse
{ {
$pageSize = $this->parameters->get('limit'); [
$type = $request->get('type') ?? 'default'; 'limit' => $limit,
$this->parameters->set('type', $type); 'page' => $page,
'types' => $types,
'start' => $start,
'end' => $end,
] = $request->attributes->all();
$types = $this->mapTransactionTypes($this->parameters->get('type'));
$manager = $this->getManager(); $manager = $this->getManager();
/** @var User $admin */ /** @var User $admin */
@@ -153,18 +161,18 @@ class ListController extends Controller
// all info needed for the API: // all info needed for the API:
->withAPIInformation() ->withAPIInformation()
// set page size: // set page size:
->setLimit($pageSize) ->setLimit($limit)
// set page to retrieve // set page to retrieve
->setPage($this->parameters->get('page')) ->setPage($page)
// set types of transactions to return. // set types of transactions to return.
->setTypes($types) ->setTypes($types)
; ;
if (null !== $this->parameters->get('start')) { if (null !== $start) {
$collector->setStart($this->parameters->get('start')); $collector->setStart($start);
} }
if (null !== $this->parameters->get('end')) { if (null !== $end) {
$collector->setEnd($this->parameters->get('end')); $collector->setEnd($end);
} }
// get paginator. // get paginator.
@@ -178,7 +186,6 @@ class ListController extends Controller
/** @var TransactionGroupTransformer $transformer */ /** @var TransactionGroupTransformer $transformer */
$transformer = app(TransactionGroupTransformer::class); $transformer = app(TransactionGroupTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($transactions, $transformer, 'transactions'); $resource = new FractalCollection($transactions, $transformer, 'transactions');
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));

View File

@@ -25,6 +25,9 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers\Models\Bill; namespace FireflyIII\Api\V1\Controllers\Models\Bill;
use FireflyIII\Api\V1\Controllers\Controller; use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\DateRangeRequest;
use FireflyIII\Api\V1\Requests\Generic\PaginationDateRangeRequest;
use FireflyIII\Api\V1\Requests\PaginationRequest;
use FireflyIII\Models\Bill; use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Support\JsonApi\Enrichments\SubscriptionEnrichment; use FireflyIII\Support\JsonApi\Enrichments\SubscriptionEnrichment;
@@ -65,28 +68,34 @@ class ShowController extends Controller
* *
* Display a listing of the resource. * Display a listing of the resource.
*/ */
public function index(): JsonResponse public function index(PaginationDateRangeRequest $request): JsonResponse
{ {
[
'limit' => $limit,
'offset' => $offset,
'start' => $start,
'end' => $end,
'page' => $page,
] = $request->attributes->all();
$this->repository->correctOrder(); $this->repository->correctOrder();
$bills = $this->repository->getBills(); $bills = $this->repository->getBills();
$manager = $this->getManager(); $manager = $this->getManager();
$pageSize = $this->parameters->get('limit');
$count = $bills->count(); $count = $bills->count();
$bills = $bills->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); $bills = $bills->slice($offset, $limit);
$paginator = new LengthAwarePaginator($bills, $count, $pageSize, $this->parameters->get('page')); $paginator = new LengthAwarePaginator($bills, $count, $limit, $page);
// enrich // enrich
/** @var User $admin */ /** @var User $admin */
$admin = auth()->user(); $admin = auth()->user();
$enrichment = new SubscriptionEnrichment(); $enrichment = new SubscriptionEnrichment();
$enrichment->setUser($admin); $enrichment->setUser($admin);
$enrichment->setStart($this->parameters->get('start')); $enrichment->setStart($start);
$enrichment->setEnd($this->parameters->get('end')); $enrichment->setEnd($end);
$bills = $enrichment->enrich($bills); $bills = $enrichment->enrich($bills);
/** @var BillTransformer $transformer */ /** @var BillTransformer $transformer */
$transformer = app(BillTransformer::class); $transformer = app(BillTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($bills, $transformer, 'bills'); $resource = new FractalCollection($bills, $transformer, 'bills');
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
@@ -100,8 +109,13 @@ class ShowController extends Controller
* *
* Show the specified bill. * Show the specified bill.
*/ */
public function show(Bill $bill): JsonResponse public function show(DateRangeRequest $request, Bill $bill): JsonResponse
{ {
[
'start' => $start,
'end' => $end,
] = $request->attributes->all();
$manager = $this->getManager(); $manager = $this->getManager();
// enrich // enrich
@@ -109,13 +123,12 @@ class ShowController extends Controller
$admin = auth()->user(); $admin = auth()->user();
$enrichment = new SubscriptionEnrichment(); $enrichment = new SubscriptionEnrichment();
$enrichment->setUser($admin); $enrichment->setUser($admin);
$enrichment->setStart($this->parameters->get('start')); $enrichment->setStart($start);
$enrichment->setEnd($this->parameters->get('end')); $enrichment->setEnd($end);
$bill = $enrichment->enrichSingle($bill); $bill = $enrichment->enrichSingle($bill);
/** @var BillTransformer $transformer */ /** @var BillTransformer $transformer */
$transformer = app(BillTransformer::class); $transformer = app(BillTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($bill, $transformer, 'bills'); $resource = new Item($bill, $transformer, 'bills');

View File

@@ -0,0 +1,43 @@
<?php
/*
* PaginationDateRangeRequest.php
* Copyright (c) 2025 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\Requests\Generic;
use FireflyIII\Api\V1\Requests\AggregateFormRequest;
use FireflyIII\Api\V1\Requests\DateRangeRequest;
use FireflyIII\Api\V1\Requests\PaginationRequest;
use FireflyIII\Models\Transaction;
/**
* TODO this class includes an object type filter which should be moved to its own thing.
*/
class PaginationDateRangeRequest extends AggregateFormRequest
{
#[Override]
protected function getRequests(): array
{
return [
DateRangeRequest::class,
[ObjectTypeApiRequest::class, 'object_type' => Transaction::class],
[PaginationRequest::class, 'sort_class' => Transaction::class],
];
}
}

View File

@@ -39,7 +39,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* *
* @property TransactionCurrency $transactionCurrency * @property TransactionCurrency $transactionCurrency
* @property Carbon $start_date * @property Carbon $start_date
* @property Carbon $end_date * @property Carbon|null $end_date
*/ */
#[ObservedBy([BudgetLimitObserver::class])] #[ObservedBy([BudgetLimitObserver::class])]
class BudgetLimit extends Model class BudgetLimit extends Model