Files
firefly-iii/app/Api/V1/Controllers/User/PreferencesController.php

180 lines
6.8 KiB
PHP
Raw Normal View History

2021-03-07 09:46:12 +01:00
<?php
/*
* PreferencesController.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\User;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\User\PreferenceStoreRequest;
use FireflyIII\Api\V1\Requests\User\PreferenceUpdateRequest;
2021-07-10 07:29:39 +02:00
use FireflyIII\Exceptions\FireflyException;
2021-03-07 09:46:12 +01:00
use FireflyIII\Models\Preference;
use FireflyIII\Transformers\PreferenceTransformer;
use Illuminate\Http\JsonResponse;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
2021-03-07 09:46:12 +01:00
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item;
/**
* Class PreferencesController
*/
class PreferencesController extends Controller
{
2023-12-02 12:56:48 +01:00
public const string DATE_FORMAT = 'Y-m-d';
public const string RESOURCE_KEY = 'preferences';
2021-03-07 09:46:12 +01:00
/**
2021-09-19 17:22:16 +02:00
* This endpoint is documented at:
2023-02-12 06:53:36 +01:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/preferences/listPreference
2021-09-19 17:22:16 +02:00
*
2021-03-07 09:46:12 +01:00
* List all of them.
*
2021-07-10 07:29:39 +02:00
* @throws FireflyException
2021-03-07 09:46:12 +01:00
*/
public function index(): JsonResponse
{
2021-07-10 07:29:39 +02:00
$collection = app('preferences')->all();
2021-03-07 09:46:12 +01:00
$manager = $this->getManager();
$count = $collection->count();
2023-10-05 18:52:01 +02:00
$pageSize = $this->parameters->get('limit');
2021-03-07 09:46:12 +01:00
$preferences = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
// make paginator:
$paginator = new LengthAwarePaginator($preferences, $count, $pageSize, $this->parameters->get('page'));
2023-12-20 19:35:52 +01:00
$paginator->setPath(route('api.v1.preferences.index').$this->buildParams());
2021-03-07 09:46:12 +01:00
/** @var PreferenceTransformer $transformer */
$transformer = app(PreferenceTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($preferences, $transformer, self::RESOURCE_KEY);
2021-03-07 09:46:12 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
/**
2021-09-19 17:22:16 +02:00
* This endpoint is documented at:
2023-02-12 06:53:36 +01:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/preferences/getPreference
2021-09-19 17:22:16 +02:00
*
2021-03-21 09:15:40 +01:00
* Return a single preference by name.
2021-03-07 09:46:12 +01:00
*/
2021-03-21 09:15:40 +01:00
public function show(Preference $preference): JsonResponse
2021-03-07 09:46:12 +01:00
{
$manager = $this->getManager();
2023-12-20 19:35:52 +01:00
if ('currencyPreference' === $preference->name) {
throw new FireflyException('Please use api/v1/currencies/default instead.');
}
2021-03-07 09:46:12 +01:00
/** @var PreferenceTransformer $transformer */
$transformer = app(PreferenceTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($preference, $transformer, 'preferences');
2021-03-07 09:46:12 +01:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
/**
* TODO This endpoint is not documented.
*
* Return a single preference by name.
*
2024-07-31 13:09:55 +02:00
* @param Collection<int, Preference> $collection
*/
public function showList(Collection $collection): JsonResponse
{
$manager = $this->getManager();
$count = $collection->count();
$pageSize = $this->parameters->get('limit');
$preferences = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
// make paginator:
$paginator = new LengthAwarePaginator($preferences, $count, $pageSize, $this->parameters->get('page'));
$paginator->setPath(route('api.v1.preferences.show-list').$this->buildParams());
/** @var PreferenceTransformer $transformer */
$transformer = app(PreferenceTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($preferences, $transformer, self::RESOURCE_KEY);
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2021-03-07 09:46:12 +01:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
/**
2021-09-19 17:22:16 +02:00
* This endpoint is documented at:
2023-02-12 06:53:36 +01:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/preferences/storePreference
2021-09-19 17:22:16 +02:00
*
2021-09-18 10:20:19 +02:00
* @throws FireflyException
2021-03-07 09:46:12 +01:00
*/
2021-03-21 09:15:40 +01:00
public function store(PreferenceStoreRequest $request): JsonResponse
2021-03-07 09:46:12 +01:00
{
$manager = $this->getManager();
$data = $request->getAll();
if ('currencyPreference' === $data['name']) {
throw new FireflyException('Please use api/v1/currencies/default instead.');
}
$pref = app('preferences')->set($data['name'], $data['data']);
2021-03-07 09:46:12 +01:00
/** @var PreferenceTransformer $transformer */
$transformer = app(PreferenceTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($pref, $transformer, 'preferences');
2021-03-07 09:46:12 +01:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
/**
2021-09-19 17:22:16 +02:00
* This endpoint is documented at:
2023-02-12 06:53:36 +01:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/preferences/updatePreference
2021-09-19 17:22:16 +02:00
*
2021-07-10 07:29:39 +02:00
* @throws FireflyException
2021-03-07 09:46:12 +01:00
*/
2021-03-21 09:15:40 +01:00
public function update(PreferenceUpdateRequest $request, Preference $preference): JsonResponse
2021-03-07 09:46:12 +01:00
{
if ('currencyPreference' === $preference->name) {
throw new FireflyException('Please use api/v1/currencies/default instead.');
}
$manager = $this->getManager();
$data = $request->getAll();
$pref = app('preferences')->set($preference->name, $data['data']);
2021-03-21 09:15:40 +01:00
2021-03-07 09:46:12 +01:00
/** @var PreferenceTransformer $transformer */
$transformer = app(PreferenceTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($pref, $transformer, 'preferences');
2021-03-07 09:46:12 +01:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
}