diff --git a/app/Api/V1/Controllers/Models/Rule/DestroyController.php b/app/Api/V1/Controllers/Models/Rule/DestroyController.php new file mode 100644 index 0000000000..237f44621a --- /dev/null +++ b/app/Api/V1/Controllers/Models/Rule/DestroyController.php @@ -0,0 +1,74 @@ +. + */ + +namespace FireflyIII\Api\V1\Controllers\Models\Rule; + + +use FireflyIII\Api\V1\Controllers\Controller; +use FireflyIII\Models\Rule; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Repositories\Rule\RuleRepositoryInterface; +use FireflyIII\User; +use Illuminate\Http\JsonResponse; + +/** + * Class DestroyController + */ +class DestroyController extends Controller +{ + private RuleRepositoryInterface $ruleRepository; + + /** + * RuleController constructor. + * + * @codeCoverageIgnore + */ + public function __construct() + { + parent::__construct(); + $this->middleware( + function ($request, $next) { + /** @var User $user */ + $user = auth()->user(); + + $this->ruleRepository = app(RuleRepositoryInterface::class); + $this->ruleRepository->setUser($user); + + return $next($request); + } + ); + } + + /** + * Delete the resource. + * + * @param Rule $rule + * + * @return JsonResponse + * @codeCoverageIgnore + */ + public function destroy(Rule $rule): JsonResponse + { + $this->ruleRepository->destroy($rule); + + return response()->json([], 204); + } +} \ No newline at end of file diff --git a/app/Api/V1/Controllers/Models/Rule/ListController.php b/app/Api/V1/Controllers/Models/Rule/ListController.php new file mode 100644 index 0000000000..86a9a99a44 --- /dev/null +++ b/app/Api/V1/Controllers/Models/Rule/ListController.php @@ -0,0 +1,62 @@ +. + */ + +namespace FireflyIII\Api\V1\Controllers\Models\Rule; + + +use FireflyIII\Api\V1\Controllers\Controller; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Repositories\Rule\RuleRepositoryInterface; +use FireflyIII\User; + +/** + * Class ListController + */ +class ListController extends Controller +{ + private AccountRepositoryInterface $accountRepository; + private RuleRepositoryInterface $ruleRepository; + + + /** + * RuleController constructor. + * + * @codeCoverageIgnore + */ + public function __construct() + { + parent::__construct(); + $this->middleware( + function ($request, $next) { + /** @var User $user */ + $user = auth()->user(); + + $this->ruleRepository = app(RuleRepositoryInterface::class); + $this->ruleRepository->setUser($user); + + $this->accountRepository = app(AccountRepositoryInterface::class); + $this->accountRepository->setUser($user); + + return $next($request); + } + ); + } +} \ No newline at end of file diff --git a/app/Api/V1/Controllers/Models/Rule/ShowController.php b/app/Api/V1/Controllers/Models/Rule/ShowController.php new file mode 100644 index 0000000000..6a46329d62 --- /dev/null +++ b/app/Api/V1/Controllers/Models/Rule/ShowController.php @@ -0,0 +1,121 @@ +. + */ + +namespace FireflyIII\Api\V1\Controllers\Models\Rule; + + +use FireflyIII\Api\V1\Controllers\Controller; +use FireflyIII\Models\Rule; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Repositories\Rule\RuleRepositoryInterface; +use FireflyIII\Transformers\RuleTransformer; +use FireflyIII\User; +use Illuminate\Http\JsonResponse; +use Illuminate\Pagination\LengthAwarePaginator; +use League\Fractal\Pagination\IlluminatePaginatorAdapter; +use League\Fractal\Resource\Collection as FractalCollection; +use League\Fractal\Resource\Item; + +/** + * Class ShowController + */ +class ShowController extends Controller +{ + private RuleRepositoryInterface $ruleRepository; + + /** + * RuleController constructor. + * + * @codeCoverageIgnore + */ + public function __construct() + { + parent::__construct(); + $this->middleware( + function ($request, $next) { + /** @var User $user */ + $user = auth()->user(); + + $this->ruleRepository = app(RuleRepositoryInterface::class); + $this->ruleRepository->setUser($user); + + return $next($request); + } + ); + } + + + /** + * List all of them. + * + * @return JsonResponse + * @codeCoverageIgnore + */ + public function index(): JsonResponse + { + $manager = $this->getManager(); + + // types to get, page size: + $pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; + + // get list of budgets. Count it and split it. + $collection = $this->ruleRepository->getAll(); + $count = $collection->count(); + $rules = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); + + // make paginator: + $paginator = new LengthAwarePaginator($rules, $count, $pageSize, $this->parameters->get('page')); + $paginator->setPath(route('api.v1.rules.index') . $this->buildParams()); + + /** @var RuleTransformer $transformer */ + $transformer = app(RuleTransformer::class); + $transformer->setParameters($this->parameters); + + $resource = new FractalCollection($rules, $transformer, 'rules'); + $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); + + return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); + + } + + + + /** + * List single resource. + * + * @param Rule $rule + * + * @return JsonResponse + * @codeCoverageIgnore + */ + public function show(Rule $rule): JsonResponse + { + $manager = $this->getManager(); + /** @var RuleTransformer $transformer */ + $transformer = app(RuleTransformer::class); + $transformer->setParameters($this->parameters); + + $resource = new Item($rule, $transformer, 'rules'); + + return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); + + } +} \ No newline at end of file diff --git a/app/Api/V1/Controllers/Models/Rule/StoreController.php b/app/Api/V1/Controllers/Models/Rule/StoreController.php new file mode 100644 index 0000000000..b3f54c542f --- /dev/null +++ b/app/Api/V1/Controllers/Models/Rule/StoreController.php @@ -0,0 +1,80 @@ +. + */ + +namespace FireflyIII\Api\V1\Controllers\Models\Rule; + + +use FireflyIII\Api\V1\Controllers\Controller; +use FireflyIII\Api\V1\Requests\Models\Rule\StoreRequest; +use FireflyIII\Repositories\Rule\RuleRepositoryInterface; +use FireflyIII\Transformers\RuleTransformer; +use FireflyIII\User; +use Illuminate\Http\JsonResponse; +use League\Fractal\Resource\Item; + +/** + * Class StoreController + */ +class StoreController extends Controller +{ + private RuleRepositoryInterface $ruleRepository; + + /** + * RuleController constructor. + * + * @codeCoverageIgnore + */ + public function __construct() + { + parent::__construct(); + $this->middleware( + function ($request, $next) { + /** @var User $user */ + $user = auth()->user(); + + $this->ruleRepository = app(RuleRepositoryInterface::class); + $this->ruleRepository->setUser($user); + + return $next($request); + } + ); + } + + /** + * Store new object. + * + * @param StoreRequest $request + * + * @return JsonResponse + */ + public function store(StoreRequest $request): JsonResponse + { + $rule = $this->ruleRepository->store($request->getAll()); + $manager = $this->getManager(); + /** @var RuleTransformer $transformer */ + $transformer = app(RuleTransformer::class); + $transformer->setParameters($this->parameters); + + $resource = new Item($rule, $transformer, 'rules'); + + return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); + } +} \ No newline at end of file diff --git a/app/Api/V1/Controllers/Models/Rule/TriggerController.php b/app/Api/V1/Controllers/Models/Rule/TriggerController.php new file mode 100644 index 0000000000..32d5c76b8d --- /dev/null +++ b/app/Api/V1/Controllers/Models/Rule/TriggerController.php @@ -0,0 +1,152 @@ +. + */ + +namespace FireflyIII\Api\V1\Controllers\Models\Rule; + + +use FireflyIII\Api\V1\Controllers\Controller; +use FireflyIII\Api\V1\Requests\Models\Rule\TestRequest; +use FireflyIII\Api\V1\Requests\Models\Rule\TriggerRequest; +use FireflyIII\Models\Rule; +use FireflyIII\Repositories\Rule\RuleRepositoryInterface; +use FireflyIII\TransactionRules\Engine\RuleEngineInterface; +use FireflyIII\Transformers\TransactionGroupTransformer; +use FireflyIII\User; +use Illuminate\Http\JsonResponse; +use Illuminate\Pagination\LengthAwarePaginator; +use Illuminate\Support\Collection; +use League\Fractal\Pagination\IlluminatePaginatorAdapter; +use League\Fractal\Resource\Collection as FractalCollection; + +/** + * Class TriggerController + */ +class TriggerController extends Controller +{ + private RuleRepositoryInterface $ruleRepository; + + + /** + * RuleController constructor. + * + * @codeCoverageIgnore + */ + public function __construct() + { + parent::__construct(); + $this->middleware( + function ($request, $next) { + /** @var User $user */ + $user = auth()->user(); + + $this->ruleRepository = app(RuleRepositoryInterface::class); + $this->ruleRepository->setUser($user); + + return $next($request); + } + ); + } + + /** + * @param TestRequest $request + * @param Rule $rule + * + * @return JsonResponse + */ + public function testRule(TestRequest $request, Rule $rule): JsonResponse + { + $parameters = $request->getTestParameters(); + + /** @var RuleEngineInterface $ruleEngine */ + $ruleEngine = app(RuleEngineInterface::class); + $ruleEngine->setRules(new Collection([$rule])); + + // overrule the rule(s) if necessary. + if (array_key_exists('start', $parameters) && null !== $parameters['start']) { + // add a range: + $ruleEngine->addOperator(['type' => 'date_after', 'value' => $parameters['start']->format('Y-m-d')]); + } + + if (array_key_exists('end', $parameters) && null !== $parameters['end']) { + // add a range: + $ruleEngine->addOperator(['type' => 'date_before', 'value' => $parameters['end']->format('Y-m-d')]); + } + if (array_key_exists('accounts', $parameters) && '' !== $parameters['accounts']) { + $ruleEngine->addOperator(['type' => 'account_id', 'value' => implode(',', $parameters['accounts'])]); + } + + // file the rule(s) + $transactions = $ruleEngine->find(); + $count = $transactions->count(); + + $paginator = new LengthAwarePaginator($transactions, $count, 31337, $this->parameters->get('page')); + $paginator->setPath(route('api.v1.rules.test', [$rule->id]) . $this->buildParams()); + + // resulting list is presented as JSON thing. + $manager = $this->getManager(); + /** @var TransactionGroupTransformer $transformer */ + $transformer = app(TransactionGroupTransformer::class); + $transformer->setParameters($this->parameters); + + $resource = new FractalCollection($transactions, $transformer, 'transactions'); + $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); + + return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); + } + + + /** + * Execute the given rule group on a set of existing transactions. + * + * @param TriggerRequest $request + * @param Rule $rule + * + * @return JsonResponse + */ + public function triggerRule(TriggerRequest $request, Rule $rule): JsonResponse + { + // Get parameters specified by the user + $parameters = $request->getTriggerParameters(); + + /** @var RuleEngineInterface $ruleEngine */ + $ruleEngine = app(RuleEngineInterface::class); + $ruleEngine->setRules(new Collection([$rule])); + + // overrule the rule(s) if necessary. + if (array_key_exists('start', $parameters) && null !== $parameters['start']) { + // add a range: + $ruleEngine->addOperator(['type' => 'date_after', 'value' => $parameters['start']->format('Y-m-d')]); + } + + if (array_key_exists('end', $parameters) && null !== $parameters['end']) { + // add a range: + $ruleEngine->addOperator(['type' => 'date_before', 'value' => $parameters['end']->format('Y-m-d')]); + } + if (array_key_exists('accounts', $parameters) && '' !== $parameters['accounts']) { + $ruleEngine->addOperator(['type' => 'account_id', 'value' => implode(',', $parameters['accounts'])]); + } + + // file the rule(s) + $ruleEngine->fire(); + + return response()->json([], 204); + } +} \ No newline at end of file diff --git a/app/Api/V1/Controllers/Models/Rule/UpdateController.php b/app/Api/V1/Controllers/Models/Rule/UpdateController.php new file mode 100644 index 0000000000..84d91db599 --- /dev/null +++ b/app/Api/V1/Controllers/Models/Rule/UpdateController.php @@ -0,0 +1,86 @@ +. + */ + +namespace FireflyIII\Api\V1\Controllers\Models\Rule; + + +use FireflyIII\Api\V1\Controllers\Controller; +use FireflyIII\Api\V1\Requests\Models\Rule\UpdateRequest; +use FireflyIII\Models\Rule; +use FireflyIII\Repositories\Rule\RuleRepositoryInterface; +use FireflyIII\Transformers\RuleTransformer; +use FireflyIII\User; +use Illuminate\Http\JsonResponse; +use League\Fractal\Resource\Item; + +/** + * Class UpdateController + */ +class UpdateController extends Controller +{ + private RuleRepositoryInterface $ruleRepository; + + + /** + * RuleController constructor. + * + * @codeCoverageIgnore + */ + public function __construct() + { + parent::__construct(); + $this->middleware( + function ($request, $next) { + /** @var User $user */ + $user = auth()->user(); + + $this->ruleRepository = app(RuleRepositoryInterface::class); + $this->ruleRepository->setUser($user); + + return $next($request); + } + ); + } + + + /** + * Update a rule. + * + * @param UpdateRequest $request + * @param Rule $rule + * + * @return JsonResponse + */ + public function update(UpdateRequest $request, Rule $rule): JsonResponse + { + $data = $request->getAll(); + $rule = $this->ruleRepository->update($rule, $data); + $manager = $this->getManager(); + + /** @var RuleTransformer $transformer */ + $transformer = app(RuleTransformer::class); + $transformer->setParameters($this->parameters); + + $resource = new Item($rule, $transformer, 'rules'); + + return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); + } +} \ No newline at end of file diff --git a/app/Api/V1/Controllers/todo/RuleController.php b/app/Api/V1/Controllers/todo/RuleController.php deleted file mode 100644 index 2c854455ee..0000000000 --- a/app/Api/V1/Controllers/todo/RuleController.php +++ /dev/null @@ -1,320 +0,0 @@ -. - */ - -declare(strict_types=1); - -namespace FireflyIII\Api\V1\Controllers; - -use FireflyIII\Api\V1\Requests\RuleStoreRequest; -use FireflyIII\Api\V1\Requests\RuleTestRequest; -use FireflyIII\Api\V1\Requests\RuleTriggerRequest; -use FireflyIII\Api\V1\Requests\RuleUpdateRequest; -use FireflyIII\Exceptions\FireflyException; -use FireflyIII\Models\Rule; -use FireflyIII\Repositories\Account\AccountRepositoryInterface; -use FireflyIII\Repositories\Rule\RuleRepositoryInterface; -use FireflyIII\TransactionRules\Engine\RuleEngineInterface; -use FireflyIII\Transformers\RuleTransformer; -use FireflyIII\Transformers\TransactionGroupTransformer; -use FireflyIII\User; -use Illuminate\Http\JsonResponse; -use Illuminate\Pagination\LengthAwarePaginator; -use Illuminate\Support\Collection; -use League\Fractal\Pagination\IlluminatePaginatorAdapter; -use League\Fractal\Resource\Collection as FractalCollection; -use League\Fractal\Resource\Item; - -/** - * Class RuleController - */ -class RuleController extends Controller -{ - /** @var AccountRepositoryInterface Account repository */ - private $accountRepository; - - /** @var RuleRepositoryInterface The rule repository */ - private $ruleRepository; - - - /** - * RuleController constructor. - * - * @codeCoverageIgnore - */ - public function __construct() - { - parent::__construct(); - $this->middleware( - function ($request, $next) { - /** @var User $user */ - $user = auth()->user(); - - $this->ruleRepository = app(RuleRepositoryInterface::class); - $this->ruleRepository->setUser($user); - - $this->accountRepository = app(AccountRepositoryInterface::class); - $this->accountRepository->setUser($user); - - return $next($request); - } - ); - } - - /** - * Delete the resource. - * - * @param Rule $rule - * - * @return JsonResponse - * @codeCoverageIgnore - */ - public function delete(Rule $rule): JsonResponse - { - $this->ruleRepository->destroy($rule); - - return response()->json([], 204); - } - - /** - * List all of them. - * - * @return JsonResponse - * @codeCoverageIgnore - */ - public function index(): JsonResponse - { - $manager = $this->getManager(); - - // types to get, page size: - $pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data; - - // get list of budgets. Count it and split it. - $collection = $this->ruleRepository->getAll(); - $count = $collection->count(); - $rules = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); - - // make paginator: - $paginator = new LengthAwarePaginator($rules, $count, $pageSize, $this->parameters->get('page')); - $paginator->setPath(route('api.v1.rules.index') . $this->buildParams()); - - /** @var RuleTransformer $transformer */ - $transformer = app(RuleTransformer::class); - $transformer->setParameters($this->parameters); - - $resource = new FractalCollection($rules, $transformer, 'rules'); - $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); - - return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); - - } - - /** - * @param Rule $rule - * - * @return JsonResponse - */ - public function moveDown(Rule $rule): JsonResponse - { - $this->ruleRepository->moveDown($rule); - $rule = $this->ruleRepository->find($rule->id); - $manager = $this->getManager(); - - /** @var RuleTransformer $transformer */ - $transformer = app(RuleTransformer::class); - $transformer->setParameters($this->parameters); - - $resource = new Item($rule, $transformer, 'rules'); - - return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); - - } - - /** - * @param Rule $rule - * - * @return JsonResponse - */ - public function moveUp(Rule $rule): JsonResponse - { - $this->ruleRepository->moveUp($rule); - $rule = $this->ruleRepository->find($rule->id); - $manager = $this->getManager(); - - /** @var RuleTransformer $transformer */ - $transformer = app(RuleTransformer::class); - $transformer->setParameters($this->parameters); - - $resource = new Item($rule, $transformer, 'rules'); - - return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); - - } - - /** - * List single resource. - * - * @param Rule $rule - * - * @return JsonResponse - * @codeCoverageIgnore - */ - public function show(Rule $rule): JsonResponse - { - $manager = $this->getManager(); - /** @var RuleTransformer $transformer */ - $transformer = app(RuleTransformer::class); - $transformer->setParameters($this->parameters); - - $resource = new Item($rule, $transformer, 'rules'); - - return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); - - } - - /** - * Store new object. - * - * @param RuleStoreRequest $request - * - * @return JsonResponse - */ - public function store(RuleStoreRequest $request): JsonResponse - { - $rule = $this->ruleRepository->store($request->getAll()); - $manager = $this->getManager(); - /** @var RuleTransformer $transformer */ - $transformer = app(RuleTransformer::class); - $transformer->setParameters($this->parameters); - - $resource = new Item($rule, $transformer, 'rules'); - - return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); - } - - /** - * @param RuleTestRequest $request - * @param Rule $rule - * - * @return JsonResponse - * @throws FireflyException - */ - public function testRule(RuleTestRequest $request, Rule $rule): JsonResponse - { - $parameters = $request->getTestParameters(); - - /** @var RuleEngineInterface $ruleEngine */ - $ruleEngine = app(RuleEngineInterface::class); - $ruleEngine->setRules(new Collection([$rule])); - - - // overrule the rule(s) if necessary. - if (array_key_exists('start', $parameters) && null !== $parameters['start']) { - // add a range: - $ruleEngine->addOperator(['type' => 'date_after', 'value' => $parameters['start']->format('Y-m-d')]); - } - - if (array_key_exists('end', $parameters) && null !== $parameters['end']) { - // add a range: - $ruleEngine->addOperator(['type' => 'date_before', 'value' => $parameters['end']->format('Y-m-d')]); - } - if (array_key_exists('accounts', $parameters) && '' !== $parameters['accounts']) { - $ruleEngine->addOperator(['type' => 'account_id', 'value' => $parameters['accounts']]); - } - - // file the rule(s) - $transactions = $ruleEngine->find(); - $count = $transactions->count(); - - $paginator = new LengthAwarePaginator($transactions, $count, 31337, $this->parameters->get('page')); - $paginator->setPath(route('api.v1.rules.test', [$rule->id]) . $this->buildParams()); - - // resulting list is presented as JSON thing. - $manager = $this->getManager(); - /** @var TransactionGroupTransformer $transformer */ - $transformer = app(TransactionGroupTransformer::class); - $transformer->setParameters($this->parameters); - - $resource = new FractalCollection($transactions, $transformer, 'transactions'); - $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); - - return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); - } - - /** - * Execute the given rule group on a set of existing transactions. - * - * @param RuleTriggerRequest $request - * @param Rule $rule - * - * @return JsonResponse - */ - public function triggerRule(RuleTriggerRequest $request, Rule $rule): JsonResponse - { - // Get parameters specified by the user - $parameters = $request->getTriggerParameters(); - - /** @var RuleEngineInterface $ruleEngine */ - $ruleEngine = app(RuleEngineInterface::class); - $ruleEngine->setRules(new Collection([$rule])); - - // overrule the rule(s) if necessary. - if (array_key_exists('start', $parameters) && null !== $parameters['start']) { - // add a range: - $ruleEngine->addOperator(['type' => 'date_after', 'value' => $parameters['start']->format('Y-m-d')]); - } - - if (array_key_exists('end', $parameters) && null !== $parameters['end']) { - // add a range: - $ruleEngine->addOperator(['type' => 'date_before', 'value' => $parameters['end']->format('Y-m-d')]); - } - if (array_key_exists('accounts', $parameters) && '' !== $parameters['accounts']) { - $ruleEngine->addOperator(['type' => 'account_id', 'value' => $parameters['accounts']]); - } - - // file the rule(s) - $ruleEngine->fire(); - - return response()->json([], 204); - } - - /** - * Update a rule. - * - * @param RuleUpdateRequest $request - * @param Rule $rule - * - * @return JsonResponse - */ - public function update(RuleUpdateRequest $request, Rule $rule): JsonResponse - { - $data = $request->getAll(); - $rule = $this->ruleRepository->update($rule, $data); - $manager = $this->getManager(); - - /** @var RuleTransformer $transformer */ - $transformer = app(RuleTransformer::class); - $transformer->setParameters($this->parameters); - - $resource = new Item($rule, $transformer, 'rules'); - - return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); - } -} diff --git a/app/Api/V1/Requests/todo/RuleStoreRequest.php b/app/Api/V1/Requests/Models/Rule/StoreRequest.php similarity index 96% rename from app/Api/V1/Requests/todo/RuleStoreRequest.php rename to app/Api/V1/Requests/Models/Rule/StoreRequest.php index 51e0c1da1e..3bbe2dac39 100644 --- a/app/Api/V1/Requests/todo/RuleStoreRequest.php +++ b/app/Api/V1/Requests/Models/Rule/StoreRequest.php @@ -21,7 +21,7 @@ declare(strict_types=1); -namespace FireflyIII\Api\V1\Requests; +namespace FireflyIII\Api\V1\Requests\Models\Rule; use FireflyIII\Rules\IsBoolean; use FireflyIII\Support\Request\ChecksLogin; @@ -33,9 +33,9 @@ use function is_array; /** - * Class RuleStoreRequest + * Class StoreRequest */ -class RuleStoreRequest extends FormRequest +class StoreRequest extends FormRequest { use ConvertsDataTypes, GetRuleConfiguration, ChecksLogin; @@ -178,7 +178,7 @@ class RuleStoreRequest extends FormRequest $data = $validator->getData(); $triggers = $data['triggers'] ?? []; // need at least one trigger - if (0 === count($triggers)) { + if (!is_countable($triggers) || 0 === count($triggers)) { $validator->errors()->add('title', (string) trans('validation.at_least_one_trigger')); } } @@ -193,7 +193,7 @@ class RuleStoreRequest extends FormRequest $data = $validator->getData(); $actions = $data['actions'] ?? []; // need at least one trigger - if (0 === count($actions)) { + if (!is_countable($actions) || 0 === count($actions)) { $validator->errors()->add('title', (string) trans('validation.at_least_one_action')); } } diff --git a/app/Api/V1/Requests/todo/RuleTestRequest.php b/app/Api/V1/Requests/Models/Rule/TestRequest.php similarity index 87% rename from app/Api/V1/Requests/todo/RuleTestRequest.php rename to app/Api/V1/Requests/Models/Rule/TestRequest.php index 1b0e5acad0..bcfb564c37 100644 --- a/app/Api/V1/Requests/todo/RuleTestRequest.php +++ b/app/Api/V1/Requests/Models/Rule/TestRequest.php @@ -22,7 +22,7 @@ declare(strict_types=1); -namespace FireflyIII\Api\V1\Requests; +namespace FireflyIII\Api\V1\Requests\Models\Rule; use Carbon\Carbon; @@ -31,14 +31,13 @@ use FireflyIII\Support\Request\ConvertsDataTypes; use Illuminate\Foundation\Http\FormRequest; /** - * Class RuleTestRequest + * Class TestRequest */ -class RuleTestRequest extends FormRequest +class TestRequest extends FormRequest { use ConvertsDataTypes, ChecksLogin; - /** * @return array */ @@ -58,7 +57,7 @@ class RuleTestRequest extends FormRequest */ private function getPage(): int { - return 0 === (int) $this->query('page') ? 1 : (int) $this->query('page'); + return 0 === (int)$this->query('page') ? 1 : (int)$this->query('page'); } @@ -73,11 +72,11 @@ class RuleTestRequest extends FormRequest } /** - * @return string + * @return array */ - private function getAccounts(): string + private function getAccounts(): array { - return (string) $this->query('accounts'); + return $this->get('accounts'); } /** diff --git a/app/Api/V1/Requests/todo/RuleTriggerRequest.php b/app/Api/V1/Requests/Models/Rule/TriggerRequest.php similarity index 89% rename from app/Api/V1/Requests/todo/RuleTriggerRequest.php rename to app/Api/V1/Requests/Models/Rule/TriggerRequest.php index d3e8adc892..8fe6b92fcf 100644 --- a/app/Api/V1/Requests/todo/RuleTriggerRequest.php +++ b/app/Api/V1/Requests/Models/Rule/TriggerRequest.php @@ -22,7 +22,7 @@ declare(strict_types=1); -namespace FireflyIII\Api\V1\Requests; +namespace FireflyIII\Api\V1\Requests\Models\Rule; use Carbon\Carbon; @@ -31,9 +31,9 @@ use FireflyIII\Support\Request\ConvertsDataTypes; use Illuminate\Foundation\Http\FormRequest; /** - * Class RuleTriggerRequest + * Class TriggerRequest */ -class RuleTriggerRequest extends FormRequest +class TriggerRequest extends FormRequest { use ConvertsDataTypes, ChecksLogin; @@ -62,11 +62,11 @@ class RuleTriggerRequest extends FormRequest } /** - * @return string + * @return array */ - private function getAccounts(): string + private function getAccounts(): array { - return (string) $this->query('accounts'); + return $this->get('accounts'); } /** diff --git a/app/Api/V1/Requests/todo/RuleUpdateRequest.php b/app/Api/V1/Requests/Models/Rule/UpdateRequest.php similarity index 98% rename from app/Api/V1/Requests/todo/RuleUpdateRequest.php rename to app/Api/V1/Requests/Models/Rule/UpdateRequest.php index 0c2ba23ad7..ae41e657f9 100644 --- a/app/Api/V1/Requests/todo/RuleUpdateRequest.php +++ b/app/Api/V1/Requests/Models/Rule/UpdateRequest.php @@ -21,7 +21,7 @@ declare(strict_types=1); -namespace FireflyIII\Api\V1\Requests; +namespace FireflyIII\Api\V1\Requests\Models\Rule; use FireflyIII\Rules\IsBoolean; use FireflyIII\Support\Request\ChecksLogin; @@ -33,14 +33,12 @@ use function is_array; /** - * Class RuleUpdateRequest + * Class UpdateRequest */ -class RuleUpdateRequest extends FormRequest +class UpdateRequest extends FormRequest { use ConvertsDataTypes, GetRuleConfiguration, ChecksLogin; - - /** * Get all data from the request. * diff --git a/routes/api.php b/routes/api.php index f0aaa1dd65..ded09db1f3 100644 --- a/routes/api.php +++ b/routes/api.php @@ -322,6 +322,26 @@ Route::group( } ); +// Rules API routes: +Route::group( + ['namespace' => 'FireflyIII\Api\V1\Controllers\Models\Rule', 'prefix' => 'rules', + 'as' => 'api.v1.rules.',], + static function () { + + Route::get('', ['uses' => 'ShowController@index', 'as' => 'index']); + Route::post('', ['uses' => 'StoreController@store', 'as' => 'store']); + Route::get('{rule}', ['uses' => 'ShowController@show', 'as' => 'show']); + Route::put('{rule}', ['uses' => 'UpdateController@update', 'as' => 'update']); + Route::delete('{rule}', ['uses' => 'DestroyController@destroy', 'as' => 'delete']); + + // TODO verify if accounts works. + Route::get('{rule}/test', ['uses' => 'TriggerController@testRule', 'as' => 'test']); + // TODO give results back. + Route::post('{rule}/trigger', ['uses' => 'TriggerController@triggerRule', 'as' => 'trigger']); + + // TODO rule transactions, rule bills? + } +); @@ -493,24 +513,7 @@ Route::group( -// TODO VERIFY API DOCS -Route::group( - ['namespace' => 'FireflyIII\Api\V1\Controllers', 'prefix' => 'rules', - 'as' => 'api.v1.rules.',], - static function () { - // Rules API routes: - Route::get('', ['uses' => 'RuleController@index', 'as' => 'index']); - Route::post('', ['uses' => 'RuleController@store', 'as' => 'store']); - Route::get('{rule}', ['uses' => 'RuleController@show', 'as' => 'show']); - Route::put('{rule}', ['uses' => 'RuleController@update', 'as' => 'update']); - Route::delete('{rule}', ['uses' => 'RuleController@delete', 'as' => 'delete']); - Route::get('{rule}/test', ['uses' => 'RuleController@testRule', 'as' => 'test']); - Route::post('{rule}/trigger', ['uses' => 'RuleController@triggerRule', 'as' => 'trigger']); - Route::post('{rule}/up', ['uses' => 'RuleController@moveUp', 'as' => 'up']); - Route::post('{rule}/down', ['uses' => 'RuleController@moveDown', 'as' => 'down']); - } -); // TODO VERIFY API DOCS Route::group(