diff --git a/app/Api/V1/Controllers/BudgetController.php b/app/Api/V1/Controllers/BudgetController.php index 595349f36a..8b144d7bf4 100644 --- a/app/Api/V1/Controllers/BudgetController.php +++ b/app/Api/V1/Controllers/BudgetController.php @@ -232,31 +232,6 @@ class BudgetController extends Controller return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); } - /** - * Store a newly created resource in storage. - * - * @param BudgetLimitRequest $request - * @param Budget $budget - * - * @throws Exception - * @return JsonResponse - */ - public function storeBudgetLimit(BudgetLimitRequest $request, Budget $budget): JsonResponse - { - $data = $request->getAll(); - $data['budget'] = $budget; - $budgetLimit = $this->blRepository->storeBudgetLimit($data); - $manager = $this->getManager(); - - /** @var BudgetLimitTransformer $transformer */ - $transformer = app(BudgetLimitTransformer::class); - $transformer->setParameters($this->parameters); - - $resource = new Item($budgetLimit, $transformer, 'budget_limits'); - - return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); - } - /** * Show all transactions. * diff --git a/app/Api/V1/Controllers/BudgetLimitController.php b/app/Api/V1/Controllers/BudgetLimitController.php index d1f38d2403..56a018c1fe 100644 --- a/app/Api/V1/Controllers/BudgetLimitController.php +++ b/app/Api/V1/Controllers/BudgetLimitController.php @@ -158,20 +158,19 @@ class BudgetLimitController extends Controller * * @param BudgetLimitRequest $request * + * @return JsonResponse * @throws FireflyException * - * @return JsonResponse */ public function store(BudgetLimitRequest $request): JsonResponse { - $data = $request->getAll(); - $budget = $this->repository->findNull($data['budget_id']); - if (null === $budget) { - throw new FireflyException('200004: Budget does not exist.'); // @codeCoverageIgnore - } - $data['budget'] = $budget; - $budgetLimit = $this->blRepository->storeBudgetLimit($data); - $manager = $this->getManager(); + $data = $request->getAll(); + $data['start_date'] = $data['start']; + $data['end_date'] = $data['end']; + + $budgetLimit = $this->blRepository->store($data); + $manager = $this->getManager(); + /** @var BudgetLimitTransformer $transformer */ $transformer = app(BudgetLimitTransformer::class); diff --git a/app/Api/V1/Requests/BudgetLimitRequest.php b/app/Api/V1/Requests/BudgetLimitRequest.php index ef87da2e50..dd93beb0ae 100644 --- a/app/Api/V1/Requests/BudgetLimitRequest.php +++ b/app/Api/V1/Requests/BudgetLimitRequest.php @@ -35,6 +35,7 @@ use Illuminate\Foundation\Http\FormRequest; class BudgetLimitRequest extends FormRequest { use ConvertsDataTypes; + /** * Authorize logged in users. * @@ -53,7 +54,7 @@ class BudgetLimitRequest extends FormRequest */ public function getAll(): array { - return [ + $data = [ 'budget_id' => $this->integer('budget_id'), 'start' => $this->date('start'), 'end' => $this->date('end'), @@ -61,6 +62,12 @@ class BudgetLimitRequest extends FormRequest 'currency_id' => $this->integer('currency_id'), 'currency_code' => $this->string('currency_code'), ]; + // if request has a budget already, drop the rule. + $budget = $this->route()->parameter('budget'); + if (null !== $budget) { + $data['budget_id'] = $budget->id; + } + return $data; } /** diff --git a/app/Repositories/Budget/BudgetLimitRepository.php b/app/Repositories/Budget/BudgetLimitRepository.php index b725c943b8..93908559c3 100644 --- a/app/Repositories/Budget/BudgetLimitRepository.php +++ b/app/Repositories/Budget/BudgetLimitRepository.php @@ -26,6 +26,7 @@ namespace FireflyIII\Repositories\Budget; use Carbon\Carbon; use Exception; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Factory\TransactionCurrencyFactory; use FireflyIII\Models\Budget; use FireflyIII\Models\BudgetLimit; @@ -284,23 +285,10 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface * @param array $data * * @return BudgetLimit + * @throws FireflyException */ public function store(array $data): BudgetLimit { - return BudgetLimit::create($data); - } - - /** - * @param array $data - * - * @return BudgetLimit - * @deprecated - */ - public function storeBudgetLimit(array $data): BudgetLimit - { - /** @var Budget $budget */ - $budget = $data['budget']; - // if no currency has been provided, use the user's default currency: /** @var TransactionCurrencyFactory $factory */ $factory = app(TransactionCurrencyFactory::class); @@ -308,16 +296,23 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface if (null === $currency) { $currency = app('amount')->getDefaultCurrencyByUser($this->user); } + $currency->enabled = true; + $currency->save(); - // find limit with same date range. - // if it exists, return that one. + // find the budget: + $budget = $this->user->budgets()->find((int) $data['budget_id']); + if (null === $budget) { + throw new FireflyException('200004: Budget does not exist.'); // @codeCoverageIgnore + } + + // find limit with same date range and currency. $limit = $budget->budgetlimits() ->where('budget_limits.start_date', $data['start']->format('Y-m-d 00:00:00')) ->where('budget_limits.end_date', $data['end']->format('Y-m-d 00:00:00')) ->where('budget_limits.transaction_currency_id', $currency->id) ->get(['budget_limits.*'])->first(); if (null !== $limit) { - return $limit; + throw new FireflyException('200027: Budget limit already exists.'); // @codeCoverageIgnore } Log::debug('No existing budget limit, create a new one'); diff --git a/app/Repositories/Budget/BudgetLimitRepositoryInterface.php b/app/Repositories/Budget/BudgetLimitRepositoryInterface.php index c9813fe39a..488df1e81b 100644 --- a/app/Repositories/Budget/BudgetLimitRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetLimitRepositoryInterface.php @@ -111,14 +111,6 @@ interface BudgetLimitRepositoryInterface */ public function store(array $data): BudgetLimit; - /** - * @param array $data - * - * @return BudgetLimit - * @deprecated - */ - public function storeBudgetLimit(array $data): BudgetLimit; - /** * @param BudgetLimit $budgetLimit * @param array $data diff --git a/routes/api.php b/routes/api.php index e8dd5897ed..c1c66396c7 100644 --- a/routes/api.php +++ b/routes/api.php @@ -167,7 +167,7 @@ Route::group( Route::get('{budget}', ['uses' => 'BudgetController@show', 'as' => 'show']); Route::put('{budget}', ['uses' => 'BudgetController@update', 'as' => 'update']); Route::delete('{budget}', ['uses' => 'BudgetController@delete', 'as' => 'delete']); - Route::post('{budget}/limits', ['uses' => 'BudgetController@storeBudgetLimit', 'as' => 'store_budget_limit']); + Route::post('{budget}/limits', ['uses' => 'BudgetLimitController@store', 'as' => 'store_budget_limit']); Route::get('{budget}/transactions', ['uses' => 'BudgetController@transactions', 'as' => 'transactions']); Route::get('{budget}/attachments', ['uses' => 'BudgetController@attachments', 'as' => 'attachments']);