mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-12-25 08:21:08 -06:00
Complete preferences.
This commit is contained in:
parent
8c28c4b5ac
commit
e287e76db5
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Api\V1\Controllers;
|
||||
|
||||
use FireflyIII\Api\V1\Requests\PreferenceRequest;
|
||||
use FireflyIII\Models\Preference;
|
||||
use FireflyIII\Transformers\PreferenceTransformer;
|
||||
use FireflyIII\User;
|
||||
@ -109,7 +110,7 @@ class PreferenceController extends Controller
|
||||
/**
|
||||
* List single resource.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Request $request
|
||||
* @param Preference $preference
|
||||
*
|
||||
* @return JsonResponse
|
||||
@ -129,27 +130,42 @@ class PreferenceController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* Store new object.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param PreferenceRequest $request
|
||||
* @param Preference $preference
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function store(Request $request): JsonResponse
|
||||
public function update(PreferenceRequest $request, Preference $preference): JsonResponse
|
||||
{
|
||||
// todo replace code and replace request object.
|
||||
|
||||
}
|
||||
$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':
|
||||
case 'twoFactorAuthEnabled':
|
||||
$newValue = (int)$data['data'] === 1;
|
||||
break;
|
||||
}
|
||||
$result = Preferences::set($preference->name, $newValue);
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param string $object
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function update(Request $request, string $object): JsonResponse
|
||||
{
|
||||
// todo replace code and replace request object.
|
||||
// create some objects:
|
||||
$manager = new Manager;
|
||||
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||
|
||||
// present to user.
|
||||
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||
$resource = new Item($result, new PreferenceTransformer($this->parameters), 'preferences');
|
||||
|
||||
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||
|
||||
}
|
||||
}
|
57
app/Api/V1/Requests/PreferenceRequest.php
Normal file
57
app/Api/V1/Requests/PreferenceRequest.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/**
|
||||
* PreferenceRequest.php
|
||||
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Api\V1\Requests;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class PreferenceRequest
|
||||
*/
|
||||
class PreferenceRequest extends Request
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
// Only allow authenticated users
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
public function getAll(): array
|
||||
{
|
||||
return [
|
||||
'data' => $this->get('data'),
|
||||
];
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'data' => 'required|between:1,65000',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -189,9 +189,8 @@ Route::group(
|
||||
// Piggy Bank API routes:
|
||||
Route::get('', ['uses' => 'PreferenceController@index', 'as' => 'index']);
|
||||
Route::get('{preference}', ['uses' => 'PreferenceController@show', 'as' => 'show']);
|
||||
Route::put('{preference}', ['uses' => 'PreferenceController@update', 'as' => 'update']);
|
||||
// Route::post('', ['uses' => 'PiggyBankController@store', 'as' => 'store']);
|
||||
|
||||
// Route::put('{piggyBank}', ['uses' => 'PiggyBankController@update', 'as' => 'update']);
|
||||
// Route::delete('{piggyBank}', ['uses' => 'PiggyBankController@delete', 'as' => 'delete']);
|
||||
}
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user