let customers = [];
getcustomers();
async function getcustomers()
{
await fetch('https://localhost:5001/api/Customer')
.then(x => x.json())
.then(y => {
customers = y;
console.log(customers);
display();
});
}
function display()
{
document.getElementById('resultarea').innerHTML = '';
customers.forEach(t => {
document.getElementById('resultarea').innerHTML +=
'
| '
+ t.id +
' | '
+ t.name +
' | '
+ ''
+ ' | '
+ ''
+ ' |
';
});
document.getElementById('resultarea').innerHTML +=
'' +
'| ' +
'Add new' +
' | ' +
'
';
}
function addcustomer()
{
document.getElementById('forms').innerHTML = '';
document.getElementById('forms').innerHTML +=
'';
}
function editcustomer(id)
{
existing_item = customers.find(x => x.id == id);
document.getElementById('forms').innerHTML = '';
document.getElementById('forms').innerHTML +=
'';
}
function savecustomer(method)
{
if (method != 'POST' && method != 'PUT')
{
console.error('Invalid method: ' + method);
return;
}
customer_id = document.getElementById('in_id').value;
customer_name = document.getElementById('in_name').value;
customer_email = document.getElementById('in_email').value;
customer_phone = document.getElementById('in_phone').value;
customer_service_id = document.getElementById('in_service_id').value;
fetch('https://localhost:5001/api/Customer', {
method: method,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
id: customer_id,
name: customer_name,
email: customer_email,
phone: customer_phone,
servicE_ID: customer_service_id
})
})
.then(response => response)
.then(data => {
console.log("Success: ", data)
document.getElementById('saveresult').innerHTML = '';
document.getElementById('saveresult').innerHTML +=
'Saved customer ' + customer_name + ' successfully';
getcustomers();
})
.catch(error => {
console.error("Error: ", error);
document.getElementById('saveresult').innerHTML = '';
document.getElementById('saveresult').innerHTML +=
'Failed to save customer ' + customer_name;
});
}
function deletecustomer(id)
{
document.getElementById('forms').innerHTML = '';
fetch('https://localhost:5001/api/Customer/' + id, {
method: 'DELETE',
headers: { 'Content-Type': 'application/json' },
})
.then(response => response)
.then(data =>
{
console.log("Success: ", data)
document.getElementById('saveresult').innerHTML = '';
document.getElementById('saveresult').innerHTML +=
'Deleted customer number ' + id + ' successfully';
getcustomers();
})
.catch(error => console.error("Error: ", error));
}