|
3 | 3 | namespace App\Services;
|
4 | 4 |
|
5 | 5 | use App\Http\Resources\CategoryResource;
|
| 6 | +use App\Http\Resources\ProductNotFoundResource; |
| 7 | +use App\Http\Resources\ProductResource; |
| 8 | +use App\Models\Product; |
6 | 9 | use App\Repositories\CategoryRepositoryInterface;
|
7 | 10 | use App\Repositories\ProductRepositoryInterface;
|
8 | 11 | use Goutte\Client;
|
9 | 12 | use Illuminate\Http\JsonResponse;
|
| 13 | +use Illuminate\Http\Resources\Json\JsonResource; |
10 | 14 | use NumberFormatter;
|
11 | 15 |
|
12 | 16 | class ProductService
|
@@ -89,28 +93,25 @@ public function handleScrapeProduct(string $asin): bool
|
89 | 93 | *
|
90 | 94 | * @param String $asin
|
91 | 95 | *
|
92 |
| - * @return JsonResponse |
| 96 | + * @return JsonResource |
93 | 97 | */
|
94 |
| - public function getProduct(string $asin): JsonResponse |
| 98 | + public function getProduct(string $asin): JsonResource |
95 | 99 | {
|
96 | 100 | try {
|
97 | 101 | $product = $this->productRepository->getProduct($asin);
|
98 | 102 |
|
99 | 103 | $categories = $this->categoryRepository->getAllParent($product->category);
|
100 | 104 |
|
101 |
| - return response()->json([ |
102 |
| - 'asin' => $product->asin, |
103 |
| - 'name' => $product->name, |
104 |
| - 'price' => $product->last_price, |
105 |
| - 'category' => new CategoryResource($product->category), |
106 |
| - 'categories' => $categories |
107 |
| - ], 200); |
| 105 | + return new ProductResource([ |
| 106 | + 'product' => $product, |
| 107 | + 'categories' => array_reverse($categories) |
| 108 | + ]); |
108 | 109 | } catch (\Exception $e) {
|
109 | 110 | logger($e->getMessage());
|
110 | 111 |
|
111 |
| - return response()->json([ |
112 |
| - 'message' => 'Product not found' |
113 |
| - ], 404); |
| 112 | + return new ProductNotFoundResource([ |
| 113 | + 'asin' => $asin |
| 114 | + ]); |
114 | 115 | }
|
115 | 116 | }
|
116 | 117 | }
|
0 commit comments