Complete preferences.

This commit is contained in:
James Cole 2018-06-29 12:28:29 +02:00
parent 8c28c4b5ac
commit e287e76db5
3 changed files with 90 additions and 18 deletions

View File

@ -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');
}
}

View 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',
];
}
}

View File

@ -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']);
}
);