mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 02:36:28 +00:00 
			
		
		
		
	Improve account list and view.
This commit is contained in:
		| @@ -108,12 +108,7 @@ abstract class Controller extends BaseController | ||||
|     { | ||||
|         $bag      = new ParameterBag(); | ||||
|         $page     = (int)request()->get('page'); | ||||
|         if ($page < 1) { | ||||
|             $page = 1; | ||||
|         } | ||||
|         if ($page > 2 ** 16) { | ||||
|             $page = 2 ** 16; | ||||
|         } | ||||
|         $page = min(max(1, $page), 2 ** 16); | ||||
|         $bag->set('page', $page); | ||||
| 
 | ||||
|         // some date fields:
 | ||||
| @@ -131,19 +126,15 @@ abstract class Controller extends BaseController | ||||
|             $obj  = null; | ||||
|             if (null !== $date) { | ||||
|                 try { | ||||
|                     $obj = Carbon::parse((string)$date); | ||||
|                     $obj = Carbon::parse((string)$date, config('app.timezone')); | ||||
|                 } catch (InvalidFormatException $e) { | ||||
|                     // don't care
 | ||||
|                     Log::warning( | ||||
|                         sprintf( | ||||
|                             'Ignored invalid date "%s" in API controller parameter check: %s', | ||||
|                             substr((string)$date, 0, 20), | ||||
|                             $e->getMessage() | ||||
|                         ) | ||||
|                     ); | ||||
|                     Log::warning(sprintf('Ignored invalid date "%s" in API controller parameter check: %s', substr((string)$date, 0, 20), $e->getMessage())); | ||||
|                 } | ||||
|             } | ||||
|             $bag->set($field, $obj); | ||||
|             if($obj instanceof Carbon){ | ||||
|                 $bag->set($field, $obj); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         // integer fields:
 | ||||
|   | ||||
| @@ -25,6 +25,7 @@ declare(strict_types=1); | ||||
| namespace FireflyIII\Api\V1\Controllers\Models\Account; | ||||
| 
 | ||||
| use FireflyIII\Api\V1\Controllers\Controller; | ||||
| use FireflyIII\Api\V1\Requests\Models\Account\ShowRequest; | ||||
| use FireflyIII\Exceptions\FireflyException; | ||||
| use FireflyIII\Models\Account; | ||||
| use FireflyIII\Repositories\Account\AccountRepositoryInterface; | ||||
| @@ -33,7 +34,6 @@ use FireflyIII\Support\JsonApi\Enrichments\AccountEnrichment; | ||||
| use FireflyIII\Transformers\AccountTransformer; | ||||
| use FireflyIII\User; | ||||
| use Illuminate\Http\JsonResponse; | ||||
| use Illuminate\Http\Request; | ||||
| use Illuminate\Pagination\LengthAwarePaginator; | ||||
| use League\Fractal\Pagination\IlluminatePaginatorAdapter; | ||||
| use League\Fractal\Resource\Collection as FractalCollection; | ||||
| @@ -71,41 +71,40 @@ class ShowController extends Controller | ||||
|      * | ||||
|      * @throws FireflyException | ||||
|      */ | ||||
|     public function index(Request $request): JsonResponse | ||||
|     public function index(ShowRequest $request): JsonResponse | ||||
|     { | ||||
|         $manager     = $this->getManager(); | ||||
|         $type        = $request->get('type') ?? 'all'; | ||||
|         $this->parameters->set('type', $type); | ||||
|         $manager = $this->getManager(); | ||||
|         $params  = $request->getParameters(); | ||||
|         $this->parameters->set('type', $params['type']); | ||||
| 
 | ||||
|         // types to get, page size:
 | ||||
|         $types       = $this->mapAccountTypes($this->parameters->get('type')); | ||||
|         $pageSize    = $this->parameters->get('limit'); | ||||
|         $types    = $this->mapAccountTypes($params['type']); | ||||
| 
 | ||||
|         // get list of accounts. Count it and split it.
 | ||||
|         $this->repository->resetAccountOrder(); | ||||
|         $collection  = $this->repository->getAccountsByType($types, $this->parameters->get('sort') ?? []); | ||||
|         $count       = $collection->count(); | ||||
|         $collection = $this->repository->getAccountsByType($types, $params['sort']); | ||||
|         $count      = $collection->count(); | ||||
| 
 | ||||
|         // continue sort:
 | ||||
|         $accounts    = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); | ||||
|         $accounts = $collection->slice(($this->parameters->get('page') - 1) * $params['limit'], $params['limit']); | ||||
| 
 | ||||
|         // enrich
 | ||||
|         /** @var User $admin */ | ||||
|         $admin       = auth()->user(); | ||||
|         $enrichment  = new AccountEnrichment(); | ||||
|         $admin      = auth()->user(); | ||||
|         $enrichment = new AccountEnrichment(); | ||||
|         $enrichment->setDate($this->parameters->get('date')); | ||||
|         $enrichment->setUser($admin); | ||||
|         $accounts    = $enrichment->enrich($accounts); | ||||
|         $accounts = $enrichment->enrich($accounts); | ||||
| 
 | ||||
|         // make paginator:
 | ||||
|         $paginator   = new LengthAwarePaginator($accounts, $count, $pageSize, $this->parameters->get('page')); | ||||
|         $paginator->setPath(route('api.v1.accounts.index').$this->buildParams()); | ||||
|         $paginator = new LengthAwarePaginator($accounts, $count, $params['limit'], $this->parameters->get('page')); | ||||
|         $paginator->setPath(route('api.v1.accounts.index') . $this->buildParams()); | ||||
| 
 | ||||
|         /** @var AccountTransformer $transformer */ | ||||
|         $transformer = app(AccountTransformer::class); | ||||
|         $transformer->setParameters($this->parameters); | ||||
| 
 | ||||
|         $resource    = new FractalCollection($accounts, $transformer, self::RESOURCE_KEY); | ||||
|         $resource = new FractalCollection($accounts, $transformer, self::RESOURCE_KEY); | ||||
|         $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); | ||||
| 
 | ||||
|         return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); | ||||
| @@ -122,21 +121,21 @@ class ShowController extends Controller | ||||
|         // get list of accounts. Count it and split it.
 | ||||
