mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-01-03 20:57:19 -06:00
Add ability to return single preference to the API
This commit is contained in:
parent
b52959d41a
commit
a55dd97d51
@ -24,7 +24,9 @@ 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;
|
||||
@ -41,6 +43,34 @@ use League\Fractal\Serializer\JsonApiSerializer;
|
||||
*/
|
||||
class PreferenceController extends Controller
|
||||
{
|
||||
/**
|
||||
* LinkTypeController constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->middleware(
|
||||
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 (\count($frontPageAccounts) === 0) {
|
||||
/** @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.
|
||||
*
|
||||
@ -51,12 +81,13 @@ class PreferenceController extends Controller
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = auth()->user();
|
||||
$available = [
|
||||
$user = auth()->user();
|
||||
$available = [
|
||||
'language', 'customFiscalYear', 'fiscalYearStart', 'currencyPreference',
|
||||
'transaction_journal_optional_fields', 'frontPageAccounts', 'viewRange',
|
||||
'listPageSize, twoFactorAuthEnabled',
|
||||
];
|
||||
|
||||
$preferences = new Collection;
|
||||
foreach ($available as $name) {
|
||||
$pref = app('preferences')->getForUser($user, $name);
|
||||
@ -83,6 +114,32 @@ class PreferenceController extends Controller
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a single preference by name.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Preference $preference
|
||||
*
|
||||
* @return JsonResponse
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
public function show(Request $request, Preference $preference): JsonResponse
|
||||
{
|
||||
// create some objects:
|
||||
$manager = new Manager;
|
||||
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||
|
||||
// present to user.
|
||||
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||
/** @var PreferenceTransformer $transformer */
|
||||
$transformer = app(PreferenceTransformer::class);
|
||||
$transformer->setParameters($this->parameters);
|
||||
|
||||
$resource = new Item($preference, $transformer, 'preferences');
|
||||
|
||||
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a preference.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user