mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-13 16:00:13 +00:00
Clean up some code, clean routes. [skip ci]
This commit is contained in:
@@ -28,6 +28,16 @@ use FireflyIII\Support\JsonApi\Concerns\UsergroupAware;
|
||||
use LaravelJsonApi\Contracts\Store\QueriesAll;
|
||||
use LaravelJsonApi\NonEloquent\AbstractRepository;
|
||||
|
||||
/**
|
||||
* Class AccountRepository
|
||||
*
|
||||
* The repository collects a single or many (account) objects from the database and returns them to the
|
||||
* account resource. The account resource links all account properties to the JSON properties.
|
||||
*
|
||||
* For the queryAll thing, a separate query is constructed that does the actual querying of the database.
|
||||
* This is necessary because the user can't just query all accounts (it would return other user's data)
|
||||
* and because we also need to collect all kinds of metadata, like the currency and user info.
|
||||
*/
|
||||
class AccountRepository extends AbstractRepository implements QueriesAll
|
||||
{
|
||||
use UsergroupAware;
|
||||
@@ -39,11 +49,13 @@ class AccountRepository extends AbstractRepository implements QueriesAll
|
||||
|
||||
public function find(string $resourceId): ?object
|
||||
{
|
||||
die('query single?');
|
||||
return Account::find((int) $resourceId);
|
||||
}
|
||||
|
||||
public function queryAll(): Capabilities\AccountQuery
|
||||
{
|
||||
die('query all?');
|
||||
return Capabilities\AccountQuery::make()
|
||||
->withUserGroup($this->userGroup)
|
||||
->withServer($this->server)
|
||||
|
@@ -10,6 +10,11 @@ use LaravelJsonApi\Core\Resources\JsonApiResource;
|
||||
|
||||
/**
|
||||
* @property Account $resource
|
||||
*
|
||||
* This class collects the resources attributes, the account in this case.
|
||||
* Generally speaking, each property here is directly related to a property on the account object itself.
|
||||
* However, many properties are collected from other sources, like the user or the currency.
|
||||
* As a result, the account repository is where it's at, which is where the collection takes place and is optimised.
|
||||
*/
|
||||
class AccountResource extends JsonApiResource
|
||||
{
|
||||
@@ -21,14 +26,14 @@ class AccountResource extends JsonApiResource
|
||||
public function attributes($request): iterable
|
||||
{
|
||||
return [
|
||||
'created_at' => $this->resource->created_at,
|
||||
'updated_at' => $this->resource->updated_at,
|
||||
'name' => $this->resource->name,
|
||||
'iban' => '' === $this->resource->iban ? null : $this->resource->iban,
|
||||
'active' => $this->resource->active,
|
||||
'last_activity' => $this->resource->last_activity,
|
||||
'type' => $this->resource->type,
|
||||
'account_role' => $this->resource->account_role,
|
||||
'created_at' => $this->resource->created_at,
|
||||
'updated_at' => $this->resource->updated_at,
|
||||
'name' => $this->resource->name,
|
||||
// 'iban' => '' === $this->resource->iban ? null : $this->resource->iban,
|
||||
// 'active' => $this->resource->active,
|
||||
// 'last_activity' => $this->resource->last_activity,
|
||||
// 'type' => $this->resource->type,
|
||||
// 'account_role' => $this->resource->account_role,
|
||||
|
||||
// 'virtual_balance' => $this->resource->virtual_balance,
|
||||
// 'native_balance' => $this->resource->native_balance,
|
||||
|
@@ -17,6 +17,13 @@ use LaravelJsonApi\Eloquent\Filters\WhereIdIn;
|
||||
use LaravelJsonApi\Eloquent\Pagination\PagePagination;
|
||||
use LaravelJsonApi\Eloquent\Schema;
|
||||
|
||||
/**
|
||||
* Class AccountSchema
|
||||
*
|
||||
* This is the schema of all fields that an account exposes to the world.
|
||||
* Fields do not have to have a relation to the actual model.
|
||||
* Fields mentioned here still need to be filled in by the AccountResource.
|
||||
*/
|
||||
class AccountSchema extends Schema
|
||||
{
|
||||
/**
|
||||
@@ -34,18 +41,19 @@ class AccountSchema extends Schema
|
||||
DateTime::make('created_at')->sortable()->readOnly(),
|
||||
DateTime::make('updated_at')->sortable()->readOnly(),
|
||||
Str::make('name')->sortable(),
|
||||
Str::make('account_type'),
|
||||
Str::make('virtual_balance'),
|
||||
Str::make('iban'),
|
||||
Boolean::make('active'),
|
||||
Number::make('order'),
|
||||
HasOne::make('user'),
|
||||
HasMany::make('account_balances'),
|
||||
// Str::make('account_type'),
|
||||
// Str::make('virtual_balance'),
|
||||
// Str::make('iban'),
|
||||
// Boolean::make('active'),
|
||||
// Number::make('order'),
|
||||
HasOne::make('user')->readOnly(),
|
||||
//HasMany::make('account_balances'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the resource filters.
|
||||
* Filters mentioned here can be used to filter the results.
|
||||
* TODO write down exactly how this works.
|
||||
*/
|
||||
public function filters(): array
|
||||
{
|
||||
|
@@ -33,7 +33,7 @@ use LaravelJsonApi\Contracts\Store\HasPagination;
|
||||
use LaravelJsonApi\NonEloquent\Capabilities\QueryAll;
|
||||
use LaravelJsonApi\NonEloquent\Concerns\PaginatesEnumerables;
|
||||
|
||||
class AccountQuery extends QueryAll implements HasPagination
|
||||
class AccountQuery implements HasPagination
|
||||
{
|
||||
use ExpandsQuery;
|
||||
use FiltersPagination;
|
||||
@@ -42,32 +42,44 @@ class AccountQuery extends QueryAll implements HasPagination
|
||||
use UsergroupAware;
|
||||
use ValidateSortParameters;
|
||||
|
||||
#[\Override]
|
||||
public function get(): iterable
|
||||
/**
|
||||
* This method returns all accounts, given a bunch of filters and sort fields, together with pagination.
|
||||
*/
|
||||
public function queryAll(): iterable
|
||||
{
|
||||
// collect filters
|
||||
$filters = $this->queryParameters->filter();
|
||||
// collect sort options
|
||||
$sort = $this->queryParameters->sortFields();
|
||||
// collect pagination based on the page
|
||||
$pagination = $this->filtersPagination($this->queryParameters->page());
|
||||
// check if we need all accounts, regardless of pagination
|
||||
// This is necessary when the user wants to sort on specific params.
|
||||
$needsAll = $this->validateParams('account', $sort);
|
||||
|
||||
// start the query
|
||||
$query = $this->userGroup->accounts();
|
||||
|
||||
// add pagination to the query, limiting the results.
|
||||
if (!$needsAll) {
|
||||
$query = $this->addPagination($query, $pagination);
|
||||
}
|
||||
|
||||
// add sort and filter parameters to the query.
|
||||
$query = $this->addSortParams($query, $sort);
|
||||
$query = $this->addFilterParams('account', $query, $filters);
|
||||
|
||||
// collect the result.
|
||||
$collection = $query->get(['accounts.*']);
|
||||
|
||||
// enrich data
|
||||
// enrich the collected data
|
||||
$enrichment = new AccountEnrichment();
|
||||
$collection = $enrichment->enrich($collection);
|
||||
|
||||
// add filters after the query
|
||||
// TODO add filters after the query, if there are filters that cannot be applied to the database but only
|
||||
// to the enriched results.
|
||||
|
||||
// add sort after the query
|
||||
// sort the data after the query, and return it right away.
|
||||
return $this->sortCollection($collection, $sort);
|
||||
// var_dump($filters->value('name'));
|
||||
// exit;
|
||||
}
|
||||
}
|
||||
|
@@ -9,6 +9,11 @@ use FireflyIII\JsonApi\V2\AccountBalances\AccountBalanceSchema;
|
||||
use FireflyIII\JsonApi\V2\Users\UserSchema;
|
||||
use LaravelJsonApi\Core\Server\Server as BaseServer;
|
||||
|
||||
/**
|
||||
* Class Server
|
||||
*
|
||||
* This class serves as a generic class for the v2 API "server".
|
||||
*/
|
||||
class Server extends BaseServer
|
||||
{
|
||||
/**
|
||||
@@ -32,7 +37,7 @@ class Server extends BaseServer
|
||||
return [
|
||||
AccountSchema::class,
|
||||
UserSchema::class,
|
||||
AccountBalanceSchema::class,
|
||||
//AccountBalanceSchema::class,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user