James Cole 2021-02-15 06:39:55 +01:00
parent e51f593a2d
commit e64b5dcf3a
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
8 changed files with 258 additions and 98 deletions

View File

@ -0,0 +1,68 @@
<?php
/*
* DestroyController.php
* Copyright (c) 2021 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\Controllers\Models\Budget;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Models\Budget;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Illuminate\Http\JsonResponse;
/**
* Class DestroyController
*/
class DestroyController extends Controller
{
private BudgetRepositoryInterface $repository;
/**
* DestroyController constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->repository = app(BudgetRepositoryInterface::class);
$this->repository->setUser(auth()->user());
return $next($request);
}
);
}
/**
* Remove the specified resource from storage.
*
* @param Budget $budget
*
* @return JsonResponse
* @codeCoverageIgnore
*/
public function destroy(Budget $budget): JsonResponse
{
$this->repository->destroy($budget);
return response()->json([], 204);
}
}

View File

@ -1,7 +1,7 @@
<?php
/**
* BudgetController.php
* Copyright (c) 2019 james@firefly-iii.org
/*
* ListController.php
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
@ -19,13 +19,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers\Models\Budget;
namespace FireflyIII\Api\V1\Controllers;
use FireflyIII\Api\V1\Requests\BudgetStoreRequest;
use FireflyIII\Api\V1\Requests\BudgetUpdateRequest;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\Budget;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
@ -43,22 +40,17 @@ use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item;
/**
* Class BudgetController.
/***
* Class ListController
*/
class BudgetController extends Controller
class ListController extends Controller
{
use TransactionFilter;
/** @var BudgetLimitRepositoryInterface */
private $blRepository;
/** @var BudgetRepositoryInterface The budget repository */
private $repository;
private BudgetLimitRepositoryInterface $blRepository;
private BudgetRepositoryInterface $repository;
/**
* BudgetController constructor.
* ListController constructor.
*
* @codeCoverageIgnore
*/
@ -67,13 +59,10 @@ class BudgetController extends Controller
parent::__construct();
$this->middleware(
function ($request, $next) {
/** @var User $admin */
$admin = auth()->user();
$this->repository = app(BudgetRepositoryInterface::class);
$this->blRepository = app(BudgetLimitRepositoryInterface::class);
$this->repository->setUser($admin);
$this->blRepository->setUser($admin);
$this->repository->setUser(auth()->user());
$this->blRepository->setUser(auth()->user());
return $next($request);
}
@ -139,21 +128,6 @@ class BudgetController extends Controller
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
/**
* Remove the specified resource from storage.
*
* @param Budget $budget
*
* @return JsonResponse
* @codeCoverageIgnore
*/
public function delete(Budget $budget): JsonResponse
{
$this->repository->destroy($budget);
return response()->json([], 204);
}
/**
* Display a listing of the resource.
*
@ -207,28 +181,7 @@ class BudgetController extends Controller
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
/**
* Store a budget.
*
* @param BudgetStoreRequest $request
*
* @return JsonResponse
* @throws FireflyException
*
*/
public function store(BudgetStoreRequest $request): JsonResponse
{
$budget = $this->repository->store($request->getAll());
$manager = $this->getManager();
/** @var BudgetTransformer $transformer */
$transformer = app(BudgetTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($budget, $transformer, 'budgets');
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
/**
* Show all transactions.
@ -294,28 +247,4 @@ class BudgetController extends Controller
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
/**
* Update a budget.
*
* @param BudgetUpdateRequest $request
* @param Budget $budget
*
* @return JsonResponse
*/
public function update(BudgetUpdateRequest $request, Budget $budget): JsonResponse
{
$data = $request->getAll();
$budget = $this->repository->update($budget, $data);
$manager = $this->getManager();
/** @var BudgetTransformer $transformer */
$transformer = app(BudgetTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($budget, $transformer, 'budgets');
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
}
}

View File

@ -0,0 +1,81 @@
<?php
/*
* StoreController.php
* Copyright (c) 2021 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\Controllers\Models\Budget;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\BudgetStoreRequest;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Transformers\BudgetTransformer;
use Illuminate\Http\JsonResponse;
use League\Fractal\Resource\Item;
/**
* Class StoreController
*/
class StoreController extends Controller
{
private BudgetRepositoryInterface $repository;
/**
* StoreController constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->repository = app(BudgetRepositoryInterface::class);
$this->repository->setUser(auth()->user());
return $next($request);
}
);
}
/**
* Store a budget.
*
* @param BudgetStoreRequest $request
*
* @return JsonResponse
* @throws FireflyException
*
*/
public function store(BudgetStoreRequest $request): JsonResponse
{
$budget = $this->repository->store($request->getAll());
$manager = $this->getManager();
/** @var BudgetTransformer $transformer */
$transformer = app(BudgetTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($budget, $transformer, 'budgets');
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
}

View File

@ -0,0 +1,80 @@
<?php
/*
* UpdateController.php
* Copyright (c) 2021 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\Controllers\Models\Budget;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\BudgetUpdateRequest;
use FireflyIII\Models\Budget;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Transformers\BudgetTransformer;
use Illuminate\Http\JsonResponse;
use League\Fractal\Resource\Item;
/**
* Class UpdateController
*/
class UpdateController extends Controller
{
private BudgetRepositoryInterface $repository;
/**
* UpdateController constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->repository = app(BudgetRepositoryInterface::class);
$this->repository->setUser(auth()->user());
return $next($request);
}
);
}
/**
* Update a budget.
*
* @param BudgetUpdateRequest $request
* @param Budget $budget
*
* @return JsonResponse
*/
public function update(BudgetUpdateRequest $request, Budget $budget): JsonResponse
{
$data = $request->getAll();
$budget = $this->repository->update($budget, $data);
$manager = $this->getManager();
/** @var BudgetTransformer $transformer */
$transformer = app(BudgetTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($budget, $transformer, 'budgets');
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
}

View File

@ -55,9 +55,11 @@ class BudgetStoreRequest extends FormRequest
'name' => $this->string('name'),
'active' => $active,
'order' => 0,
'auto_budget_type' => $this->string('auto_budget_type'),
'transaction_currency_id' => $this->integer('auto_budget_currency_id'),
'transaction_currency_code' => $this->string('auto_budget_currency_code'),
// auto budget info
'auto_budget_type' => $this->string('auto_budget_type'),
'auto_budget_amount' => $this->string('auto_budget_amount'),
'auto_budget_period' => $this->string('auto_budget_period'),
];
@ -73,15 +75,15 @@ class BudgetStoreRequest extends FormRequest
return [
'name' => 'required|between:1,100|uniqueObjectForUser:budgets,name',
'active' => [new IsBoolean],
'auto_budget_type' => 'in:reset,rollover,none',
'auto_budget_currency_id' => 'exists:transaction_currencies,id',
'auto_budget_currency_code' => 'exists:transaction_currencies,code',
// auto budget info
'auto_budget_type' => 'in:reset,rollover,none',
'auto_budget_amount' => 'min:0|max:1000000000',
'auto_budget_period' => 'in:daily,weekly,monthly,quarterly,half_year,yearly',
];
}
/**
* Configure the validator instance with special rules for after the basic validation rules.
*

View File

@ -52,7 +52,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @property int $size
* @property bool $uploaded
* @property-read Model|\Eloquent $attachable
* @property-read Collection|\FireflyIII\Models\Note[] $notes
* @property Collection|\FireflyIII\Models\Note[] $notes
* @property-read int|null $notes_count
* @property-read User $user
* @method static \Illuminate\Database\Eloquent\Builder|Attachment newModelQuery()

View File

@ -269,7 +269,7 @@ class BudgetRepository implements BudgetRepositoryInterface
// try to create associated auto budget:
$type = $data['auto_budget_type'] ?? 0;
if (0 === $type) {
if (0 === $type || '' === $type || 'none' === $type) {
return $newBudget;
}
if ('reset' === $type) {

View File

@ -162,16 +162,16 @@ Route::group(
static function () {
// Budget API routes:
Route::get('', ['uses' => 'BudgetController@index', 'as' => 'index']);
Route::post('', ['uses' => 'BudgetController@store', 'as' => 'store']);
Route::get('{budget}', ['uses' => 'BudgetController@show', 'as' => 'show']);
Route::put('{budget}', ['uses' => 'BudgetController@update', 'as' => 'update']);
Route::delete('{budget}', ['uses' => 'BudgetController@delete', 'as' => 'delete']);
Route::get('', ['uses' => 'Models\Budget\ListController@index', 'as' => 'index']);
Route::post('', ['uses' => 'Models\Budget\StoreController@store', 'as' => 'store']);
Route::get('{budget}', ['uses' => 'Models\Budget\ListController@show', 'as' => 'show']);
Route::put('{budget}', ['uses' => 'Models\Budget\UpdateController@update', 'as' => 'update']);
Route::delete('{budget}', ['uses' => 'Models\Budget\DestroyController@destroy', 'as' => 'delete']);
Route::post('{budget}/limits', ['uses' => 'BudgetLimitController@store', 'as' => 'store_budget_limit']);
Route::get('{budget}/transactions', ['uses' => 'BudgetController@transactions', 'as' => 'transactions']);
Route::get('{budget}/attachments', ['uses' => 'BudgetController@attachments', 'as' => 'attachments']);
Route::get('{budget}/limits', ['uses' => 'BudgetController@budgetLimits', 'as' => 'budget_limits']);
Route::get('{budget}/transactions', ['uses' => 'Models\Budget\ListController@transactions', 'as' => 'transactions']);
Route::get('{budget}/attachments', ['uses' => 'Models\Budget\ListController@attachments', 'as' => 'attachments']);
Route::get('{budget}/limits', ['uses' => 'Models\Budget\ListController@budgetLimits', 'as' => 'budget_limits']);
}
);