83 lines
2.4 KiB
JavaScript
83 lines
2.4 KiB
JavaScript
const apiUrl = "http://localhost:5011";
|
|
|
|
function listPizze(){
|
|
let reply;
|
|
const requestOptions = {
|
|
method: "GET",
|
|
redirect: "follow"
|
|
};
|
|
|
|
fetch(apiUrl + "/api/pizze", requestOptions)
|
|
.then((response) => response.json())
|
|
.then((pizze) => {
|
|
// Creiamo la tabella
|
|
let table = '<table border="1"><tr><th>ID</th><th>Nome</th><th>Prezzo</th><th>Azioni</th></tr>';
|
|
|
|
// Aggiungiamo ogni pizza come riga della tabella
|
|
pizze.pizze.forEach(pizza => {
|
|
table += `<tr>
|
|
<td>${pizza.id}</td>
|
|
<td>${pizza.nome}</td>
|
|
<td>${pizza.prezzo.toFixed(2)}€</td>
|
|
<td><button class="editBtn" onclick="editPizza(${pizza.id})">Modifica</button>
|
|
<button class="delBtn" onclick="deletePizza(${pizza.id})">X</button></td>
|
|
</tr>`;
|
|
});
|
|
|
|
table += '</table>';
|
|
|
|
// Inseriamo la tabella nel div
|
|
document.getElementById('tabellaPizze').innerHTML = table;
|
|
})
|
|
.catch((error) => {
|
|
console.error(error)
|
|
alert("Errore nel recupero delle pizze, controllare la console per dettagli sull'errore.");
|
|
});
|
|
}
|
|
|
|
function deletePizza(id){
|
|
const requestOptions = {
|
|
method: "DELETE",
|
|
redirect: "follow"
|
|
};
|
|
|
|
fetch(apiUrl + "/api/pizze/" + id, requestOptions)
|
|
.then((response) => response.text())
|
|
.then((result) => {
|
|
alert("Pizza eliminata con successo!");
|
|
listPizze();
|
|
})
|
|
.catch((error) => {
|
|
console.error(error)
|
|
alert("Errore nell'eliminazione della pizza, controllare la console per dettagli sull'errore.");
|
|
});
|
|
}
|
|
|
|
function editPizza(id){
|
|
let prezzo = prompt("Inserire prezzo da assegnare");
|
|
|
|
const myHeaders = new Headers();
|
|
myHeaders.append("Content-Type", "application/json");
|
|
|
|
const raw = JSON.stringify({
|
|
"prezzo": prezzo
|
|
});
|
|
|
|
const requestOptions = {
|
|
method: "PUT",
|
|
headers: myHeaders,
|
|
body: raw,
|
|
redirect: "follow"
|
|
};
|
|
|
|
fetch(apiUrl + "/api/pizze/" + id, requestOptions)
|
|
.then((response) => response.text())
|
|
.then((result) => {
|
|
alert("Pizza modificata con successo!");
|
|
listPizze();
|
|
})
|
|
.catch((error) => {
|
|
console.error(error)
|
|
alert("Errore nella modifica, controllare la console per dettagli sull'errore.");
|
|
});
|
|
} |