Expand API, add new migration.

This commit is contained in:
James Cole 2020-06-30 20:33:08 +02:00
parent 77e7af75dc
commit cb65999124
11 changed files with 203 additions and 4 deletions

View File

@ -241,7 +241,6 @@ class PiggyBankController extends Controller
$this->repository->setCurrentAmount($piggyBank, $data['current_amount']);
}
$manager = $this->getManager();
/** @var PiggyBankTransformer $transformer */
$transformer = app(PiggyBankTransformer::class);

View File

@ -63,6 +63,7 @@ class PiggyBankRequest extends Request
'startdate' => $this->date('start_date'),
'targetdate' => $this->date('target_date'),
'notes' => $this->nlString('notes'),
'order' => $this->integer('order'),
];
}

View File

@ -30,6 +30,7 @@ use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Bill;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\ObjectGroup\OrganisesObjectGroups;
use FireflyIII\Transformers\BillTransformer;
use Symfony\Component\HttpFoundation\ParameterBag;
@ -38,6 +39,7 @@ use Symfony\Component\HttpFoundation\ParameterBag;
*/
class IndexController extends Controller
{
use OrganisesObjectGroups;
private BillRepositoryInterface $repository;
/**
@ -65,6 +67,10 @@ class IndexController extends Controller
*/
public function index()
{
$this->cleanupObjectGroups();
$this->repository->correctOrder();
$start = session('start');
$end = session('end');
$collection = $this->repository->getBills();

View File

@ -190,7 +190,10 @@ class BillRepository implements BillRepositoryInterface
public function getBills(): Collection
{
/** @var Collection $set */
return $this->user->bills()->orderBy('active', 'DESC')->orderBy('name', 'ASC')->get();
return $this->user->bills()
->orderBy('order', 'ASC')
->orderBy('active', 'DESC')
->orderBy('name', 'ASC')->get();
}
/**
@ -711,4 +714,20 @@ class BillRepository implements BillRepositoryInterface
{
$this->user->transactionJournals()->where('bill_id', $bill->id)->update(['bill_id' => null]);
}
/**
* Correct order of piggies in case of issues.
*/
public function correctOrder(): void
{
$set = $this->user->bills()->orderBy('order', 'ASC')->get();
$current = 1;
foreach ($set as $bill) {
if ((int) $bill->order !== $current) {
$bill->order = $current;
$bill->save();
}
$current++;
}
}
}

View File

@ -40,6 +40,11 @@ interface BillRepositoryInterface
*/
public function unlinkAll(Bill $bill): void;
/**
* Add correct order to bills.
*/
public function correctOrder(): void;
/**
* @param Bill $bill
*

View File

@ -26,6 +26,7 @@ namespace FireflyIII\Repositories\PiggyBank;
use Carbon\Carbon;
use DB;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Note;
@ -34,6 +35,7 @@ use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\Models\PiggyBankRepetition;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\ObjectGroup\CreatesObjectGroups;
use FireflyIII\User;
use Illuminate\Database\QueryException;
use Log;
@ -351,6 +353,14 @@ trait ModifiesPiggyBanks
$this->updateNote($piggyBank, $data['notes'] ?? '');
// update the order of the piggy bank:
$oldOrder = (int) $piggyBank->order;
$newOrder = (int) ($data['order'] ?? $oldOrder);
if ($oldOrder !== $newOrder) {
$this->updateOrder($piggyBank, $oldOrder, $newOrder);
}
// if the piggy bank is now smaller than the current relevant rep,
// remove money from the rep.
$repetition = $this->getRepetition($piggyBank);
@ -415,4 +425,38 @@ trait ModifiesPiggyBanks
return true;
}
/**
* @param PiggyBank $piggyBank
* @param int $oldOrder
* @param int $newOrder
*/
private function updateOrder(PiggyBank $piggyBank, int $oldOrder, int $newOrder): void
{
if ($newOrder > $oldOrder) {
// Iedereen [7 en lager] [hoger dan 3] behalve piggy zelf, puntje er af:
//piggy zelf naar 7
/** @var User $user */
$user = $this->user;
$user->piggyBanks()->where('order', '<=', $newOrder)->where('order', '>', $oldOrder)
->where('piggy_banks.id', '!=', $piggyBank->id)
->update(['order' => DB::raw('piggy_banks.order-1')]);
$piggyBank->order = $newOrder;
$piggyBank->save();
}
if ($newOrder < $oldOrder) {
//
//Van 8 naar 2
// iedereen [2 of hoger] en [kleiner dan 8] puntje er bij.
// 8 naar 2
/** @var User $user */
$user = $this->user;
$user->piggyBanks()->where('order', '>=', $newOrder)->where('order', '<', $oldOrder)
->where('piggy_banks.id', '!=', $piggyBank->id)
->update(['order' => DB::raw('piggy_banks.order+1')]);
$piggyBank->order = $newOrder;
$piggyBank->save();
}
}
}

View File

@ -143,9 +143,9 @@ return [
],
//'encryption' => null === env('USE_ENCRYPTION') || true === env('USE_ENCRYPTION'),
'version' => '5.3.0-beta.1',
'version' => '5.3.0-beta.2',
'api_version' => '1.2.0',
'db_version' => 14,
'db_version' => 15,
'maxUploadSize' => 1073741824, // 1 GB
'send_error_message' => env('SEND_ERROR_MESSAGE', true),
'site_owner' => env('SITE_OWNER', ''),

View File

@ -1,5 +1,26 @@
<?php
/**
* 2020_03_13_201950_changes_for_v520.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/>.
*/
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;

View File

@ -1,4 +1,25 @@
<?php
/**
* 2020_06_07_063612_changes_for_v530.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/>.
*/
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
@ -41,6 +62,7 @@ class ChangesForV530 extends Migration
);
}
if (!Schema::hasTable('object_groupables')) {
Schema::create(
'object_groupables', static function (Blueprint $table) {

View File

@ -0,0 +1,61 @@
<?php
/**
* 2020_06_30_202620_changes_for_v530a.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/>.
*/
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* Class ChangesForV530a
*/
class ChangesForV530a extends Migration
{
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table(
'bills', static function (Blueprint $table) {
$table->dropColumn('order');
}
);
}
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table(
'bills', static function (Blueprint $table) {
$table->integer('order', false, true)->default(0);
}
);
}
}

View File

@ -1,5 +1,26 @@
<?php
/**
* ConfigSeeder.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/>.
*/
declare(strict_types=1);
/**