Esercizio 11 API
This commit is contained in:
93
javascript/JS_Esercizi 11 - API/04_todo_app_crud/index.html
Normal file
93
javascript/JS_Esercizi 11 - API/04_todo_app_crud/index.html
Normal file
@@ -0,0 +1,93 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="it">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Esercizio 4 - Todo App CRUD</title>
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
<body>
|
||||
<a href="../index.html" style="position: absolute; top: 20px; left: 20px; text-decoration: none; color: #555; font-weight: bold;">← Dashboard</a>
|
||||
|
||||
<div class="app-container">
|
||||
<h1>✅ Todo App CRUD</h1>
|
||||
<p class="subtitle">GET, POST, PUT, DELETE - App completa</p>
|
||||
|
||||
<!-- SEZIONE SELEZIONE UTENTE -->
|
||||
<div class="config-box">
|
||||
<h2>👤 Seleziona Utente</h2>
|
||||
<div class="input-group">
|
||||
<input type="number" id="userId" min="1" max="40" value="1" placeholder="ID Utente (1-40)">
|
||||
<button id="btnLoadTodos">Carica TODO</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- SEZIONE AGGIUNTA TODO -->
|
||||
<div id="addTodoSection" class="add-todo-box" style="display: none;">
|
||||
<h2>➕ Aggiungi Nuovo TODO</h2>
|
||||
<div class="input-group">
|
||||
<input type="text" id="todoInput" placeholder="Scrivi un nuovo TODO...">
|
||||
<button id="btnAddTodo">Aggiungi</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- LOADING -->
|
||||
<div id="loading" class="loading" style="display: none;">
|
||||
⏳ Caricamento...
|
||||
</div>
|
||||
|
||||
<!-- COUNTER -->
|
||||
<div id="counter" class="counter" style="display: none;"></div>
|
||||
|
||||
<!-- LISTA TODO -->
|
||||
<div id="todosContainer" class="todos-container"></div>
|
||||
|
||||
<!-- ISTRUZIONI -->
|
||||
<div class="instructions">
|
||||
<h2>📝 Cosa Devi Fare</h2>
|
||||
<p style="margin-bottom: 15px;"><strong>Questa è l'esercitazione finale!</strong> Devi implementare TUTTE le operazioni CRUD:</p>
|
||||
|
||||
<h3>1️⃣ Carica TODO (GET)</h3>
|
||||
<ul style="margin-left: 20px; margin-bottom: 15px;">
|
||||
<li>Fai una GET a <code>/todos?userId={id}</code> per ottenere i TODO dell'utente</li>
|
||||
<li>Visualizza la lista</li>
|
||||
</ul>
|
||||
|
||||
<h3>2️⃣ Aggiungi TODO (POST)</h3>
|
||||
<ul style="margin-left: 20px; margin-bottom: 15px;">
|
||||
<li>Fai una POST a <code>/todos</code> con: <code>{userId, titolo, completato: false}</code></li>
|
||||
<li>Ricarica la lista</li>
|
||||
</ul>
|
||||
|
||||
<h3>3️⃣ Modifica TODO (PUT)</h3>
|
||||
<ul style="margin-left: 20px; margin-bottom: 15px;">
|
||||
<li>Fai una PUT a <code>/todos/{id}</code> con: <code>{completato: !currentValue}</code></li>
|
||||
<li>Aggiorna la lista</li>
|
||||
</ul>
|
||||
|
||||
<h3>4️⃣ Elimina TODO (DELETE)</h3>
|
||||
<ul style="margin-left: 20px; margin-bottom: 15px;">
|
||||
<li>Fai una DELETE a <code>/todos/{id}</code></li>
|
||||
<li>Rimuovi dalla lista</li>
|
||||
</ul>
|
||||
|
||||
<div class="hint">
|
||||
<strong>💡 Struttura del codice:</strong>
|
||||
<pre>// Funzioni che devi implementare:
|
||||
loadUserTodos(userId) // GET
|
||||
addTodo() // POST
|
||||
toggleTodo(id, current) // PUT
|
||||
deleteTodo(id) // DELETE
|
||||
displayTodos(todos) // Visualizza (già fatto)</pre>
|
||||
</div>
|
||||
|
||||
<div class="challenge">
|
||||
<strong>🎯 Bonus Challenge:</strong>
|
||||
<p>Aggiungi un riepilogo: quanti TODO sono completati vs quanti rimangono?</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="script.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
197
javascript/JS_Esercizi 11 - API/04_todo_app_crud/script.js
Normal file
197
javascript/JS_Esercizi 11 - API/04_todo_app_crud/script.js
Normal file
@@ -0,0 +1,197 @@
|
||||
// ⚠️ COMPILARE PRIMA DI INIZIARE
|
||||
const BASE_URL = 'http://localhost:3000/api';
|
||||
|
||||
let currentUserId = null;
|
||||
|
||||
/**
|
||||
* ESERCIZIO 4: Todo App CRUD Completa
|
||||
*
|
||||
* Devi implementare 4 funzioni:
|
||||
* 1. loadUserTodos() - GET /todos?userId={id}
|
||||
* 2. addTodo() - POST /todos
|
||||
* 3. toggleTodo() - PUT /todos/{id}
|
||||
* 4. deleteTodo() - DELETE /todos/{id}
|
||||
*/
|
||||
|
||||
// ======== 1️⃣ CARICA TODO (GET) ========
|
||||
/**
|
||||
* Leggi l'ID utente, fai GET a /todos?userId={id}
|
||||
* Mostra la lista con displayTodos()
|
||||
*/
|
||||
async function loadUserTodos() {
|
||||
const userId = document.getElementById('userId').value;
|
||||
|
||||
if (!userId || userId < 1 || userId > 40) {
|
||||
alert('Inserisci un ID valido tra 1 e 40');
|
||||
return;
|
||||
}
|
||||
|
||||
currentUserId = userId;
|
||||
const loading = document.getElementById('loading');
|
||||
const container = document.getElementById('todosContainer');
|
||||
const addSection = document.getElementById('addTodoSection');
|
||||
const counter = document.getElementById('counter');
|
||||
|
||||
loading.style.display = 'block';
|
||||
container.innerHTML = '';
|
||||
counter.style.display = 'none';
|
||||
|
||||
try {
|
||||
// 👇 SCRIVI QUI - Fai fetch GET a /todos con query parameter userId
|
||||
// const response = await fetch(BASE_URL + '/todos?userId=' + userId);
|
||||
// const todos = await response.json();
|
||||
// displayTodos(todos);
|
||||
|
||||
throw new Error('Codice non implementato - Completa loadUserTodos()');
|
||||
|
||||
} catch (error) {
|
||||
container.innerHTML = `<div class="error">❌ ${error.message}</div>`;
|
||||
console.error('Errore:', error);
|
||||
} finally {
|
||||
loading.style.display = 'none';
|
||||
addSection.style.display = 'block';
|
||||
}
|
||||
}
|
||||
|
||||
// ======== 2️⃣ AGGIUNGI TODO (POST) ========
|
||||
/**
|
||||
* Leggi il testo dall'input
|
||||
* Fai POST a /todos con {userId, titolo, completato: false}
|
||||
* Ricarica la lista
|
||||
*/
|
||||
async function addTodo() {
|
||||
if (!currentUserId) {
|
||||
alert('Carica prima una lista di TODO!');
|
||||
return;
|
||||
}
|
||||
|
||||
const input = document.getElementById('todoInput');
|
||||
const titolo = input.value.trim();
|
||||
|
||||
if (!titolo) {
|
||||
alert('Scrivi un TODO!');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 👇 SCRIVI QUI - Fai fetch POST
|
||||
// const response = await fetch(BASE_URL + '/todos', {
|
||||
// method: 'POST',
|
||||
// headers: { 'Content-Type': 'application/json' },
|
||||
// body: JSON.stringify({ userId: currentUserId, titolo, completato: false })
|
||||
// });
|
||||
// input.value = '';
|
||||
// loadUserTodos();
|
||||
|
||||
throw new Error('Codice non implementato - Completa addTodo()');
|
||||
|
||||
} catch (error) {
|
||||
alert('Errore: ' + error.message);
|
||||
console.error('Errore:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// ======== 3️⃣ MODIFICA TODO (PUT) ========
|
||||
/**
|
||||
* Fai PUT a /todos/{id} con {completato: !currentValue}
|
||||
* Ricarica la lista
|
||||
*/
|
||||
async function toggleTodo(id, currentCompleted) {
|
||||
try {
|
||||
// 👇 SCRIVI QUI - Fai fetch PUT
|
||||
// const response = await fetch(BASE_URL + '/todos/' + id, {
|
||||
// method: 'PUT',
|
||||
// headers: { 'Content-Type': 'application/json' },
|
||||
// body: JSON.stringify({ completato: !currentCompleted })
|
||||
// });
|
||||
// loadUserTodos();
|
||||
|
||||
throw new Error('Codice non implementato - Completa toggleTodo()');
|
||||
|
||||
} catch (error) {
|
||||
alert('Errore: ' + error.message);
|
||||
console.error('Errore:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// ======== 4️⃣ ELIMINA TODO (DELETE) ========
|
||||
/**
|
||||
* Chiedi conferma con confirm()
|
||||
* Fai DELETE a /todos/{id}
|
||||
* Ricarica la lista
|
||||
*/
|
||||
async function deleteTodo(id) {
|
||||
if (!confirm('Sei sicuro di voler eliminare questo TODO?')) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 👇 SCRIVI QUI - Fai fetch DELETE
|
||||
// const response = await fetch(BASE_URL + '/todos/' + id, {
|
||||
// method: 'DELETE'
|
||||
// });
|
||||
// loadUserTodos();
|
||||
|
||||
throw new Error('Codice non implementato - Completa deleteTodo()');
|
||||
|
||||
} catch (error) {
|
||||
alert('Errore: ' + error.message);
|
||||
console.error('Errore:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Visualizza i TODO
|
||||
* (Questa funzione è già fatta - non modificare)
|
||||
*/
|
||||
function displayTodos(todos) {
|
||||
const container = document.getElementById('todosContainer');
|
||||
const counter = document.getElementById('counter');
|
||||
|
||||
if (!Array.isArray(todos) || todos.length === 0) {
|
||||
container.innerHTML = '<div class="empty">Nessun TODO. Creane uno!</div>';
|
||||
counter.style.display = 'none';
|
||||
return;
|
||||
}
|
||||
|
||||
// CONTA COMPLETATI E NON
|
||||
const completed = todos.filter(t => t.completato).length;
|
||||
const pending = todos.length - completed;
|
||||
|
||||
// MOSTRA COUNTER
|
||||
counter.innerHTML = `
|
||||
📊 Totale: <strong>${todos.length}</strong> |
|
||||
✅ Completati: <strong>${completed}</strong> |
|
||||
⏳ In Sospeso: <strong>${pending}</strong>
|
||||
`;
|
||||
counter.style.display = 'block';
|
||||
|
||||
// CREA CARD TODO
|
||||
const todosHTML = todos.map(todo => `
|
||||
<div class="todo-item ${todo.completato ? 'completed' : ''}">
|
||||
<div class="todo-checkbox">
|
||||
<input type="checkbox"
|
||||
${todo.completato ? 'checked' : ''}
|
||||
onchange="toggleTodo(${todo.id}, ${todo.completato})">
|
||||
</div>
|
||||
<div class="todo-content">
|
||||
<div class="todo-title">${todo.titolo}</div>
|
||||
<div class="todo-id">ID: ${todo.id}</div>
|
||||
</div>
|
||||
<button class="btn-delete" onclick="deleteTodo(${todo.id})">🗑️ Elimina</button>
|
||||
</div>
|
||||
`).join('');
|
||||
|
||||
container.innerHTML = todosHTML;
|
||||
}
|
||||
|
||||
// ======== COLLEGA GLI EVENTI ========
|
||||
document.getElementById('btnLoadTodos').addEventListener('click', loadUserTodos);
|
||||
document.getElementById('btnAddTodo').addEventListener('click', addTodo);
|
||||
|
||||
// PERMETTI ENTER per aggiungere TODO
|
||||
document.getElementById('todoInput').addEventListener('keypress', (e) => {
|
||||
if (e.key === 'Enter') {
|
||||
addTodo();
|
||||
}
|
||||
});
|
||||
266
javascript/JS_Esercizi 11 - API/04_todo_app_crud/style.css
Normal file
266
javascript/JS_Esercizi 11 - API/04_todo_app_crud/style.css
Normal file
@@ -0,0 +1,266 @@
|
||||
body {
|
||||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: flex-start;
|
||||
min-height: 100vh;
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.app-container {
|
||||
background: white;
|
||||
width: 100%;
|
||||
max-width: 550px;
|
||||
padding: 30px;
|
||||
border-radius: 15px;
|
||||
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: #333;
|
||||
text-align: center;
|
||||
margin: 0 0 10px 0;
|
||||
font-size: 1.8rem;
|
||||
}
|
||||
|
||||
.subtitle {
|
||||
color: #666;
|
||||
text-align: center;
|
||||
margin: 0 0 30px 0;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.config-box, .add-todo-box {
|
||||
background: #f9f9f9;
|
||||
padding: 20px;
|
||||
border-radius: 10px;
|
||||
margin-bottom: 25px;
|
||||
}
|
||||
|
||||
.config-box label, .add-todo-box label {
|
||||
display: block;
|
||||
color: #555;
|
||||
font-weight: 600;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.input-group {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.input-group input {
|
||||
flex: 1;
|
||||
padding: 12px;
|
||||
border: 2px solid #ddd;
|
||||
border-radius: 6px;
|
||||
font-size: 15px;
|
||||
font-family: inherit;
|
||||
}
|
||||
|
||||
.input-group input:focus {
|
||||
outline: none;
|
||||
border-color: #007bff;
|
||||
}
|
||||
|
||||
.input-group button {
|
||||
padding: 10px 20px;
|
||||
background: #007bff;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
font-weight: bold;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.input-group button:hover {
|
||||
background: #0056b3;
|
||||
}
|
||||
|
||||
.loading {
|
||||
text-align: center;
|
||||
color: white;
|
||||
font-si#666
|
||||
animation: pulse 1s infinite;
|
||||
}
|
||||
|
||||
@keyframes pulse {
|
||||
0%, 100% { opacity: 1; }
|
||||
50% { opacity: 0.6; }
|
||||
}
|
||||
|
||||
.counter {
|
||||
background: rgba(255, 255, 255, 0.2);
|
||||
color: white;
|
||||
padding: 15p#f0f0f0;
|
||||
color: #333;
|
||||
padding: 12px;
|
||||
border-radius: 6px;
|
||||
margin-bottom: 20px;
|
||||
font-weight: bold;
|
||||
text-align: center
|
||||
.todos-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
margin-bottom: 50px;
|
||||
}0px;
|
||||
margin-bottom: 3
|
||||
.todo-item {
|
||||
background: white;
|
||||
padding: 15p#f9f9f9;
|
||||
padding: 12px;
|
||||
border-radius: 6px;
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
align-items: center;
|
||||
border-left: 4px solid #007bff;
|
||||
animation: slideIn 0.2s ease-out
|
||||
|
||||
.todo-item.completed {
|
||||
background: #f5f5f5;
|
||||
border-left-color: #4caf50;
|
||||
opacity: 0.7;
|
||||
}0f0f0;
|
||||
opacity: 0.7;
|
||||
border-left-color: #28a745eted .todo-title {
|
||||
text-decoration: line-through;
|
||||
color: #999;
|
||||
}
|
||||
|
||||
@keyframes slideIn {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateX(-20px);
|
||||
}
|
||||
to {1
|
||||
opacity: 1;
|
||||
transform: translateX(0);
|
||||
}
|
||||
}
|
||||
|
||||
.todo-checkbox {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.todo-checkbox input {
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
cursor:18px;
|
||||
height: 18
|
||||
|
||||
.todo-content {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.todo-title {
|
||||
color: #333;
|
||||
font-weight: 500;
|
||||
font-size: 1em;
|
||||
margin-bottom: 4px;
|
||||
}margin: 0
|
||||
.todo-id {
|
||||
color: #999;
|
||||
font-size: 0.8em;
|
||||
}
|
||||
|
||||
.btn-delete {rem;
|
||||
margin: 2px 0 0 0
|
||||
padding: 8px 12px;
|
||||
background: #fee;
|
||||
color: #c6px 10px;
|
||||
background: #fee;
|
||||
color: #c00;
|
||||
border: 1px solid #fcc;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-weight: 500
|
||||
|
||||
.btn-delete:hover {
|
||||
background: #fcc;
|
||||
color: #800;
|
||||
|
||||
.empty {
|
||||
background: white;
|
||||
color: #999;
|
||||
padding: 40p#f0f0f0;
|
||||
color: #999;
|
||||
padding: 30px;
|
||||
text-align: center;
|
||||
border-radius: 6px;
|
||||
border: 1
|
||||
.error {
|
||||
background: #fee;
|
||||
color: #c00;
|
||||
padding: 15px;
|
||||
border-radius: 8px;
|
||||
border-left: 4px solid #c00;
|
||||
font-weight: 506px;
|
||||
border-left: 4px solid #c
|
||||
.instructions {
|
||||
background: rgba(255, 255, 255, 0.95);
|
||||
padding: 25px;
|
||||
border-ra-box {
|
||||
background: #f9f9f9;
|
||||
padding: 20px;
|
||||
border-radius: 10px;
|
||||
border-left: 4px solid #007bff;
|
||||
margin-top: 30px;
|
||||
}
|
||||
|
||||
.instructions-box h2 {
|
||||
color: #333;
|
||||
margin: 0 0 15px 0;
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
|
||||
.instructions-box h3 {
|
||||
color: #555;
|
||||
margin: 15px 0 10px 0;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.instructions-box ol, .instructions-box ul {
|
||||
margin-left: 20px;
|
||||
color: #555;
|
||||
line-height: 1.7;
|
||||
}
|
||||
|
||||
.instructions-box li {
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.instructions-box code {
|
||||
background: white;
|
||||
padding: 2px 5px;
|
||||
border-radius: 3px;
|
||||
font-family: 'Courier New', monospace;
|
||||
color: #d63384;
|
||||
font-size: 0.9em;
|
||||
}
|
||||
|
||||
.hint {
|
||||
background: white;
|
||||
border: 1px solid #ddd;
|
||||
padding: 15px;
|
||||
border-radius: 6px;
|
||||
margin-top: 15px;
|
||||
}
|
||||
|
||||
.hint strong {
|
||||
color: #007bff;
|
||||
display: block;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.hint pre {
|
||||
background: #f0f0f0;
|
||||
padding: 10px;
|
||||
border-radius: 4px;
|
||||
overflow-x: auto;
|
||||
font-size: 0.8em;
|
||||
color: #333;
|
||||
margin: 0
|
||||
Reference in New Issue
Block a user