Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
add account balances with some random data
James Coled356d39d43b6
diff --git a/app/Entities/AccountBalance.php b/app/Entities/AccountBalance.phpnew file mode 100644index 0000000000..3d63e600a1--- /dev/null+++ b/app/Entities/AccountBalance.php@@ -0,0 +1,49 @@+<?php+/*+ * AccountBalance.php+ * Copyright (c) 2024 [email protected].+ *+ * 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/.+ */++declare(strict_types=1);++namespace FireflyIII\Entities;++use FireflyIII\Models\Account;++class AccountBalance+{+ public string $id;+ public string $amount;+ public string $currencyId;++ public static function fromArray(): self+ {+ $balance = new self;+ $balance->id = (string) random_int(1, 1000);+ $balance->name = (string) random_int(1, 1000);+ $balance->amount = (string) random_int(1, 1000);+ $balance->currencyId = '1';++ return $balance;+ }++ public function getAccount():Account {+ return Account::inRandomOrder()->first();+ }++}diff --git a/app/Http/Controllers/Api/V3/Controllers/AccountController.php b/app/Http/Controllers/Api/V3/Controllers/AccountController.phpnew file mode 100644index 0000000000..fd5df19545--- /dev/null+++ b/app/Http/Controllers/Api/V3/Controllers/AccountController.php@@ -0,0 +1,41 @@+<?php++namespace FireflyIII\Http\Controllers\Api\V3\Controllers;++use FireflyIII\Http\Controllers\Controller;+use FireflyIII\JsonApi\V3\AccountBalances\AccountBalanceSchema;+use FireflyIII\Models\Account;+use Illuminate\Contracts\Support\Responsable;+use LaravelJsonApi\Core\Facades\JsonApi;+use LaravelJsonApi\Core\Responses\DataResponse;+use LaravelJsonApi\Laravel\Http\Controllers\Actions;+use LaravelJsonApi\Laravel\Http\Requests\AnonymousQuery;++class AccountController extends Controller+{++ use Actions\FetchMany;+ use Actions\FetchOne;+ use Actions\Store;+ use Actions\Update;+ use Actions\Destroy;+ use Actions\FetchRelated;+ use Actions\FetchRelationship;+ use Actions\UpdateRelationship;+ use Actions\AttachRelationship;+ use Actions\DetachRelationship;++ public function readAccountBalances(AnonymousQuery $query, AccountBalanceSchema $schema, Account $account): Responsable+ {+ $schema = JsonApi::server()->schemas()->schemaFor('account-balances');++ $models = $schema+ ->repository()+ ->queryAll()+ ->withRequest($query)+ ->withAccount($account)+ ->get();++ return DataResponse::make($models);+ }+}diff --git a/app/JsonApi/V3/AccountBalances/AccountBalanceRepository.php b/app/JsonApi/V3/AccountBalances/AccountBalanceRepository.phpnew file mode 100644index 0000000000..fba9fe46b2--- /dev/null+++ b/app/JsonApi/V3/AccountBalances/AccountBalanceRepository.php@@ -0,0 +1,51 @@+<?php+/*+ * AccountBalanceRepository.php+ * Copyright (c) 2024 [email protected].+ *+ * 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/.+ */++declare(strict_types=1);++namespace FireflyIII\JsonApi\V3\AccountBalances;++use FireflyIII\Entities\AccountBalance;+use LaravelJsonApi\Contracts\Store\QueriesAll;+use LaravelJsonApi\NonEloquent\AbstractRepository;++class AccountBalanceRepository extends AbstractRepository implements QueriesAll+{++ /**+ * @inheritDoc+ */+ #[\Override] public function find(string $resourceId): ?object+ {+ return AccountBalance::fromArray();+ }+++ /**+ * @inheritDoc+ */+ public function queryAll(): Capabilities\AccountBalanceQuery+ {+ return Capabilities\AccountBalanceQuery::make()+ ->withServer($this->server)+ ->withSchema($this->schema);+ }+}diff --git a/app/JsonApi/V3/AccountBalances/AccountBalanceResource.php b/app/JsonApi/V3/AccountBalances/AccountBalanceResource.phpnew file mode 100644index 0000000000..cbb453f544--- /dev/null+++ b/app/JsonApi/V3/AccountBalances/AccountBalanceResource.php@@ -0,0 +1,49 @@+<?php++namespace FireflyIII\JsonApi\V3\AccountBalances;++use Illuminate\Http\Request;+use LaravelJsonApi\Core\Resources\JsonApiResource;++class AccountBalanceResource extends JsonApiResource+{+ /**+ * Get the resource id.+ *+ * @return string+ */+ public function id(): string+ {+ return $this->resource->id;+ }++ /**+ * Get the resource's attributes.+ *+ * @param Request|null $request+ *+ * @return iterable+ */+ public function attributes($request): iterable+ {+ return [+ 'name' => $this->resource->amount,+ 'amount' => $this->resource->amount,+ ];+ }++ /**+ * Get the resource's relationships.+ *+ * @param Request|null $request+ *+ * @return iterable+ */+ public function relationships($request): iterable+ {+ return [+ $this->relation('account')->withData($this->resource->getAccount()),+ ];+ }++}diff --git a/app/JsonApi/V3/AccountBalances/AccountBalanceSchema.php b/app/JsonApi/V3/AccountBalances/AccountBalanceSchema.phpnew file mode 100644index 0000000000..3ae5cbebd3--- /dev/null+++ b/app/JsonApi/V3/AccountBalances/AccountBalanceSchema.php@@ -0,0 +1,54 @@+<?php++namespace FireflyIII\JsonApi\V3\AccountBalances;++use FireflyIII\Entities\AccountBalance;+use LaravelJsonApi\Core\Schema\Schema;+use LaravelJsonApi\Eloquent\Fields\Relations\HasOne;+use LaravelJsonApi\NonEloquent\Fields\Attribute;+use LaravelJsonApi\NonEloquent\Fields\ID;++class AccountBalanceSchema extends Schema+{++ /**+ * The model the schema corresponds to.+ *+ * @var string+ */+ public static string $model = AccountBalance::class;++ /**+ * Get the resource fields.+ *+ * @return array+ */+ public function fields(): array+ {+ return [+ ID::make(),+ Attribute::make('name'),+ Attribute::make('amount'),+ HasOne::make('account'),+ ];+ }++ /**+ * Get the resource filters.+ *+ * @return array+ */+ public function filters(): array+ {+ return [+ // Filter::make('id'),+ ];+ }++ public function repository(): AccountBalanceRepository+ {+ return AccountBalanceRepository::make()+ ->withServer($this->server)+ ->withSchema($this);+ }+}diff --git a/app/JsonApi/V3/AccountBalances/Capabilities/AccountBalanceQuery.php b/app/JsonApi/V3/AccountBalances/Capabilities/AccountBalanceQuery.phpnew file mode 100644index 0000000000..dd178675ab--- /dev/null+++ b/app/JsonApi/V3/AccountBalances/Capabilities/AccountBalanceQuery.php@@ -0,0 +1,61 @@+<?php+/*+ * AccountBalanceQuery.php+ * Copyright (c) 2024 [email protected].+ *+ * 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/.+ */++declare(strict_types=1);++namespace FireflyIII\JsonApi\V3\AccountBalances\Capabilities;++use FireflyIII\Entities\AccountBalance;+use FireflyIII\Models\Account;+use LaravelJsonApi\NonEloquent\Capabilities\QueryAll;++class AccountBalanceQuery extends QueryAll+{+ private Account $account;++ /**+ * QuerySites constructor.+ */+ public function __construct()+ {+ parent::__construct();+ }++ /**+ * @inheritDoc+ */+ public function get(): iterable+ {+ return [+ AccountBalance::fromArray(),+ AccountBalance::fromArray(),+ AccountBalance::fromArray(),+ AccountBalance::fromArray(),+ ];+ }++ public function withAccount(Account $account): self+ {+ $this->account = $account;+ return $this;+ }++}diff --git a/app/JsonApi/V3/Accounts/AccountResource.php b/app/JsonApi/V3/Accounts/AccountResource.phpindex 51cd94db43..4f00e1d594 100644--- a/app/JsonApi/V3/Accounts/AccountResource.php+++ b/app/JsonApi/V3/Accounts/AccountResource.php@@ -133,7 +133,7 @@ class AccountResource extends JsonApiResource{return [$this->relation('user')->withData($this->resource->user),- $this->relation('balances')->withData($this->resource->balances),+ $this->relation('account_balances')->withData($this->resource->balances),];}diff --git a/app/JsonApi/V3/Accounts/AccountSchema.php b/app/JsonApi/V3/Accounts/AccountSchema.phpindex 531959d9e4..6a6a1d3fed 100644--- a/app/JsonApi/V3/Accounts/AccountSchema.php+++ b/app/JsonApi/V3/Accounts/AccountSchema.php@@ -43,6 +43,7 @@ class AccountSchema extends SchemaBoolean::make('active'),Number::make('order'),HasOne::make('user'),+ HasMany::make('account_balances'),];}diff --git a/app/JsonApi/V3/Server.php b/app/JsonApi/V3/Server.phpindex b94c51d0cd..5eade8d9e0 100644--- a/app/JsonApi/V3/Server.php+++ b/app/JsonApi/V3/Server.php@@ -3,7 +3,7 @@namespace FireflyIII\JsonApi\V3;use FireflyIII\JsonApi\V3\Accounts\AccountSchema;-use FireflyIII\JsonApi\V3\Balances\BalanceSchema;+use FireflyIII\JsonApi\V3\AccountBalances\AccountBalanceSchema;use FireflyIII\JsonApi\V3\Users\UserSchema;use LaravelJsonApi\Core\Server\Server as BaseServer;@@ -37,7 +37,7 @@ class Server extends BaseServerreturn [AccountSchema::class,UserSchema::class,- BalanceSchema::class,+ AccountBalanceSchema::class,];}diff --git a/app/JsonApi/V3/Users/UserResource.php b/app/JsonApi/V3/Users/UserResource.phpindex 0a6d1be766..2293a42803 100644--- a/app/JsonApi/V3/Users/UserResource.php+++ b/app/JsonApi/V3/Users/UserResource.php@@ -23,6 +23,7 @@ class UserResource extends JsonApiResourcereturn ['created_at' => $this->resource->created_at,'updated_at' => $this->resource->updated_at,+ 'email' => $this->resource->email,];}diff --git a/app/Policies/AccountBalancePolicy.php b/app/Policies/AccountBalancePolicy.phpnew file mode 100644index 0000000000..688b4fd189--- /dev/null+++ b/app/Policies/AccountBalancePolicy.php@@ -0,0 +1,59 @@+<?php+/*+ * AccountPolicy.php+ * Copyright (c) 2024 [email protected].+ *+ * 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/.+ */++declare(strict_types=1);++namespace FireflyIII\Policies;++use FireflyIII\Entities\AccountBalance;+use FireflyIII\Models\Account;+use FireflyIII\User;++class AccountBalancePolicy+{++ /**+ * TODO needs better authentication.+ *+ * @param User $user+ * @param Account $account+ *+ * @return bool+ */+ public function view(User $user, AccountBalance $accountBalance): bool+ {+ return true;+ }++++ /**+ * Everybody can do this, but selection should limit to user.+ *+ * @return true+ */+ public function viewAny(): bool+ {+ return true;+ }+++}diff --git a/app/Policies/AccountPolicy.php b/app/Policies/AccountPolicy.phpindex 3f7b05fa2f..78b39649bc 100644--- a/app/Policies/AccountPolicy.php+++ b/app/Policies/AccountPolicy.php@@ -43,6 +43,8 @@ class AccountPolicyreturn auth()->check() && $user->id === $account->user_id;}++/*** Everybody can do this, but selection should limit to user.*@@ -64,7 +66,7 @@ class AccountPolicyreturn $this->view($user, $account);}- public function viewBalances(User $user, Account $account): bool+ public function viewAccountBalances(User $user, Account $account): bool{return $this->view($user, $account);}diff --git a/app/Policies/UserPolicy.php b/app/Policies/UserPolicy.phpindex 2d043d77c9..58135f6496 100644--- a/app/Policies/UserPolicy.php+++ b/app/Policies/UserPolicy.php@@ -36,7 +36,7 @@ class UserPolicy** @return bool*/- public function view(User $user, Account $account): bool+ public function view(User $user, User $user1): bool{return true;return auth()->check() && $user->id === $account->user_id;@@ -52,4 +52,9 @@ class UserPolicyreturn true;return auth()->check();}+ public function viewAccounts(User $user): bool+ {+ return true;+ return auth()->check();+ }}diff --git a/routes/api.php b/routes/api.phpindex 1cdb0c9f96..f4bbcc9fe4 100644--- a/routes/api.php+++ b/routes/api.php@@ -22,8 +22,10 @@declare(strict_types=1);+use FireflyIII\Http\Controllers\Api\V3\Controllers\AccountController;use LaravelJsonApi\Laravel\Facades\JsonApiRoute;use LaravelJsonApi\Laravel\Http\Controllers\JsonApiController;+use LaravelJsonApi\Laravel\Routing\ActionRegistrar;use LaravelJsonApi\Laravel\Routing\Relationships;use LaravelJsonApi\Laravel\Routing\ResourceRegistrar;@@ -31,11 +33,17 @@ use LaravelJsonApi\Laravel\Routing\ResourceRegistrar;JsonApiRoute::server('v3')->prefix('v3')->resources(function (ResourceRegistrar $server) {- $server->resource('accounts', JsonApiController::class)->readOnly()->relationships(function (Relationships $relations) {+ $server->resource('accounts', AccountController::class)->readOnly()->relationships(function (Relationships $relations) {$relations->hasOne('user')->readOnly();- $relations->hasMany('balances')->readOnly();+ //$relations->hasMany('account_balances')->readOnly();+ })+ ->actions(function (ActionRegistrar $actions) {+ $actions->withId()->get('account-balances', 'readAccountBalances'); // non-eloquent pseudo relation+ });+ $server->resource('users', JsonApiController::class)->readOnly()->relationships(function (Relationships $relations) {+ $relations->hasMany('accounts')->readOnly();});- $server->resource('users', JsonApiController::class);+ $server->resource('account-balances', JsonApiController::class);});