From e1574eea12bbaabfbfbcc7f4cf4e23d5d85bd167 Mon Sep 17 00:00:00 2001 From: NgBlaze Date: Wed, 7 Aug 2024 21:04:30 +0100 Subject: [PATCH 1/3] Fixed super admin update functionality and route --- .../SuperAdminProductController.php | 42 +++++++++++++++++++ routes/api.php | 1 + 2 files changed, 43 insertions(+) diff --git a/app/Http/Controllers/Api/V1/SuperAdmin/SuperAdminProductController.php b/app/Http/Controllers/Api/V1/SuperAdmin/SuperAdminProductController.php index 811b22a0..f28e5c66 100644 --- a/app/Http/Controllers/Api/V1/SuperAdmin/SuperAdminProductController.php +++ b/app/Http/Controllers/Api/V1/SuperAdmin/SuperAdminProductController.php @@ -53,4 +53,46 @@ public function store(Request $request) 'data' => $product, ], 201); } + public function update(Request $request, $productId) + { + $validator = Validator::make($request->all(), [ + 'name' => 'sometimes|string|max:255', + 'description' => 'sometimes|string', + 'price' => 'sometimes|numeric', + 'slug' => 'sometimes|string|max:255', + 'tags' => 'sometimes|string', + 'imageUrl' => 'nullable|string|max:255', + 'status' => 'sometimes|string|max:50', + 'quantity' => 'sometimes|integer', + 'org_id' => 'sometimes|uuid', + ]); + + if ($validator->fails()) { + return response()->json([ + 'success' => false, + 'status_code' => 422, + 'message' => 'Validation errors', + 'data' => $validator->errors(), + ], 422); + } + + $product = Product::findOrFail($productId); + + $product->fill($request->only([ + 'name', 'description', 'price', 'slug', 'tags', + 'imageUrl', 'status', 'quantity', 'org_id' + ])); + + + $product->user_id = $product->user_id; + + $product->save(); + + return response()->json([ + 'success' => true, + 'status_code' => 200, + 'message' => 'Product updated successfully', + 'data' => $product, + ]); + } } diff --git a/routes/api.php b/routes/api.php index 383d42d3..75653472 100755 --- a/routes/api.php +++ b/routes/api.php @@ -142,6 +142,7 @@ //Super Admin Add Products Route::middleware(['auth:api', 'admin'])->group(function () { Route::post('/products', [SuperAdminProductController::class, 'store']); + Route::patch('/products/{productId}', [SuperAdminProductController::class, 'update']); }); Route::middleware(['auth:api', 'admin'])->group(function () { From a8c07eab4cc6439bff3230a4dc1a4c3d451821d6 Mon Sep 17 00:00:00 2001 From: NgBlaze Date: Wed, 7 Aug 2024 21:16:57 +0100 Subject: [PATCH 2/3] fixed order seder --- database/seeders/OrderSeeder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/seeders/OrderSeeder.php b/database/seeders/OrderSeeder.php index 5fd30d41..8accffea 100644 --- a/database/seeders/OrderSeeder.php +++ b/database/seeders/OrderSeeder.php @@ -14,6 +14,6 @@ class OrderSeeder extends Seeder { public function run() { - // Order::factory()->count(10)->create(); + Order::factory()->count(10)->create(); } } From 309b4a7908d07a2d647a90ff22730da2483c807e Mon Sep 17 00:00:00 2001 From: NgBlaze Date: Wed, 7 Aug 2024 23:48:46 +0100 Subject: [PATCH 3/3] Updated Oatch method in conttoler and included tests --- .../SuperAdminProductController.php | 1 + .../SuperAdminProductControllerTest.php | 35 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/V1/SuperAdmin/SuperAdminProductController.php b/app/Http/Controllers/Api/V1/SuperAdmin/SuperAdminProductController.php index 932d411a..67349412 100644 --- a/app/Http/Controllers/Api/V1/SuperAdmin/SuperAdminProductController.php +++ b/app/Http/Controllers/Api/V1/SuperAdmin/SuperAdminProductController.php @@ -66,6 +66,7 @@ public function update(Request $request, $productId) 'status' => 'sometimes|string|max:50', 'quantity' => 'sometimes|integer', 'org_id' => 'sometimes|uuid', + 'category' => 'nullable|string|max:255', ]); if ($validator->fails()) { diff --git a/tests/Feature/SuperAdminProductControllerTest.php b/tests/Feature/SuperAdminProductControllerTest.php index b2cdb4ed..d92048ba 100644 --- a/tests/Feature/SuperAdminProductControllerTest.php +++ b/tests/Feature/SuperAdminProductControllerTest.php @@ -10,10 +10,11 @@ class SuperAdminProductControllerTest extends TestCase { use RefreshDatabase; - public function testSuperAdminCanCreateProduct() + public function testSuperAdminCanCreateAndUpdateProduct() { $this->artisan('migrate:fresh --seed'); + // Log in as Super Admin $loginResponse = $this->postJson('/api/v1/auth/login', [ 'email' => 'bulldozeradmin@hng.com', 'password' => 'bulldozer', @@ -36,8 +37,10 @@ public function testSuperAdminCanCreateProduct() $accessToken = $loginResponse->json('access_token'); $userId = $loginResponse->json('data.user.id'); + // Fetch a valid organization ID from the seeded data $validOrgId = Product::first()->org_id; + // Create a product $productResponse = $this->withHeaders([ 'Authorization' => 'Bearer ' . $accessToken, ])->postJson('/api/v1/products', [ @@ -51,6 +54,7 @@ public function testSuperAdminCanCreateProduct() 'org_id' => $validOrgId, ]); + // Assert product creation was successful $productResponse->assertStatus(201); $productResponse->assertJson([ 'success' => true, @@ -70,5 +74,34 @@ public function testSuperAdminCanCreateProduct() 'user_id' => $userId, ], ]); + + $productId = $productResponse->json('data.product_id'); + + $updateData = [ + 'name' => 'Updated Name', + 'description' => 'Updated Description', + 'price' => 200, + 'status' => 'active', + 'quantity' => 100, + ]; + + $updateResponse = $this->withHeaders([ + 'Authorization' => 'Bearer ' . $accessToken, + ])->patchJson("/api/v1/products/{$productId}", $updateData); + + + $updateResponse->assertStatus(200); + $updateResponse->assertJson([ + 'success' => true, + 'status_code' => 200, + 'message' => 'Product updated successfully', + 'data' => [ + 'name' => 'Updated Name', + 'description' => 'Updated Description', + 'price' => 200, + 'status' => 'active', + 'quantity' => 100, + ], + ]); } }