Final patches in API

This commit is contained in:
James Cole 2021-03-07 12:16:03 +01:00
parent ba163f82d1
commit 414c99489c
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
5 changed files with 4 additions and 319 deletions

View File

@ -27,7 +27,7 @@ namespace FireflyIII\Api\V1\Controllers\Summary;
use Carbon\Carbon;
use Exception;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\DateRequest;
use FireflyIII\Api\V1\Requests\Data\DateRequest;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Helpers\Report\NetWorthInterface;
use FireflyIII\Models\Account;

View File

@ -1,101 +0,0 @@
<?php
/*
* CreateController.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/>.
*/
declare(strict_types=1);
/*
* CreateController.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/>.
*/
namespace FireflyIII\Api\V1\Controllers\Webhook;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\Webhook\CreateRequest;
use FireflyIII\Repositories\Webhook\WebhookRepositoryInterface;
use FireflyIII\Transformers\WebhookTransformer;
use FireflyIII\User;
use Illuminate\Http\JsonResponse;
use League\Fractal\Resource\Item;
/**
* Class CreateController
*/
class CreateController extends Controller
{
private WebhookRepositoryInterface $repository;
/**
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
/** @var User $admin */
$admin = auth()->user();
/** @var WebhookRepositoryInterface repository */
$this->repository = app(WebhookRepositoryInterface::class);
$this->repository->setUser($admin);
return $next($request);
}
);
}
/**
* @param CreateRequest $request
*
* @return JsonResponse
*/
public function store(CreateRequest $request): JsonResponse
{
$data = $request->getData();
$webhook = $this->repository->store($data);
$manager = $this->getManager();
/** @var WebhookTransformer $transformer */
$transformer = app(WebhookTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($webhook, $transformer, 'webhooks');
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
}

View File

@ -1,149 +0,0 @@
<?php
/*
* PreferenceController.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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers;
use FireflyIII\Api\V1\Requests\PreferenceRequest;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Preference;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Transformers\PreferenceTransformer;
use FireflyIII\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item;
/**
* Class PreferenceController
*/
class PreferenceController extends Controller
{
/**
* LinkTypeController constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
static function ($request, $next) {
/** @var User $user */
$user = auth()->user();
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($user);
// an important fallback is that the frontPageAccount array gets refilled automatically
// when it turns up empty.
$frontPageAccounts = app('preferences')->getForUser($user, 'frontPageAccounts', [])->data;
if (0 === count($frontPageAccounts)) {
/** @var Collection $accounts */
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
$accountIds = $accounts->pluck('id')->toArray();
app('preferences')->setForUser($user, 'frontPageAccounts', $accountIds);
}
return $next($request);
}
);
}
/**
* List all of them.
*
* @return JsonResponse
* @codeCoverageIgnore
*/
public function index(): JsonResponse
{
/** @var User $user */
$user = auth()->user();
$available = [
'language', 'customFiscalYear', 'fiscalYearStart', 'currencyPreference',
'transaction_journal_optional_fields', 'frontPageAccounts', 'viewRange',
'listPageSize',
];
$preferences = new Collection;
foreach ($available as $name) {
$pref = app('preferences')->getForUser($user, $name);
if (null !== $pref) {
$preferences->push($pref);
}
}
$manager = $this->getManager();
/** @var PreferenceTransformer $transformer */
$transformer = app(PreferenceTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($preferences, $transformer, 'preferences');
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
/**
* Update a preference.
*
* @param PreferenceRequest $request
* @param Preference $preference
*
* @return JsonResponse
*/
public function update(PreferenceRequest $request, Preference $preference): JsonResponse
{
$data = $request->getAll();
$newValue = $data['data'];
switch ($preference->name) {
default:
break;
case 'transaction_journal_optional_fields':
case 'frontPageAccounts':
$newValue = explode(',', $data['data']);
break;
case 'listPageSize':
$newValue = (int) $data['data'];
break;
case 'customFiscalYear':
$newValue = 1 === (int) $data['data'];
break;
}
$result = app('preferences')->set($preference->name, $newValue);
$manager = $this->getManager();
/** @var PreferenceTransformer $transformer */
$transformer = app(PreferenceTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($result, $transformer, 'preferences');
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
}

View File

@ -1,8 +1,8 @@
<?php
/**
/*
* DateRequest.php
* Copyright (c) 2019 james@firefly-iii.org
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
@ -22,7 +22,7 @@
declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
namespace FireflyIII\Api\V1\Requests\Data;
use FireflyIII\Support\Request\ChecksLogin;

View File

@ -1,65 +0,0 @@
<?php
/**
* PreferenceRequest.php
* Copyright (c) 2019 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);
namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class PreferenceRequest
*
* @codeCoverageIgnore
*/
class PreferenceRequest extends FormRequest
{
use ConvertsDataTypes, ChecksLogin;
/**
* Get all data from the request.
*
* @return array
*/
public function getAll(): array
{
return [
'data' => $this->get('data'),
];
}
/**
* The rules that the incoming request must be matched against.
*
* @return array
*/
public function rules(): array
{
return [
'data' => 'required|between:1,65000',
];
}
}