Skip to content

Commit 34bed59

Browse files
committed
feat: add endpoint and action for delete product
1 parent cb2639d commit 34bed59

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

app/Http/Controllers/ProductController.php

+16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use App\Services\ProductService;
99
use Illuminate\Http\JsonResponse;
1010
use Illuminate\Http\Request;
11+
use Illuminate\Http\Resources\Json\JsonResource;
12+
use Illuminate\Support\Facades\Log;
1113

1214
class ProductController extends Controller
1315
{
@@ -69,4 +71,18 @@ public function show(string $asin)
6971
{
7072
return $this->productService->getProduct($asin);
7173
}
74+
75+
76+
/**
77+
* Delete product
78+
*
79+
* @param Product $product
80+
* @return JsonResource
81+
*/
82+
public function destroy(Product $product)
83+
{
84+
$this->productService->deleteProduct($product->asin);
85+
86+
return response()->noContent();
87+
}
7288
}

app/Services/ProductService.php

+19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Http\Resources\ProductCollection;
66
use App\Http\Resources\ProductNotFoundResource;
77
use App\Http\Resources\ProductResource;
8+
use App\Models\Product;
89
use App\Repositories\CategoryRepositoryInterface;
910
use App\Repositories\ProductRepositoryInterface;
1011
use Exception;
@@ -231,4 +232,22 @@ public function findImages(Crawler $html): array
231232

232233
return $images;
233234
}
235+
236+
237+
/**
238+
* Delete aProduct
239+
*
240+
* @param string $asin
241+
* @return bool
242+
*/
243+
public function deleteProduct(string $asin): bool
244+
{
245+
try {
246+
return Product::find($asin)->delete();
247+
} catch (Exception $e) {
248+
Log::error('Delete product Error: ' . $e->getMessage());
249+
}
250+
251+
return false;
252+
}
234253
}

tests/Unit/ProductServiceTest.php

+32-1
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22

33
namespace Tests\Unit;
44

5+
use App\Models\Category;
6+
use App\Models\Price;
57
use App\Models\Product;
68
use App\Repositories\CategoryRepository;
79
use App\Repositories\ProductRepository;
810
use App\Services\ProductService;
911
use Illuminate\Foundation\Testing\RefreshDatabase;
1012
use Illuminate\Support\Facades\Log;
1113
use Mockery;
12-
//use PHPUnit\Framework\TestCase;
1314
use Tests\TestCase;
1415
use Symfony\Component\DomCrawler\Crawler;
16+
use Faker\Generator as Faker;
1517

1618
class ProductServiceTest extends TestCase
1719
{
@@ -147,6 +149,35 @@ public function test_handle_scrape_product()
147149
}
148150

149151

152+
/**
153+
* Test delete existing product
154+
*
155+
* @return void
156+
*/
157+
public function test_delete_existing_product(): void
158+
{
159+
$faker = new Faker();
160+
161+
$asin = 'BX26AU';
162+
$category = Category::create(['name' => 'Category Test']);
163+
$product = Product::create([
164+
'asin' => $asin,
165+
'name' => 'Product Test',
166+
'category_id' => $category->id
167+
]);
168+
169+
$product->prices()->create([
170+
'price' => rand(10, 80)
171+
]);
172+
173+
$result = $this->service->deleteProduct($asin);
174+
175+
$this->assertTrue($result);
176+
177+
$this->assertEmpty(Price::where(['product_asin' => $asin])->get());
178+
$this->assertNull(Product::find($asin));
179+
}
180+
150181
protected function setUp(): void
151182
{
152183
parent::setUp();

0 commit comments

Comments
 (0)