Skip to content

Commit

Permalink
Merge pull request #409 from NgBlaze/feat/add-superadmin-update-products
Browse files Browse the repository at this point in the history
feat: add super admin update products
  • Loading branch information
Dev-Tonia authored Aug 8, 2024
2 parents 7ccfa5b + 3f4e0ee commit f4d5c89
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,47 @@ 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',
'category' => 'nullable|string|max:255',
]);

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,
]);
}
}
2 changes: 1 addition & 1 deletion database/seeders/OrderSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class OrderSeeder extends Seeder
{
public function run()
{
Order::factory()->count(10)->create();
Order::factory()->count(10)->create();
}
}
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,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 () {
Expand Down
35 changes: 34 additions & 1 deletion tests/Feature/SuperAdminProductControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]',
'password' => 'bulldozer',
Expand All @@ -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', [
Expand All @@ -51,6 +54,7 @@ public function testSuperAdminCanCreateProduct()
'org_id' => $validOrgId,
]);

// Assert product creation was successful
$productResponse->assertStatus(201);
$productResponse->assertJson([
'success' => true,
Expand All @@ -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,
],
]);
}
}

0 comments on commit f4d5c89

Please sign in to comment.