|         $this->repository->resetAccountOrder(); | ||||
|         $account->refresh(); | ||||
|         $manager     = $this->getManager(); | ||||
|         $manager = $this->getManager(); | ||||
| 
 | ||||
|         // enrich
 | ||||
|         /** @var User $admin */ | ||||
|         $admin       = auth()->user(); | ||||
|         $enrichment  = new AccountEnrichment(); | ||||
|         $admin      = auth()->user(); | ||||
|         $enrichment = new AccountEnrichment(); | ||||
|         $enrichment->setDate($this->parameters->get('date')); | ||||
|         $enrichment->setUser($admin); | ||||
|         $account     = $enrichment->enrichSingle($account); | ||||
|         $account = $enrichment->enrichSingle($account); | ||||
| 
 | ||||
| 
 | ||||
|         /** @var AccountTransformer $transformer */ | ||||
|         $transformer = app(AccountTransformer::class); | ||||
|         $transformer->setParameters($this->parameters); | ||||
|         $resource    = new Item($account, $transformer, self::RESOURCE_KEY); | ||||
|         $resource = new Item($account, $transformer, self::RESOURCE_KEY); | ||||
| 
 | ||||
|         return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); | ||||
|     } | ||||
|   | ||||
							
								
								
									
										96
									
								
								app/Api/V1/Requests/Models/Account/ShowRequest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										96
									
								
								app/Api/V1/Requests/Models/Account/ShowRequest.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,96 @@ | ||||
