improved APIs

master
A1Gard 2 weeks ago
parent bbae5955d5
commit ca310273b8

@ -3,6 +3,8 @@
namespace App\Http\Controllers\Api; namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Resources\CategoriesCollection;
use App\Http\Resources\CategoryResource;
use App\Http\Resources\PropCollection; use App\Http\Resources\PropCollection;
use App\Models\Category; use App\Models\Category;
use App\Models\Prop; use App\Models\Prop;
@ -15,56 +17,13 @@ class CategoryController extends Controller
*/ */
public function index() public function index()
{ {
// return CategoriesCollection::collection(Category::orderBy('sort', 'asc')->get());
} }
/** public function show(Category $category){
* Show the form for creating a new resource. return new CategoryResource($category);
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
} }
/**
* 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){ public function props( $id){
$category = Category::whereId($id)->firstOrFail(); $category = Category::whereId($id)->firstOrFail();

@ -20,7 +20,7 @@ class AdvResource extends JsonResource
*/ */
return [ return [
'id' => $this->id, 'id' => $this->id,
'image' => $this->imgUrl, 'image' => $this->imgUrl(),
'title' => $this->title, 'title' => $this->title,
'link' => $this->link, 'link' => $this->link,
]; ];

@ -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,
];
}
}

@ -33,9 +33,14 @@ class CategoryResource extends JsonResource
'subtitle' => $this->subtitle, 'subtitle' => $this->subtitle,
'description' => $this->description, 'description' => $this->description,
'sort' => $this->sort, 'sort' => $this->sort,
'image' => $this->image, 'image' => $this->imgUrl(),
'bg' => $this->bg, 'image_original' => $this->imgOriginalUrl(),
'products' => $this->when($request->input('loadProduct', true), ProductResource::collection($this->products)->additional(['request' => $request['loadCategory']])) '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) ),
]; ];
} }
} }

@ -5,7 +5,7 @@ namespace App\Http\Resources;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection; use Illuminate\Http\Resources\Json\ResourceCollection;
use App\Models\City;
class CityCollection extends JsonResource class CityCollection extends JsonResource
{ {
/** /**
@ -15,11 +15,17 @@ class CityCollection extends JsonResource
*/ */
public function toArray(Request $request): array public function toArray(Request $request): array
{ {
/**
* @var $this City
*/
return [ return [
'id' => $this->id, 'id' => $this->id,
'name' => $this->name, 'name' => $this->name,
'lat' => $this->lat, 'lat' => $this->lat,
'lng' => $this->lng 'lng' => $this->lng,
'state' => $this->state,
]; ];
} }
} }

@ -32,6 +32,7 @@ class PostResource extends JsonResource
'dislike' => $this->dislike, 'dislike' => $this->dislike,
'icon' => $this->icon, 'icon' => $this->icon,
'created_at' => $this->created_at, 'created_at' => $this->created_at,
'image' => $this->imgUrl(),
]; ];
} }

@ -2,6 +2,7 @@
namespace App\Http\Resources; namespace App\Http\Resources;
use App\Models\Product;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection; use Illuminate\Http\Resources\Json\ResourceCollection;
@ -15,6 +16,9 @@ class ProductCardCollection extends JsonResource
*/ */
public function toArray(Request $request): array public function toArray(Request $request): array
{ {
/**
* @var $this Product
*/
return [ return [
'id'=> $this->id, 'id'=> $this->id,

@ -37,7 +37,9 @@ class ProductResource extends JsonResource
'average_rating' => floatval($this->average_rating), 'average_rating' => floatval($this->average_rating),
'view' => $this->view, 'view' => $this->view,
'category' => $this->when($request->input('loadCategory', true), new CategoryResource($this->category)), '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(),
]; ];
} }
} }

@ -2,6 +2,7 @@
namespace App\Http\Resources; namespace App\Http\Resources;
use App\Models\Prop;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection; use Illuminate\Http\Resources\Json\ResourceCollection;
@ -15,6 +16,10 @@ class PropCollection extends JsonResource
*/ */
public function toArray(Request $request): array public function toArray(Request $request): array
{ {
/**
* @var $this Prop
*/
return [ return [
'id' => $this->id, 'id' => $this->id,
'name' => $this->name, 'name' => $this->name,

@ -2,6 +2,7 @@
namespace App\Http\Resources; namespace App\Http\Resources;
use App\Models\Quantity;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection; use Illuminate\Http\Resources\Json\ResourceCollection;
@ -15,6 +16,9 @@ class QunatityCollection extends JsonResource
*/ */
public function toArray(Request $request): array public function toArray(Request $request): array
{ {
/**
* @var $this Quantity
*/
return [ return [
'id' => $this->id, 'id' => $this->id,
'product_name' => $this->product->name, 'product_name' => $this->product->name,
@ -22,7 +26,7 @@ class QunatityCollection extends JsonResource
'data'=> json_decode($this->data), 'data'=> json_decode($this->data),
'meta' => $this->meta, 'meta' => $this->meta,
'price'=> $this->price, 'price'=> $this->price,
'image' => $this->image, 'image' => $this->product->getMedia()[$this->image]->getUrl('product-image'),
]; ];
} }
} }

@ -22,6 +22,7 @@ class SliderResource extends JsonResource
'id' => $this->id, 'id' => $this->id,
'body' => $this->body, 'body' => $this->body,
'image' => $this->imgUrl(), 'image' => $this->imgUrl(),
'image_original' => $this->imgOriginalUrl(),
'tag' => $this->tag, 'tag' => $this->tag,
'user_id' => $this->user_id, 'user_id' => $this->user_id,
'status' => $this->status, 'status' => $this->status,

@ -2,6 +2,7 @@
namespace App\Http\Resources; namespace App\Http\Resources;
use App\Models\State;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection; use Illuminate\Http\Resources\Json\ResourceCollection;
@ -15,6 +16,9 @@ class StateCollection extends JsonResource
*/ */
public function toArray(Request $request): array public function toArray(Request $request): array
{ {
/**
* @var $this State
*/
return [ return [
'id' => $this->id, 'id' => $this->id,
'name' => $this->name, 'name' => $this->name,

@ -2,6 +2,7 @@
namespace App\Http\Resources; namespace App\Http\Resources;
use App\Models\Transport;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection; use Illuminate\Http\Resources\Json\ResourceCollection;
@ -15,6 +16,10 @@ class TransportCollection extends JsonResource
*/ */
public function toArray(Request $request): array public function toArray(Request $request): array
{ {
/**
* @var $this Transport
*/
return [ return [
'id' => $this->id, 'id' => $this->id,
'title' => $this->title, 'title' => $this->title,

@ -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('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('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::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('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::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('web', \App\Http\Controllers\Api\HomeController::class)->only('index');
Route::apiResource('products' , \App\Http\Controllers\Api\ProductController::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'); Route::get('tag/search/{q}', [\App\Http\Controllers\Api\TagController::class, 'search'])->name('tag.search');
}); });

Loading…
Cancel
Save