Skip to content

Firefly III: a personal finances manager

API for piggies + groups.

James Cole00440f282b19
diff --git a/app/Api/V1/Controllers/PiggyBankController.php b/app/Api/V1/Controllers/PiggyBankController.php
index ec61455628..185a37636f 100644
--- a/app/Api/V1/Controllers/PiggyBankController.php
+++ b/app/Api/V1/Controllers/PiggyBankController.php
@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers;
use FireflyIII\Api\V1\Requests\PiggyBankRequest;
+use FireflyIII\Api\V1\Requests\PiggyBankStoreRequest;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
@@ -204,12 +205,12 @@ class PiggyBankController extends Controller
/**
* Store new object.
*
- * @param PiggyBankRequest $request
+ * @param PiggyBankStoreRequest $request
*
* @throws FireflyException
* @return JsonResponse
*/
- public function store(PiggyBankRequest $request): JsonResponse
+ public function store(PiggyBankStoreRequest $request): JsonResponse
{
$piggyBank = $this->repository->store($request->getAll());
$manager = $this->getManager();
diff --git a/app/Api/V1/Requests/PiggyBankStoreRequest.php b/app/Api/V1/Requests/PiggyBankStoreRequest.php
new file mode 100644
index 0000000000..8d32203aa9
--- /dev/null
+++ b/app/Api/V1/Requests/PiggyBankStoreRequest.php
@@ -0,0 +1,86 @@
+<?php
+/**
+ * PiggyBankStoreRequest.php
+ * Copyright (c) 2019 [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\Api\V1\Requests;
+
+use FireflyIII\Rules\ZeroOrMore;
+
+/**
+ *
+ * Class PiggyBankStoreRequest
+ *
+ * @codeCoverageIgnore
+ */
+class PiggyBankStoreRequest extends Request
+{
+ /**
+ * Authorize logged in users.
+ *
+ * @return bool
+ */
+ public function authorize(): bool
+ {
+ // Only allow authenticated users
+ return auth()->check();
+ }
+
+ /**
+ * Get all data from the request.
+ *
+ * @return array
+ */
+ public function getAll(): array
+ {
+ return [
+ 'name' => $this->string('name'),
+ 'account_id' => $this->integer('account_id'),
+ 'targetamount' => $this->string('target_amount'),
+ 'current_amount' => $this->string('current_amount'),
+ 'startdate' => $this->date('start_date'),
+ 'targetdate' => $this->date('target_date'),
+ 'notes' => $this->nlString('notes'),
+ 'object_group_id' => $this->integer('object_group_id'),
+ 'object_group' => $this->string('object_group_name'),
+ ];
+ }
+
+ /**
+ * The rules that the incoming request must be matched against.
+ *
+ * @return array
+ */
+ public function rules(): array
+ {
+ return [
+ 'name' => 'required|between:1,255|uniquePiggyBankForUser',
+ 'current_amount' => ['numeric', new ZeroOrMore, 'lte:target_amount'],
+ 'account_id' => 'required|numeric|belongsToUser:accounts,id',
+ 'object_group_id' => 'numeric|belongsToUser:object_groups,id',
+ 'target_amount' => ['numeric', new ZeroOrMore, 'lte:target_amount', 'required'],
+ 'start_date' => 'date|nullable',
+ 'target_date' => 'date|nullable|after:start_date',
+ 'notes' => 'max:65000',
+ ];
+ }
+
+}
diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php
index 134efafa10..a54c40afba 100644
--- a/app/Http/Controllers/Controller.php
+++ b/app/Http/Controllers/Controller.php
@@ -55,7 +55,7 @@ class Controller extends BaseController
public function __construct()
{
// is site a demo site?
- $isDemoSite = app('fireflyconfig')->get('is_demo_site', config('firefly.configuration.is_demo_site',),)->data;
+ $isDemoSite = app('fireflyconfig')->get('is_demo_site', config('firefly.configuration.is_demo_site', false,),)->data;
app('view')->share('IS_DEMO_SITE', $isDemoSite,);
app('view')->share('DEMO_USERNAME', config('firefly.demo_username'));
app('view')->share('DEMO_PASSWORD', config('firefly.demo_password'));
diff --git a/app/Repositories/ObjectGroup/CreatesObjectGroups.php b/app/Repositories/ObjectGroup/CreatesObjectGroups.php
index 91f8ae2515..3bdc83bf1f 100644
--- a/app/Repositories/ObjectGroup/CreatesObjectGroups.php
+++ b/app/Repositories/ObjectGroup/CreatesObjectGroups.php
@@ -21,6 +21,15 @@ trait CreatesObjectGroups
return $this->user->objectGroups()->where('title', $title)->first();
}
+ /**
+ * @param int $groupId
+ *
+ * @return ObjectGroup|null
+ */
+ protected function findObjectGroupById(int $groupId): ?ObjectGroup
+ {
+ return $this->user->objectGroups()->where('id', $groupId)->first();
+ }
/**
* @param User $user
diff --git a/app/Repositories/PiggyBank/ModifiesPiggyBanks.php b/app/Repositories/PiggyBank/ModifiesPiggyBanks.php
index 21526df11d..4528dba729 100644
--- a/app/Repositories/PiggyBank/ModifiesPiggyBanks.php
+++ b/app/Repositories/PiggyBank/ModifiesPiggyBanks.php
@@ -298,14 +298,23 @@ trait ModifiesPiggyBanks
// repetition is auto created.
$repetition = $this->getRepetition($piggyBank);
- if (null !== $repetition && isset($data['current_amount'])) {
+ if (null !== $repetition && isset($data['current_amount']) && '' !== $data['current_amount']) {
$repetition->currentamount = $data['current_amount'];
$repetition->save();
}
$objectGroupTitle = $data['object_group'] ?? '';
if ('' !== $objectGroupTitle) {
- $objectGroup = $this->findOrCreateObjectGroup($this->user, $objectGroupTitle);
+ $objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle);
+ if (null !== $objectGroup) {
+ $piggyBank->objectGroups()->sync([$objectGroup->id]);
+ $piggyBank->save();
+ }
+ }
+ // try also with ID:
+ $objectGroupId = (int) ($data['object_group_id'] ?? 0);
+ if (0 !== $objectGroupId) {
+ $objectGroup = $this->findObjectGroupById($objectGroupId);
if (null !== $objectGroup) {
$piggyBank->objectGroups()->sync([$objectGroup->id]);
$piggyBank->save();
diff --git a/config/firefly.php b/config/firefly.php
index b96dd479ea..b265566943 100644
--- a/config/firefly.php
+++ b/config/firefly.php
@@ -142,8 +142,8 @@ return [
'telemetry' => true,
],
- 'encryption' => null === env('USE_ENCRYPTION') || true === env('USE_ENCRYPTION'),
- 'version' => '5.3.0',
+ //'encryption' => null === env('USE_ENCRYPTION') || true === env('USE_ENCRYPTION'),
+ 'version' => '5.3.0-alpha.1',
'api_version' => '1.2.0',
'db_version' => 14,
'maxUploadSize' => 15242880,