| <?php | ||||
| /* | ||||
|  * ShowRequest.php | ||||
|  * Copyright (c) 2025 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\Requests\Models\Account; | ||||
| 
 | ||||
| 
 | ||||
| use Carbon\Carbon; | ||||
| use FireflyIII\Models\Preference; | ||||
| use FireflyIII\Support\Facades\Preferences; | ||||
| use FireflyIII\Support\Http\Api\AccountFilter; | ||||
| use FireflyIII\Support\Request\ConvertsDataTypes; | ||||
| use FireflyIII\User; | ||||
| use Illuminate\Contracts\Validation\Validator; | ||||
| use Illuminate\Foundation\Http\FormRequest; | ||||
| 
 | ||||
| class ShowRequest extends FormRequest | ||||
| { | ||||
|     use ConvertsDataTypes; | ||||
|     use AccountFilter; | ||||
| 
 | ||||
|     public function getParameters(): array | ||||
|     { | ||||
|         $limit = $this->convertInteger('limit'); | ||||
|         if (0 === $limit) { | ||||
|             // get default for user:
 | ||||
|             /** @var User $user */ | ||||
|             $user = auth()->user(); | ||||
| 
 | ||||
|             /** @var Preference $pageSize */ | ||||
|             $limit = (int)Preferences::getForUser($user, 'listPageSize', 50)->data; | ||||
|         } | ||||
| 
 | ||||
|         $page = $this->convertInteger('page'); | ||||
|         $page = min(max(1, $page), 2 ** 16); | ||||
| 
 | ||||
|         return [ | ||||
|             'type'  => $this->convertString('type', 'all'), | ||||
|             'limit' => $limit, | ||||
|             'sort'  => $this->convertString('sort', 'order'), | ||||
|             'page'  => $page, | ||||
|         ]; | ||||
|     } | ||||
| 
 | ||||
|     public function rules(): array | ||||
|     { | ||||
|         $keys = join(',', array_keys($this->types)); | ||||
|         return [ | ||||
|             'date'  => 'date', | ||||
|             'start' => 'date|present_with:end|before_or_equal:end|before:2038-01-17|after:1970-01-02', | ||||
|             'end'   => 'date|present_with:start|after_or_equal:start|before:2038-01-17|after:1970-01-02', | ||||
|             'sort'  => 'in:active,iban,name,order,-active,-iban,-name,-order', // TODO improve me.
 | ||||
|             'type'  => sprintf('in:%s', $keys), | ||||
|             'limit' => 'number|min:1|max:131337', | ||||
|             'page'  => 'number|min:1|max:131337', | ||||
|         ]; | ||||
|     } | ||||
| 
 | ||||
|     public function withValidator(Validator $validator): void | ||||
|     { | ||||
|         $validator->after( | ||||
|             function (Validator $validator): void { | ||||
|                 if ($validator->failed()) { | ||||
|                     return; | ||||
|                 } | ||||
|                 $data = $validator->getData(); | ||||
|                 if (array_key_exists('date', $data) && array_key_exists('start', $data) && array_key_exists('end', $data)) { | ||||
|                     // assume valid dates, before we got here.
 | ||||
|                     $start = Carbon::parse($data['start'], config('app.timezone'))->startOfDay(); | ||||
|                     $end   = Carbon::parse($data['end'], config('app.timezone'))->endOfDay(); | ||||
|                     $date  = Carbon::parse($data['date'], config('app.timezone')); | ||||
|                     if (!$date->between($start, $end)) { | ||||
|                         $validator->errors()->add('date', (string)trans('validation.between_date')); | ||||
|                     } | ||||
|                 } | ||||
|             }); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @@ -78,7 +78,7 @@ class StoreRequest extends FormRequest | ||||
|             'object_group_id'           => 'numeric|belongsToUser:object_groups,id', | ||||
|             'object_group_title'        => ['min:1', 'max:255'], | ||||
|             'target_amount'             => ['required', new IsValidZeroOrMoreAmount()], | ||||
|             'start_date'                => 'date|nullable', | ||||
|             'start_date'                => 'required|date|after:1970-01-01|before:2038-01-17', | ||||
|             'transaction_currency_id'   => 'exists:transaction_currencies,id|required_without:transaction_currency_code', | ||||
|             'transaction_currency_code' => 'exists:transaction_currencies,code|required_without:transaction_currency_id', | ||||
|             'target_date'               => 'date|nullable|after:start_date', | ||||
|   | ||||
		Reference in New Issue
	
	Block a user