mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 02:36:28 +00:00 
			
		
		
		
	Reset some code.
This commit is contained in:
		| @@ -23,7 +23,10 @@ declare(strict_types=1); | ||||
|  | ||||
| namespace FireflyIII\Api\V1\Controllers; | ||||
|  | ||||
| use Exception; | ||||
| use FireflyIII\Api\V1\Requests\AccountRequest; | ||||
| use FireflyIII\Api\V1\Requests\AccountStoreRequest; | ||||
| use FireflyIII\Api\V1\Requests\AccountUpdateRequest; | ||||
| use FireflyIII\Helpers\Collector\GroupCollectorInterface; | ||||
| use FireflyIII\Models\Account; | ||||
| use FireflyIII\Repositories\Account\AccountRepositoryInterface; | ||||
| @@ -76,7 +79,7 @@ class AccountController extends Controller | ||||
|     /** | ||||
|      * Remove the specified resource from storage. | ||||
|      * | ||||
|      * @param \FireflyIII\Models\Account $account | ||||
|      * @param Account $account | ||||
|      * | ||||
|      * @return JsonResponse | ||||
|      */ | ||||
| @@ -132,7 +135,7 @@ class AccountController extends Controller | ||||
|  | ||||
|  | ||||
|     /** | ||||
|      * List all of them. | ||||
|      * List all piggies. | ||||
|      * | ||||
|      * @param Request $request | ||||
|      * @param Account $account | ||||
| @@ -177,7 +180,7 @@ class AccountController extends Controller | ||||
|      * @param Request $request | ||||
|      * @param Account $account | ||||
|      * | ||||
|      * @return \Illuminate\Http\JsonResponse | ||||
|      * @return JsonResponse | ||||
|      */ | ||||
|     public function show(Request $request, Account $account): JsonResponse | ||||
|     { | ||||
| @@ -196,11 +199,12 @@ class AccountController extends Controller | ||||
|     /** | ||||
|      * Store a new instance. | ||||
|      * | ||||
|      * @param AccountRequest $request | ||||
|      * @param AccountStoreRequest $request | ||||
|      * | ||||
|      * @return \Illuminate\Http\JsonResponse | ||||
|      * @return JsonResponse | ||||
|      * @throws Exception | ||||
|      */ | ||||
|     public function store(AccountRequest $request): JsonResponse | ||||
|     public function store(AccountStoreRequest $request): JsonResponse | ||||
|     { | ||||
|         $data    = $request->getAll(); | ||||
|         $account = $this->repository->store($data); | ||||
| @@ -285,12 +289,13 @@ class AccountController extends Controller | ||||
|     /** | ||||
|      * Update account. | ||||
|      * | ||||
|      * @param AccountRequest $request | ||||
|      * @param Account        $account | ||||
|      * @param AccountUpdateRequest $request | ||||
|      * @param Account              $account | ||||
|      * | ||||
|      * @return \Illuminate\Http\JsonResponse | ||||
|      * @return JsonResponse | ||||
|      * @throws Exception | ||||
|      */ | ||||
|     public function update(AccountRequest $request, Account $account): JsonResponse | ||||
|     public function update(AccountUpdateRequest $request, Account $account): JsonResponse | ||||
|     { | ||||
|         $data         = $request->getAll(); | ||||
|         $data['type'] = config('firefly.shortNamesByFullName.' . $account->accountType->type); | ||||
|   | ||||
| @@ -1,8 +1,8 @@ | ||||
| <?php | ||||
| 
 | ||||
| /** | ||||
|  * AccountRequest.php | ||||
|  * Copyright (c) 2018 thegrumpydictator@gmail.com | ||||
|  * AccountStoreRequest.php | ||||
|  * Copyright (c) 2019 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This file is part of Firefly III. | ||||
|  * | ||||
| @@ -24,12 +24,13 @@ declare(strict_types=1); | ||||
| 
 | ||||
| namespace FireflyIII\Api\V1\Requests; | ||||
| 
 | ||||
| use Exception; | ||||
| use FireflyIII\Rules\IsBoolean; | ||||
| 
 | ||||
| /** | ||||
|  * Class AccountRequest | ||||
|  * Class AccountStoreRequest | ||||
|  */ | ||||
| class AccountRequest extends Request | ||||
| class AccountStoreRequest extends Request | ||||
| { | ||||
| 
 | ||||
|     /** | ||||
| @@ -47,6 +48,7 @@ class AccountRequest extends Request | ||||
|      * Get all data from the request. | ||||
|      * | ||||
|      * @return array | ||||
|      * @throws Exception | ||||
|      */ | ||||
|     public function getAll(): array | ||||
|     { | ||||
| @@ -103,7 +105,7 @@ class AccountRequest extends Request | ||||
|         $ccPaymentTypes = implode(',', array_keys(config('firefly.ccTypes'))); | ||||
|         $rules          = [ | ||||
|             'name'                 => 'required|min:1|uniqueAccountForUser', | ||||
|             'type'                 => 'required|in:' . $types, | ||||
|             'type'                 => sprintf('in:%s', $types), | ||||
|             'iban'                 => 'iban|nullable', | ||||
|             'bic'                  => 'bic|nullable', | ||||
|             'account_number'       => 'between:1,255|nullable|uniqueAccountNumberForUser', | ||||
| @@ -114,8 +116,8 @@ class AccountRequest extends Request | ||||
|             'currency_code'        => 'min:3|max:3|exists:transaction_currencies,code', | ||||
|             'active'               => [new IsBoolean], | ||||
|             'include_net_worth'    => [new IsBoolean], | ||||
|             'account_role'         => 'in:' . $accountRoles . '|required_if:type,asset', | ||||
|             'credit_card_type'     => 'in:' . $ccPaymentTypes . '|required_if:account_role,ccAsset', | ||||
|             'account_role'         => sprintf('in:%s|required_if:type,asset', $accountRoles), | ||||
|             'credit_card_type'     => sprintf('in:%s|required_if:account_role,ccAsset', $ccPaymentTypes), | ||||
|             'monthly_payment_date' => 'date' . '|required_if:account_role,ccAsset|required_if:credit_card_type,monthlyFull', | ||||
|             'liability_type'       => 'required_if:type,liability|in:loan,debt,mortgage', | ||||
|             'liability_amount'     => 'required_if:type,liability|min:0|numeric', | ||||
| @@ -124,17 +126,6 @@ class AccountRequest extends Request | ||||
|             'interest_period'      => 'required_if:type,liability|in:daily,monthly,yearly', | ||||
|             'notes'                => 'min:0|max:65536', | ||||
|         ]; | ||||
|         switch ($this->method()) { | ||||
|             default: | ||||
|                 break; | ||||
|             case 'PUT': | ||||
|             case 'PATCH': | ||||
|                 $account                 = $this->route()->parameter('account'); | ||||
|                 $rules['name']           .= ':' . $account->id; | ||||
|                 $rules['account_number'] .= ':' . $account->id; | ||||
|                 $rules['type']           = 'in:' . $types; | ||||
|                 break; | ||||
|         } | ||||
| 
 | ||||
|         return $rules; | ||||
|     } | ||||
							
								
								
									
										133
									
								
								app/Api/V1/Requests/AccountUpdateRequest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										133
									
								
								app/Api/V1/Requests/AccountUpdateRequest.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,133 @@ | ||||
| <?php | ||||
|  | ||||
| /** | ||||
|  * AccountUpdateRequest.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; | ||||
|  | ||||
| use Exception; | ||||
| use FireflyIII\Rules\IsBoolean; | ||||
|  | ||||
| /** | ||||
|  * Class AccountUpdateRequest | ||||
|  */ | ||||
| class AccountUpdateRequest extends Request | ||||
| { | ||||
|  | ||||
|     /** | ||||
|      * Authorize logged in users. | ||||
|      * | ||||
|      * @return bool | ||||
|      */ | ||||
|     public function authorize(): bool | ||||
|     { | ||||
|         // Only allow authenticated users | ||||
|         return auth()->check(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get all data from the request. | ||||
|      * | ||||
|      * @return array | ||||
|      * @throws Exception | ||||
|      */ | ||||
|     public function getAll(): array | ||||
|     { | ||||
|         $active          = true; | ||||
|         $includeNetWorth = true; | ||||
|         if (null !== $this->get('active')) { | ||||
|             $active = $this->boolean('active'); | ||||
|         } | ||||
|         if (null !== $this->get('include_net_worth')) { | ||||
|             $includeNetWorth = $this->boolean('include_net_worth'); | ||||
|         } | ||||
|  | ||||
|         $data = [ | ||||
|             'name'                 => $this->string('name'), | ||||
|             'active'               => $active, | ||||
|             'include_net_worth'    => $includeNetWorth, | ||||
|             'accountType'          => $this->string('type'), | ||||
|             'account_type_id'      => null, | ||||
|             'currency_id'          => $this->integer('currency_id'), | ||||
|             'currency_code'        => $this->string('currency_code'), | ||||
|             'virtualBalance'       => $this->string('virtual_balance'), | ||||
|             'iban'                 => $this->string('iban'), | ||||
|             'BIC'                  => $this->string('bic'), | ||||
|             'accountNumber'        => $this->string('account_number'), | ||||
|             'accountRole'          => $this->string('account_role'), | ||||
|             'openingBalance'       => $this->string('opening_balance'), | ||||
|             'openingBalanceDate'   => $this->date('opening_balance_date'), | ||||
|             'ccType'               => $this->string('credit_card_type'), | ||||
|             'ccMonthlyPaymentDate' => $this->string('monthly_payment_date'), | ||||
|             'notes'                => $this->string('notes'), | ||||
|             'interest'             => $this->string('interest'), | ||||
|             'interest_period'      => $this->string('interest_period'), | ||||
|         ]; | ||||
|  | ||||
|         if ('liability' === $data['accountType']) { | ||||
|             $data['openingBalance']     = bcmul($this->string('liability_amount'), '-1'); | ||||
|             $data['openingBalanceDate'] = $this->date('liability_start_date'); | ||||
|             $data['accountType']        = $this->string('liability_type'); | ||||
|             $data['account_type_id']    = null; | ||||
|         } | ||||
|  | ||||
|         return $data; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * The rules that the incoming request must be matched against. | ||||
|      * | ||||
|      * @return array | ||||
|      */ | ||||
|     public function rules(): array | ||||
|     { | ||||
|         $account        = $this->route()->parameter('account'); | ||||
|         $accountRoles   = implode(',', config('firefly.accountRoles')); | ||||
|         $types          = implode(',', array_keys(config('firefly.subTitlesByIdentifier'))); | ||||
|         $ccPaymentTypes = implode(',', array_keys(config('firefly.ccTypes'))); | ||||
|         $rules          = [ | ||||
|             'name'                 => sprintf('required|min:1|uniqueAccountForUser:%d', $account->id), | ||||
|             'type'                 => sprintf('in:%s', $types), | ||||
|             'iban'                 => 'iban|nullable', | ||||
|             'bic'                  => 'bic|nullable', | ||||
|             'account_number'       => sprintf('between:1,255|nullable|uniqueAccountNumberForUser:%d', $account->id), | ||||
|             'opening_balance'      => 'numeric|required_with:opening_balance_date|nullable', | ||||
|             'opening_balance_date' => 'date|required_with:opening_balance|nullable', | ||||
|             'virtual_balance'      => 'numeric|nullable', | ||||
|             'currency_id'          => 'numeric|exists:transaction_currencies,id', | ||||
|             'currency_code'        => 'min:3|max:3|exists:transaction_currencies,code', | ||||
|             'active'               => [new IsBoolean], | ||||
|             'include_net_worth'    => [new IsBoolean], | ||||
|             'account_role'         => sprintf('in:%s|required_if:type,asset', $accountRoles), | ||||
|             'credit_card_type'     => sprintf('in:%s|required_if:account_role,ccAsset', $ccPaymentTypes), | ||||
|             'monthly_payment_date' => 'date' . '|required_if:account_role,ccAsset|required_if:credit_card_type,monthlyFull', | ||||
|             'liability_type'       => 'required_if:type,liability|in:loan,debt,mortgage', | ||||
|             'liability_amount'     => 'required_if:type,liability|min:0|numeric', | ||||
|             'liability_start_date' => 'required_if:type,liability|date', | ||||
|             'interest'             => 'required_if:type,liability|between:0,100|numeric', | ||||
|             'interest_period'      => 'required_if:type,liability|in:daily,monthly,yearly', | ||||
|             'notes'                => 'min:0|max:65536', | ||||
|         ]; | ||||
|  | ||||
|         return $rules; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user