From ca310273b81adb676410a932a12d09000408178b Mon Sep 17 00:00:00 2001 From: A1Gard Date: Mon, 30 Dec 2024 19:28:57 +0330 Subject: [PATCH] improved APIs --- .../Controllers/Api/CategoryController.php | 51 ++----------------- app/Http/Resources/AdvResource.php | 2 +- app/Http/Resources/CategoriesCollection.php | 38 ++++++++++++++ app/Http/Resources/CategoryResource.php | 11 ++-- app/Http/Resources/CityCollection.php | 10 +++- app/Http/Resources/PostResource.php | 1 + app/Http/Resources/ProductCardCollection.php | 4 ++ app/Http/Resources/ProductResource.php | 4 +- app/Http/Resources/PropCollection.php | 5 ++ app/Http/Resources/QunatityCollection.php | 6 ++- app/Http/Resources/SliderResource.php | 1 + app/Http/Resources/StateCollection.php | 4 ++ app/Http/Resources/TransportCollection.php | 5 ++ routes/api.php | 6 ++- 14 files changed, 93 insertions(+), 55 deletions(-) create mode 100644 app/Http/Resources/CategoriesCollection.php diff --git a/app/Http/Controllers/Api/CategoryController.php b/app/Http/Controllers/Api/CategoryController.php index 8338fba..17e60f4 100644 --- a/app/Http/Controllers/Api/CategoryController.php +++ b/app/Http/Controllers/Api/CategoryController.php @@ -3,6 +3,8 @@ namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; +use App\Http\Resources\CategoriesCollection; +use App\Http\Resources\CategoryResource; use App\Http\Resources\PropCollection; use App\Models\Category; use App\Models\Prop; @@ -15,56 +17,13 @@ class CategoryController extends Controller */ public function index() { - // + return CategoriesCollection::collection(Category::orderBy('sort', 'asc')->get()); } - /** - * Show the form for creating a new resource. - */ - public function create() - { - // - } - - /** - * Store a newly created resource in storage. - */ - public function store(Request $request) - { - // + public function show(Category $category){ + return new CategoryResource($category); } - /** - * Display the specified resource. - */ - public function show(Category $category) - { - // - } - - /** - * Show the form for editing the specified resource. - */ - public function edit(Category $category) - { - // - } - - /** - * Update the specified resource in storage. - */ - public function update(Request $request, Category $category) - { - // - } - - /** - * Remove the specified resource from storage. - */ - public function destroy(Category $category) - { - // - } public function props( $id){ $category = Category::whereId($id)->firstOrFail(); diff --git a/app/Http/Resources/AdvResource.php b/app/Http/Resources/AdvResource.php index 5cb703a..965815f 100644 --- a/app/Http/Resources/AdvResource.php +++ b/app/Http/Resources/AdvResource.php @@ -20,7 +20,7 @@ class AdvResource extends JsonResource */ return [ 'id' => $this->id, - 'image' => $this->imgUrl, + 'image' => $this->imgUrl(), 'title' => $this->title, 'link' => $this->link, ]; diff --git a/app/Http/Resources/CategoriesCollection.php b/app/Http/Resources/CategoriesCollection.php new file mode 100644 index 0000000..1248c60 --- /dev/null +++ b/app/Http/Resources/CategoriesCollection.php @@ -0,0 +1,38 @@ + + */ + public function toArray(Request $request, $data = null): array + { + /** + * @var $this Category + */ + + return [ + 'id' => $this->id, + 'name' => $this->name, + 'slug' => $this->slug, + 'subtitle' => $this->subtitle, + 'description' => $this->description, + 'sort' => $this->sort, + 'image' => $this->imgUrl(), + 'image_original' => $this->imgOriginalUrl(), + 'bg' => $this->bgUrl(), + 'bg_original' => $this->bgOriginalUrl(), + 'svg' => $this->svgUrl(), + 'icons' => $this->icon, + ]; + } +} diff --git a/app/Http/Resources/CategoryResource.php b/app/Http/Resources/CategoryResource.php index 64c347f..20f8b56 100644 --- a/app/Http/Resources/CategoryResource.php +++ b/app/Http/Resources/CategoryResource.php @@ -33,9 +33,14 @@ class CategoryResource extends JsonResource 'subtitle' => $this->subtitle, 'description' => $this->description, 'sort' => $this->sort, - 'image' => $this->image, - 'bg' => $this->bg, - 'products' => $this->when($request->input('loadProduct', true), ProductResource::collection($this->products)->additional(['request' => $request['loadCategory']])) + 'image' => $this->imgUrl(), + 'image_original' => $this->imgOriginalUrl(), + 'bg' => $this->bgUrl(), + 'bg_original' => $this->bgOriginalUrl(), + 'svg' => $this->svgUrl(), + 'icons' => $this->icon, + 'products' => $this->when($request->input('loadProduct', true), ProductResource::collection($this->products()->paginate($request->input('per_page', 20)))->additional(['request' => $request['loadCategory']])), + 'products_pages_count' => ceil($this->products()->count() / $request->input('per_page', 20) ), ]; } } diff --git a/app/Http/Resources/CityCollection.php b/app/Http/Resources/CityCollection.php index 4ec8818..f581d4c 100644 --- a/app/Http/Resources/CityCollection.php +++ b/app/Http/Resources/CityCollection.php @@ -5,7 +5,7 @@ namespace App\Http\Resources; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\ResourceCollection; - +use App\Models\City; class CityCollection extends JsonResource { /** @@ -15,11 +15,17 @@ class CityCollection extends JsonResource */ public function toArray(Request $request): array { + + /** + * @var $this City + */ + return [ 'id' => $this->id, 'name' => $this->name, 'lat' => $this->lat, - 'lng' => $this->lng + 'lng' => $this->lng, + 'state' => $this->state, ]; } } diff --git a/app/Http/Resources/PostResource.php b/app/Http/Resources/PostResource.php index 74a5795..ad8f8e0 100644 --- a/app/Http/Resources/PostResource.php +++ b/app/Http/Resources/PostResource.php @@ -32,6 +32,7 @@ class PostResource extends JsonResource 'dislike' => $this->dislike, 'icon' => $this->icon, 'created_at' => $this->created_at, + 'image' => $this->imgUrl(), ]; } diff --git a/app/Http/Resources/ProductCardCollection.php b/app/Http/Resources/ProductCardCollection.php index 7901c31..3d45f89 100644 --- a/app/Http/Resources/ProductCardCollection.php +++ b/app/Http/Resources/ProductCardCollection.php @@ -2,6 +2,7 @@ namespace App\Http\Resources; +use App\Models\Product; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\ResourceCollection; @@ -15,6 +16,9 @@ class ProductCardCollection extends JsonResource */ public function toArray(Request $request): array { + /** + * @var $this Product + */ return [ 'id'=> $this->id, diff --git a/app/Http/Resources/ProductResource.php b/app/Http/Resources/ProductResource.php index 3b0ea55..6990f3d 100644 --- a/app/Http/Resources/ProductResource.php +++ b/app/Http/Resources/ProductResource.php @@ -37,7 +37,9 @@ class ProductResource extends JsonResource 'average_rating' => floatval($this->average_rating), 'view' => $this->view, 'category' => $this->when($request->input('loadCategory', true), new CategoryResource($this->category)), - 'image' => $this->imgUrl() + 'categories' => CategoriesCollection::collection($this->categories), + 'image' => $this->imgUrl(), + 'images' => $this->getMedia(), ]; } } diff --git a/app/Http/Resources/PropCollection.php b/app/Http/Resources/PropCollection.php index 58a8577..2296e78 100644 --- a/app/Http/Resources/PropCollection.php +++ b/app/Http/Resources/PropCollection.php @@ -2,6 +2,7 @@ namespace App\Http\Resources; +use App\Models\Prop; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\ResourceCollection; @@ -15,6 +16,10 @@ class PropCollection extends JsonResource */ public function toArray(Request $request): array { + + /** + * @var $this Prop + */ return [ 'id' => $this->id, 'name' => $this->name, diff --git a/app/Http/Resources/QunatityCollection.php b/app/Http/Resources/QunatityCollection.php index e4f8cb3..fc101cb 100644 --- a/app/Http/Resources/QunatityCollection.php +++ b/app/Http/Resources/QunatityCollection.php @@ -2,6 +2,7 @@ namespace App\Http\Resources; +use App\Models\Quantity; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\ResourceCollection; @@ -15,6 +16,9 @@ class QunatityCollection extends JsonResource */ public function toArray(Request $request): array { + /** + * @var $this Quantity + */ return [ 'id' => $this->id, 'product_name' => $this->product->name, @@ -22,7 +26,7 @@ class QunatityCollection extends JsonResource 'data'=> json_decode($this->data), 'meta' => $this->meta, 'price'=> $this->price, - 'image' => $this->image, + 'image' => $this->product->getMedia()[$this->image]->getUrl('product-image'), ]; } } diff --git a/app/Http/Resources/SliderResource.php b/app/Http/Resources/SliderResource.php index cebf359..a7a9b2d 100644 --- a/app/Http/Resources/SliderResource.php +++ b/app/Http/Resources/SliderResource.php @@ -22,6 +22,7 @@ class SliderResource extends JsonResource 'id' => $this->id, 'body' => $this->body, 'image' => $this->imgUrl(), + 'image_original' => $this->imgOriginalUrl(), 'tag' => $this->tag, 'user_id' => $this->user_id, 'status' => $this->status, diff --git a/app/Http/Resources/StateCollection.php b/app/Http/Resources/StateCollection.php index 0583176..e06a96b 100644 --- a/app/Http/Resources/StateCollection.php +++ b/app/Http/Resources/StateCollection.php @@ -2,6 +2,7 @@ namespace App\Http\Resources; +use App\Models\State; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\ResourceCollection; @@ -15,6 +16,9 @@ class StateCollection extends JsonResource */ public function toArray(Request $request): array { + /** + * @var $this State + */ return [ 'id' => $this->id, 'name' => $this->name, diff --git a/app/Http/Resources/TransportCollection.php b/app/Http/Resources/TransportCollection.php index 82a97c3..7cf4049 100644 --- a/app/Http/Resources/TransportCollection.php +++ b/app/Http/Resources/TransportCollection.php @@ -2,6 +2,7 @@ namespace App\Http\Resources; +use App\Models\Transport; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\ResourceCollection; @@ -15,6 +16,10 @@ class TransportCollection extends JsonResource */ public function toArray(Request $request): array { + + /** + * @var $this Transport + */ return [ 'id' => $this->id, 'title' => $this->title, diff --git a/routes/api.php b/routes/api.php index 67c7342..1f5e9f2 100644 --- a/routes/api.php +++ b/routes/api.php @@ -35,13 +35,17 @@ Route::prefix('v1')->name('v1.')->group( Route::get('states', [\App\Http\Controllers\Api\StateController::class, 'index'])->name('state.index'); Route::get('state/{state}', [\App\Http\Controllers\Api\StateController::class, 'show'])->name('state.show'); + Route::get('categories', [\App\Http\Controllers\Api\CategoryController::class, 'index'])->name('category.index'); + Route::get('category/{category}', [\App\Http\Controllers\Api\CategoryController::class, 'show'])->name('category.show'); + Route::get('products', [\App\Http\Controllers\Api\ProductController::class, 'index'])->name('product.index'); Route::get('category/props/{category}', [\App\Http\Controllers\Api\CategoryController::class, 'props'])->name('category.prop'); Route::post('morph/search', [\App\Http\Controllers\Api\MorphController::class, 'search'])->name('morph.search'); Route::post('visitor/display', [\App\Http\Controllers\Api\VisitorController::class, 'display'])->name('visitor.display'); Route::apiResource('web', \App\Http\Controllers\Api\HomeController::class)->only('index'); Route::apiResource('products' , \App\Http\Controllers\Api\ProductController::class)->only('index'); - Route::get('tag/search/{q}', [\App\Http\Controllers\Api\TagController::class, 'search'])->name('tag.search'); + + });