-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
60. api resource for catalog products
- Loading branch information
1 parent
65f73d7
commit 5b7a40f
Showing
9 changed files
with
138 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/app/Http/Resources/Product/CatalogProductCollection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources\Product; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\ResourceCollection; | ||
|
||
class CatalogProductCollection extends ResourceCollection | ||
{ | ||
/** | ||
* The resource that this resource collects. | ||
* | ||
* @var string | ||
*/ | ||
public $collects = CatalogProductResource::class; | ||
|
||
/** | ||
* Transform the resource collection into an array. | ||
* | ||
* @return array<int|string, mixed> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
return [ | ||
...$this->resource->toArray($request), | ||
'total' => $this->resource->totalCount, | ||
'minPrice' => $this->resource->minPrice, | ||
'maxPrice' => $this->resource->maxPrice, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources\Product; | ||
|
||
use App\Facades\Currency; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
/** | ||
* @mixin \App\Models\Product | ||
*/ | ||
class CatalogProductResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'slug' => $this->slug, | ||
'sku' => $this->sku, | ||
'url' => $this->getUrl(), | ||
'prices' => [ | ||
'price' => $this->getFinalPrice(), | ||
'old_price' => $this->getFinalOldPrice(), | ||
'formatted_price' => $this->getFormattedPrice(), | ||
'formatted_old_price' => $this->getFormattedOldPrice(), | ||
'has_discount' => $this->hasDiscount(), | ||
'sale_percentage' => $this->getSalePercentage(), | ||
'sales' => $this->getSales(), | ||
'currency' => Currency::getCurrentCurrency(), | ||
], | ||
|
||
'is_favorite' => $this->isFavorite(), | ||
'is_new' => $this->isNew(), | ||
'short_name' => $this->shortName(), | ||
|
||
'media' => MediaResource::collection($this->getMedia()), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters