Skip to content

Commit

Permalink
improved APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
A1Gard committed Dec 30, 2024
1 parent bbae595 commit ca31027
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 55 deletions.
51 changes: 5 additions & 46 deletions app/Http/Controllers/Api/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/AdvResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function toArray(Request $request): array
*/
return [
'id' => $this->id,
'image' => $this->imgUrl,
'image' => $this->imgUrl(),
'title' => $this->title,
'link' => $this->link,
];
Expand Down
38 changes: 38 additions & 0 deletions app/Http/Resources/CategoriesCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Http\Resources;

use App\Models\Category;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;

class CategoriesCollection extends JsonResource
{
/**
* Transform the resource collection into an array.
*
* @return array<int|string, mixed>
*/
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,
];
}
}
11 changes: 8 additions & 3 deletions app/Http/Resources/CategoryResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ public function toArray(Request $request, $data = null): array
'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) ),
];
}
}
10 changes: 8 additions & 2 deletions app/Http/Resources/CityCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;

use App\Models\City;
class CityCollection extends JsonResource
{
/**
Expand All @@ -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,
];
}
}
1 change: 1 addition & 0 deletions app/Http/Resources/PostResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function toArray(Request $request): array
'dislike' => $this->dislike,
'icon' => $this->icon,
'created_at' => $this->created_at,
'image' => $this->imgUrl(),

];
}
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Resources/ProductCardCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +16,9 @@ class ProductCardCollection extends JsonResource
*/
public function toArray(Request $request): array
{
/**
* @var $this Product
*/
return [

'id'=> $this->id,
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Resources/ProductResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public function toArray(Request $request): array
'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(),
];
}
}
5 changes: 5 additions & 0 deletions app/Http/Resources/PropCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +16,10 @@ class PropCollection extends JsonResource
*/
public function toArray(Request $request): array
{

/**
* @var $this Prop
*/
return [
'id' => $this->id,
'name' => $this->name,
Expand Down
6 changes: 5 additions & 1 deletion app/Http/Resources/QunatityCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,14 +16,17 @@ class QunatityCollection extends JsonResource
*/
public function toArray(Request $request): array
{
/**
* @var $this Quantity
*/
return [
'id' => $this->id,
'product_name' => $this->product->name,
'count' => $this->count,
'data'=> json_decode($this->data),
'meta' => $this->meta,
'price'=> $this->price,
'image' => $this->image,
'image' => $this->product->getMedia()[$this->image]->getUrl('product-image'),
];
}
}
1 change: 1 addition & 0 deletions app/Http/Resources/SliderResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function toArray(Request $request): array
'id' => $this->id,
'body' => $this->body,
'image' => $this->imgUrl(),
'image_original' => $this->imgOriginalUrl(),
'tag' => $this->tag,
'user_id' => $this->user_id,
'status' => $this->status,
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Resources/StateCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +16,9 @@ class StateCollection extends JsonResource
*/
public function toArray(Request $request): array
{
/**
* @var $this State
*/
return [
'id' => $this->id,
'name' => $this->name,
Expand Down
5 changes: 5 additions & 0 deletions app/Http/Resources/TransportCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +16,10 @@ class TransportCollection extends JsonResource
*/
public function toArray(Request $request): array
{

/**
* @var $this Transport
*/
return [
'id' => $this->id,
'title' => $this->title,
Expand Down
6 changes: 5 additions & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ function () {

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');



});

0 comments on commit ca31027

Please sign in to comment.