Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 28 additions & 37 deletions src/routes/customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
$db = null;
echo json_encode($customers);
} catch(PDOException $e){
echo '{"error": {"text": '.$e->getMessage().'}';
echo '{"error": {"text": '.$e->getMessage().'} }';
}
});

Expand All @@ -52,7 +52,7 @@
$db = null;
echo json_encode($customer);
} catch(PDOException $e){
echo '{"error": {"text": '.$e->getMessage().'}';
echo '{"error": {"text": '.$e->getMessage().'} }';
}
});

Expand Down Expand Up @@ -87,56 +87,47 @@

$stmt->execute();

echo '{"notice": {"text": "Customer Added"}';
echo '{"notice": {"text": "Customer Added"} }';

} catch(PDOException $e){
echo '{"error": {"text": '.$e->getMessage().'}';
echo '{"error": {"text": '.$e->getMessage().'} }';
}
});

// Update Customer
$app->put('/api/customer/update/{id}', function(Request $request, Response $response){
$id = $request->getAttribute('id');
$first_name = $request->getParam('first_name');
$last_name = $request->getParam('last_name');
$phone = $request->getParam('phone');
$email = $request->getParam('email');
$address = $request->getParam('address');
$city = $request->getParam('city');
$state = $request->getParam('state');

$sql = "UPDATE customers SET
first_name = :first_name,
last_name = :last_name,
phone = :phone,
email = :email,
address = :address,
city = :city,
state = :state
WHERE id = $id";
$reguestParams = $request->getParams();

$updates = array_filter($reguestParams, function ($value) {
return null !== $value;
});

$sql = 'UPDATE customers SET';

$values = array();
foreach ($updates as $name => $value) {
$sql .= ' '.$name.' = :'.$name.',';
$values[':'.$name] = $value;
}

$sql = substr($query, 0, -1);
$sql .= " WHERE id = " . $id . ';';

try{
// Get DB Object
$db = new db();
// Connect
$db = $db->connect();

$stmt = $db->prepare($sql);
$stmt->execute($values);
$db = null;

$stmt->bindParam(':first_name', $first_name);
$stmt->bindParam(':last_name', $last_name);
$stmt->bindParam(':phone', $phone);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':address', $address);
$stmt->bindParam(':city', $city);
$stmt->bindParam(':state', $state);

$stmt->execute();

echo '{"notice": {"text": "Customer Updated"}';
echo '{"notice": {"text": "Customer Updated"} }';

} catch(PDOException $e){
echo '{"error": {"text": '.$e->getMessage().'}';
echo '{"error": {"text": '.$e->getMessage().'} }';
}
});

Expand All @@ -155,8 +146,8 @@
$stmt = $db->prepare($sql);
$stmt->execute();
$db = null;
echo '{"notice": {"text": "Customer Deleted"}';
echo '{"notice": {"text": "Customer Deleted"} }';
} catch(PDOException $e){
echo '{"error": {"text": '.$e->getMessage().'}';
echo '{"error": {"text": '.$e->getMessage().'} }';
}
});
});