Skip to content

Commit

Permalink
fix bugs in updateProduct
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed May 16, 2024
1 parent ae09ce0 commit 6ac06c7
Showing 1 changed file with 4 additions and 18 deletions.
22 changes: 4 additions & 18 deletions src/controllers/api/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
class Products
{
use Model;

/**
* Get the list of all products available in the store.
*/
Expand Down Expand Up @@ -162,7 +163,7 @@ private function updateProduct(): void
$putData = json_decode(file_get_contents("php://input"), true);

// Check if PUT data is valid
if ($putData === null) {
if (empty($putData)) {
// Invalid JSON data
http_response_code(400); // Bad Request
echo json_encode(['error' => 'Invalid JSON data']);
Expand All @@ -180,25 +181,10 @@ private function updateProduct(): void
return;
}

// Update product attributes
foreach ($putData as $key => $value) {
$product->{$key} = $value;
}

// Validate updated product attributes
$errors = $product->validate();
if (!empty($errors)) {
// Validation errors
http_response_code(400); // Bad Request
echo json_encode(['error' => 'Validation failed', 'errors' => $errors]);
return;
}

// Update product in the database
$rowsAffected = $product->update($productId, $putData, 'product');
$success = $product->updateProduct($putData);

// Check if update was successful
if ($rowsAffected > 0) {
if ($success) {
// Product updated successfully
http_response_code(200); // OK
echo json_encode(['message' => 'Product updated successfully']);
Expand Down

0 comments on commit 6ac06c7

Please sign in to comment.