From 991b2ce7b65cd3370f4a0efc16092f768df8405b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Thu, 25 Nov 2021 16:27:59 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=95=86=E5=93=81=E5=88=86?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Api/Filters/ProductCategoryFilter.php | 18 +++++ .../Controllers/ProductCategoryController.php | 23 ++++++ .../Api/Http/Middleware/ConfigModelFilter.php | 23 ++++++ .../Resources/ProductCategoryResource.php | 23 ++++++ app/Endpoint/Api/routes.php | 8 +- app/Models/ProductCategory.php | 4 + composer.json | 3 +- composer.lock | 73 ++++++++++++++++++- 8 files changed, 172 insertions(+), 3 deletions(-) create mode 100644 app/Endpoint/Api/Filters/ProductCategoryFilter.php create mode 100644 app/Endpoint/Api/Http/Controllers/ProductCategoryController.php create mode 100644 app/Endpoint/Api/Http/Middleware/ConfigModelFilter.php create mode 100644 app/Endpoint/Api/Http/Resources/ProductCategoryResource.php diff --git a/app/Endpoint/Api/Filters/ProductCategoryFilter.php b/app/Endpoint/Api/Filters/ProductCategoryFilter.php new file mode 100644 index 00000000..7db88136 --- /dev/null +++ b/app/Endpoint/Api/Filters/ProductCategoryFilter.php @@ -0,0 +1,18 @@ +where('parent_id', $pid); + } + + public function setup() + { + $this->showable(); + } +} diff --git a/app/Endpoint/Api/Http/Controllers/ProductCategoryController.php b/app/Endpoint/Api/Http/Controllers/ProductCategoryController.php new file mode 100644 index 00000000..a0cde45d --- /dev/null +++ b/app/Endpoint/Api/Http/Controllers/ProductCategoryController.php @@ -0,0 +1,23 @@ +all())->latest('sort')->get(); + + return ProductCategoryResource::collection($categories); + } +} diff --git a/app/Endpoint/Api/Http/Middleware/ConfigModelFilter.php b/app/Endpoint/Api/Http/Middleware/ConfigModelFilter.php new file mode 100644 index 00000000..41ac67f1 --- /dev/null +++ b/app/Endpoint/Api/Http/Middleware/ConfigModelFilter.php @@ -0,0 +1,23 @@ + 'App\\Endpoint\\Api\\Filters\\']); + + return $next($request); + } +} diff --git a/app/Endpoint/Api/Http/Resources/ProductCategoryResource.php b/app/Endpoint/Api/Http/Resources/ProductCategoryResource.php new file mode 100644 index 00000000..52570aa4 --- /dev/null +++ b/app/Endpoint/Api/Http/Resources/ProductCategoryResource.php @@ -0,0 +1,23 @@ + $this->id, + 'name' => $this->name, + 'icon' => (string) $this->icon, + ]; + } +} diff --git a/app/Endpoint/Api/routes.php b/app/Endpoint/Api/routes.php index 58d3ed2a..1601ffda 100644 --- a/app/Endpoint/Api/routes.php +++ b/app/Endpoint/Api/routes.php @@ -4,13 +4,17 @@ use App\Endpoint\Api\Http\Controllers\AdController; use App\Endpoint\Api\Http\Controllers\CaptchaController; use App\Endpoint\Api\Http\Controllers\LoginController; use App\Endpoint\Api\Http\Controllers\LogoutController; +use App\Endpoint\Api\Http\Controllers\ProductCategoryController; use App\Endpoint\Api\Http\Controllers\RegisterController; use App\Endpoint\Api\Http\Controllers\SmsCodeController; use Illuminate\Support\Facades\Route; Route::group([ 'prefix' => 'v1', - 'middleware' => ['guard:api'], + 'middleware' => [ + 'guard:api', + \App\Endpoint\Api\Http\Middleware\ConfigModelFilter::class, + ], ], function () { Route::post('captchas', [CaptchaController::class, 'store']); Route::get('captchas/{captcha}', [CaptchaController::class, 'show']); @@ -21,4 +25,6 @@ Route::group([ Route::post('register', RegisterController::class); Route::get('ads', [AdController::class, 'index']); + + Route::get('product-categories', [ProductCategoryController::class, 'index']); }); diff --git a/app/Models/ProductCategory.php b/app/Models/ProductCategory.php index 06c468d5..e35eddb5 100644 --- a/app/Models/ProductCategory.php +++ b/app/Models/ProductCategory.php @@ -5,14 +5,18 @@ namespace App\Models; use Dcat\Admin\Traits\HasDateTimeFormatter; use Dcat\Admin\Traits\ModelTree; +use EloquentFilter\Filterable; use Illuminate\Database\Eloquent\Model; use Kalnoy\Nestedset\NodeTrait; class ProductCategory extends Model { + use Concerns\HasShowable; + use Filterable; use NodeTrait; use ModelTree; use HasDateTimeFormatter; + protected $table = 'product_categories'; protected $casts = [ diff --git a/composer.json b/composer.json index 255000ee..a61a13c2 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,8 @@ "laravel/framework": "^8.65", "laravel/sanctum": "^2.12", "laravel/tinker": "^2.5", - "overtrue/easy-sms": "^2.0" + "overtrue/easy-sms": "^2.0", + "tucker-eric/eloquentfilter": "^3.0" }, "require-dev": { "facade/ignition": "^2.5", diff --git a/composer.lock b/composer.lock index 2c5f946f..6394518e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "d93e7912aa10ac47d7b1923ce5dd28aa", + "content-hash": "bf84da9e525e883eda04018255f63cda", "packages": [ { "name": "asm89/stack-cors", @@ -6366,6 +6366,77 @@ }, "time": "2020-07-13T06:12:54+00:00" }, + { + "name": "tucker-eric/eloquentfilter", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/Tucker-Eric/EloquentFilter.git", + "reference": "050dd36f788edaa413a5e5e16748a575df040bca" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Tucker-Eric/EloquentFilter/zipball/050dd36f788edaa413a5e5e16748a575df040bca", + "reference": "050dd36f788edaa413a5e5e16748a575df040bca", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "illuminate/config": "~6.0|~7.0|~8.0", + "illuminate/console": "~6.0|~7.0|~8.0", + "illuminate/database": "~6.0|~7.0|~8.0", + "illuminate/filesystem": "~6.0|~7.0|~8.0", + "illuminate/pagination": "~6.0|~7.0|~8.0", + "illuminate/support": "~6.0|~7.0|~8.0", + "php": ">=7.2" + }, + "require-dev": { + "mockery/mockery": "^1.3", + "phpunit/phpunit": "^8" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "EloquentFilter\\ServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "EloquentFilter\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Eric Tucker", + "email": "tucker.ericm@gmail.com" + } + ], + "description": "An Eloquent way to filter Eloquent Models", + "keywords": [ + "eloquent", + "filter", + "laravel", + "model", + "query", + "search" + ], + "support": { + "issues": "https://github.com/Tucker-Eric/EloquentFilter/issues", + "source": "https://github.com/Tucker-Eric/EloquentFilter/tree/3.0.0" + }, + "time": "2021-05-12T16:08:43+00:00" + }, { "name": "vlucas/phpdotenv", "version": "v5.4.0",