From 3217b68f8e7b7b2dc1fba2f07cef4c5924074767 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 29 Apr 2023 09:44:34 +1000 Subject: [PATCH] Add group setting filters --- app/Filters/BankIntegrationFilters.php | 4 +- app/Filters/GroupSettingFilters.php | 79 +++++++++++++++++++ .../Controllers/GroupSettingController.php | 43 +--------- app/Models/GroupSetting.php | 6 +- tests/Feature/GroupSettingTest.php | 37 +++++++++ 5 files changed, 125 insertions(+), 44 deletions(-) create mode 100644 app/Filters/GroupSettingFilters.php diff --git a/app/Filters/BankIntegrationFilters.php b/app/Filters/BankIntegrationFilters.php index f0a6aa117c..f637afd9cd 100644 --- a/app/Filters/BankIntegrationFilters.php +++ b/app/Filters/BankIntegrationFilters.php @@ -86,7 +86,7 @@ class BankIntegrationFilters extends QueryFilters /** * Sorts the list based on $sort. * - * @param string sort formatted as column|asc + * @param string $sort formatted as column|asc * @return Builder */ public function sort(string $sort = ''): Builder @@ -103,7 +103,7 @@ class BankIntegrationFilters extends QueryFilters /** * Filters the query by the users company ID. * - * @return Illuminate\Database\Query\Builder + * @return Builder */ public function entityFilter(): Builder { diff --git a/app/Filters/GroupSettingFilters.php b/app/Filters/GroupSettingFilters.php new file mode 100644 index 0000000000..0ea7512f50 --- /dev/null +++ b/app/Filters/GroupSettingFilters.php @@ -0,0 +1,79 @@ +builder; + } + + return $this->builder->where('name', 'like', '%'.$name.'%'); + } + + /** + * Filter based on search text. + * + * @param string $filter + * @return Builder + */ + public function filter(string $filter = ''): Builder + { + if (strlen($filter) == 0) { + return $this->builder; + } + + return $this->builder->where(function ($query) use ($filter) { + $query->where('name', 'like', '%'.$filter.'%'); + }); + } + + /** + * Sorts the list based on $sort. + * + * @param string $sort formatted as column|asc + * @return Builder + */ + public function sort(string $sort = ''): Builder + { + $sort_col = explode('|', $sort); + + if (!is_array($sort_col) || count($sort_col) != 2) { + return $this->builder; + } + + return $this->builder->orderBy($sort_col[0], $sort_col[1]); + } + + /** + * Filters the query by the users company ID. + * + * @return Builder + */ + public function entityFilter(): Builder + { + return $this->builder->company(); + } +} diff --git a/app/Http/Controllers/GroupSettingController.php b/app/Http/Controllers/GroupSettingController.php index bafcdd17c7..0cf062af1c 100644 --- a/app/Http/Controllers/GroupSettingController.php +++ b/app/Http/Controllers/GroupSettingController.php @@ -12,6 +12,7 @@ namespace App\Http\Controllers; use App\Factory\GroupSettingFactory; +use App\Filters\GroupSettingFilters; use App\Http\Requests\GroupSetting\CreateGroupSettingRequest; use App\Http\Requests\GroupSetting\DestroyGroupSettingRequest; use App\Http\Requests\GroupSetting\EditGroupSettingRequest; @@ -49,47 +50,9 @@ class GroupSettingController extends BaseController $this->group_setting_repo = $group_setting_repo; } - /** - * Display a listing of the resource. - * - * @return Response - * - * - * @OA\Get( - * path="/api/v1/group_settings", - * operationId="getGroupSettings", - * tags={"group_settings"}, - * summary="Gets a list of group_settings", - * description="Lists group_settings, search and filters allow fine grained lists to be generated. - - Query parameters can be added to performed more fine grained filtering of the group_settings, these are handled by the GroupSettingFilters class which defines the methods available", - * @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"), - * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), - * @OA\Parameter(ref="#/components/parameters/include"), - * @OA\Response( - * response=200, - * description="A list of group_settings", - * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), - * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), - * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), - * @OA\JsonContent(ref="#/components/schemas/GroupSetting"), - * ), - * @OA\Response( - * response=422, - * description="Validation error", - * @OA\JsonContent(ref="#/components/schemas/ValidationError"), - - * ), - * @OA\Response( - * response="default", - * description="Unexpected Error", - * @OA\JsonContent(ref="#/components/schemas/Error"), - * ), - * ) - */ - public function index() + public function index(GroupSettingFilters $filters) { - $group_settings = GroupSetting::whereCompanyId(auth()->user()->company()->id); + $group_settings = GroupSetting::filter($filters); return $this->listResponse($group_settings); } diff --git a/app/Models/GroupSetting.php b/app/Models/GroupSetting.php index d5271b9627..fb6b52b651 100644 --- a/app/Models/GroupSetting.php +++ b/app/Models/GroupSetting.php @@ -11,10 +11,11 @@ namespace App\Models; +use App\Models\Filterable; use App\Utils\Traits\MakesHash; use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException; use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException; /** * App\Models\GroupSetting @@ -61,7 +62,8 @@ class GroupSetting extends StaticModel { use MakesHash; use SoftDeletes; - + use Filterable; + protected $casts = [ 'settings' => 'object', 'updated_at' => 'timestamp', diff --git a/tests/Feature/GroupSettingTest.php b/tests/Feature/GroupSettingTest.php index d200289be5..944edbe2bf 100644 --- a/tests/Feature/GroupSettingTest.php +++ b/tests/Feature/GroupSettingTest.php @@ -38,6 +38,43 @@ class GroupSettingTest extends TestCase $this->makeTestData(); } + + public function testAddGroupFilters() + { + $settings = new \stdClass; + $settings->currency_id = '1'; + + $data = [ + 'name' => 'testX', + 'settings' => $settings, + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/group_settings', $data); + + $response->assertStatus(200); + + $arr = $response->json(); + + $this->assertEquals('testX', $arr['data']['name']); + $this->assertEquals(0, $arr['data']['archived_at']); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/group_settings?name=fdfdfd'); + + $response->assertStatus(200); + + $arr = $response->json(); + + $this->assertCount(0, $arr['data']); + + } + + public function testAddGroupSettings() { $settings = new \stdClass;