Esercizio 11 API
This commit is contained in:
66
JS_Esercizi 11 - API/todo_app/index.html
Normal file
66
JS_Esercizi 11 - API/todo_app/index.html
Normal file
@@ -0,0 +1,66 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="it">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Todo App - CRUD Operations</title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>✓ Todo App</h1>
|
||||
<p class="subtitle">Operazioni CRUD (Create, Read, Update, Delete)</p>
|
||||
|
||||
<div class="instructions">
|
||||
<h3>📝 Obiettivo dell'Esercizio</h3>
|
||||
<p>Dovrai implementare un'app TODO con tutte le operazioni CRUD:</p>
|
||||
<ol>
|
||||
<li><strong>Ricerca:</strong> Seleziona un utente dalla barra di ricerca</li>
|
||||
<li><strong>CREATE:</strong> Aggiungi nuovi todo per l'utente selezionato</li>
|
||||
<li><strong>READ:</strong> Visualizza i todo dell'utente</li>
|
||||
<li><strong>UPDATE:</strong> Segna come completato</li>
|
||||
<li><strong>DELETE:</strong> Elimina todo</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
<!-- Barra di ricerca utenti -->
|
||||
<div class="search-section">
|
||||
<h3>👤 Seleziona Utente</h3>
|
||||
<div class="search-container">
|
||||
<input
|
||||
type="text"
|
||||
id="userSearch"
|
||||
placeholder="Cerca un utente per nome..."
|
||||
autocomplete="off"
|
||||
>
|
||||
</div>
|
||||
<div class="suggestions" id="suggestions"></div>
|
||||
<div id="selectedUser" class="selected-user">Nessun utente selezionato</div>
|
||||
</div>
|
||||
|
||||
<!-- Input per aggiungere todo -->
|
||||
<div class="input-group">
|
||||
<input
|
||||
type="text"
|
||||
id="todoInput"
|
||||
placeholder="Aggiungi un nuovo todo..."
|
||||
onkeypress="if(event.key === 'Enter') addTodo()"
|
||||
>
|
||||
<button class="btn" onclick="addTodo()">Aggiungi</button>
|
||||
</div>
|
||||
|
||||
<div class="loading" id="loading">
|
||||
<div class="spinner"></div>
|
||||
<p>Caricamento...</p>
|
||||
</div>
|
||||
|
||||
<div class="todos-container" id="todosContainer">
|
||||
<!-- I todo verranno inseriti qui -->
|
||||
</div>
|
||||
|
||||
<a href="../index.html" class="back-button">← Torna al Hub</a>
|
||||
</div>
|
||||
|
||||
<script src="script.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
158
JS_Esercizi 11 - API/todo_app/script.js
Normal file
158
JS_Esercizi 11 - API/todo_app/script.js
Normal file
@@ -0,0 +1,158 @@
|
||||
let currentUserId = null;
|
||||
|
||||
// 1. Funzione che crea un todo (oggetto)
|
||||
function createTodo(id, title, completato) {
|
||||
return { id, title, completato };
|
||||
}
|
||||
|
||||
// 2. Carica tutti i todos di un particolare utente
|
||||
async function loadUserTodos(userId) {
|
||||
const loading = document.getElementById('loading');
|
||||
const container = document.getElementById('todosContainer');
|
||||
|
||||
loading.style.display = 'block';
|
||||
container.innerHTML = '';
|
||||
|
||||
try {
|
||||
const response = await fetch(`http://localhost:3000/api/todos?userId=${userId}`);
|
||||
const todos = await response.json();
|
||||
displayTodos(todos);
|
||||
} catch (error) {
|
||||
container.innerHTML = `<div class="error">❌ ${error.message}</div>`;
|
||||
} finally {
|
||||
loading.style.display = 'none';
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Aggiungi un todo
|
||||
async function addTodo() {
|
||||
if (!currentUserId) {
|
||||
alert('Seleziona un utente prima!');
|
||||
return;
|
||||
}
|
||||
|
||||
const input = document.getElementById('todoInput');
|
||||
const title = input.value.trim();
|
||||
|
||||
if (!title) {
|
||||
alert('Inserisci un todo!');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch('http://localhost:3000/api/todos', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ userId: currentUserId, title, completato: false })
|
||||
});
|
||||
input.value = '';
|
||||
loadUserTodos(currentUserId);
|
||||
} catch (error) {
|
||||
alert('Errore: ' + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Toggle completato
|
||||
async function toggleTodo(id, currentCompleted) {
|
||||
try {
|
||||
const response = await fetch(`http://localhost:3000/api/todos/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ completato: !currentCompleted })
|
||||
});
|
||||
loadUserTodos(currentUserId);
|
||||
} catch (error) {
|
||||
alert('Errore: ' + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 5. Rimuovi un todo
|
||||
async function deleteTodo(id) {
|
||||
if (!confirm('Sei sicuro di voler eliminare?')) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`http://localhost:3000/api/todos/${id}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
loadUserTodos(currentUserId);
|
||||
} catch (error) {
|
||||
alert('Errore: ' + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Visualizza i todos
|
||||
function displayTodos(todos) {
|
||||
const container = document.getElementById('todosContainer');
|
||||
|
||||
if (!Array.isArray(todos) || todos.length === 0) {
|
||||
container.innerHTML = '<div class="empty">Nessun todo</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
const todosHTML = todos.map(todo => `
|
||||
<div class="todo-item ${todo.completato ? 'completed' : ''}">
|
||||
<div class="todo-content">
|
||||
<div class="todo-text">${todo.title}</div>
|
||||
<div class="todo-id">ID: ${todo.id}</div>
|
||||
</div>
|
||||
<div class="todo-actions">
|
||||
<button class="btn btn-small btn-complete" onclick="toggleTodo(${todo.id}, ${todo.completato})">
|
||||
${todo.completato ? '↩️' : '✓'}
|
||||
</button>
|
||||
<button class="btn btn-small btn-delete" onclick="deleteTodo(${todo.id})">
|
||||
🗑️
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
`).join('');
|
||||
|
||||
container.innerHTML = todosHTML;
|
||||
}
|
||||
|
||||
// Carica gli utenti nella ricerca
|
||||
async function loadUsers() {
|
||||
try {
|
||||
const response = await fetch('http://localhost:3000/api/users');
|
||||
const users = await response.json();
|
||||
|
||||
const searchInput = document.getElementById('userSearch');
|
||||
const suggestionsContainer = document.getElementById('suggestions');
|
||||
|
||||
// Filtra gli utenti mentre digiti
|
||||
searchInput.addEventListener('input', (e) => {
|
||||
const searchTerm = e.target.value.toLowerCase();
|
||||
|
||||
if (searchTerm.length === 0) {
|
||||
suggestionsContainer.innerHTML = '';
|
||||
return;
|
||||
}
|
||||
|
||||
const filtered = users.filter(user =>
|
||||
user.nome.toLowerCase().includes(searchTerm) ||
|
||||
user.cognome.toLowerCase().includes(searchTerm)
|
||||
);
|
||||
|
||||
suggestionsContainer.innerHTML = filtered.map(user => `
|
||||
<div class="suggestion-item" onclick="selectUser(${user.id}, '${user.nome} ${user.cognome}')">
|
||||
${user.nome} ${user.cognome} (${user.email})
|
||||
</div>
|
||||
`).join('');
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Errore nel caricamento utenti:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Seleziona un utente
|
||||
function selectUser(userId, userName) {
|
||||
currentUserId = userId;
|
||||
document.getElementById('userSearch').value = userName;
|
||||
document.getElementById('suggestions').innerHTML = '';
|
||||
document.getElementById('selectedUser').textContent = `Utente: ${userName}`;
|
||||
loadUserTodos(userId);
|
||||
}
|
||||
|
||||
// Carica gli utenti all'avvio
|
||||
loadUsers();
|
||||
316
JS_Esercizi 11 - API/todo_app/styles.css
Normal file
316
JS_Esercizi 11 - API/todo_app/styles.css
Normal file
@@ -0,0 +1,316 @@
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
min-height: 100vh;
|
||||
padding: 40px 20px;
|
||||
}
|
||||
|
||||
.container {
|
||||
max-width: 700px;
|
||||
margin: 0 auto;
|
||||
background: white;
|
||||
border-radius: 15px;
|
||||
padding: 40px;
|
||||
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: #333;
|
||||
text-align: center;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.subtitle {
|
||||
text-align: center;
|
||||
color: #666;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
|
||||
.instructions {
|
||||
background: #e3f2fd;
|
||||
border-left: 4px solid #2196F3;
|
||||
padding: 15px;
|
||||
margin-bottom: 30px;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.instructions h3 {
|
||||
color: #1565c0;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.instructions ol {
|
||||
margin-left: 20px;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.instructions li {
|
||||
margin: 8px 0;
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
.input-group {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
input {
|
||||
flex: 1;
|
||||
padding: 12px;
|
||||
border: 2px solid #ddd;
|
||||
border-radius: 4px;
|
||||
font-size: 1rem;
|
||||
transition: border-color 0.3s;
|
||||
}
|
||||
|
||||
input:focus {
|
||||
outline: none;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: 12px 20px;
|
||||
background: #667eea;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
font-size: 1rem;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.btn:hover {
|
||||
background: #764ba2;
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
|
||||
}
|
||||
|
||||
.btn-small {
|
||||
padding: 6px 12px;
|
||||
font-size: 0.85rem;
|
||||
margin-left: 5px;
|
||||
}
|
||||
|
||||
.btn-delete {
|
||||
background: #f44336;
|
||||
}
|
||||
|
||||
.btn-delete:hover {
|
||||
background: #d32f2f;
|
||||
}
|
||||
|
||||
.btn-complete {
|
||||
background: #4caf50;
|
||||
}
|
||||
|
||||
.btn-complete:hover {
|
||||
background: #388e3c;
|
||||
}
|
||||
|
||||
.todos-container {
|
||||
margin-top: 30px;
|
||||
}
|
||||
|
||||
.todo-item {
|
||||
background: #f9f9f9;
|
||||
border-left: 4px solid #667eea;
|
||||
padding: 15px;
|
||||
margin-bottom: 12px;
|
||||
border-radius: 4px;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
transition: all 0.3s;
|
||||
}
|
||||
|
||||
.todo-item:hover {
|
||||
background: #f5f5f5;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.todo-item.completed {
|
||||
opacity: 0.6;
|
||||
border-left-color: #4caf50;
|
||||
}
|
||||
|
||||
.todo-item.completed .todo-text {
|
||||
text-decoration: line-through;
|
||||
color: #999;
|
||||
}
|
||||
|
||||
.todo-content {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.todo-text {
|
||||
color: #333;
|
||||
font-size: 1rem;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
|
||||
.todo-id {
|
||||
color: #999;
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
.todo-actions {
|
||||
display: flex;
|
||||
gap: 5px;
|
||||
}
|
||||
|
||||
.error {
|
||||
background: #ffebee;
|
||||
border-left: 4px solid #f44336;
|
||||
color: #c62828;
|
||||
padding: 15px;
|
||||
border-radius: 4px;
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.success {
|
||||
background: #e8f5e9;
|
||||
border-left: 4px solid #4caf50;
|
||||
color: #2e7d32;
|
||||
padding: 15px;
|
||||
border-radius: 4px;
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.empty {
|
||||
text-align: center;
|
||||
color: #999;
|
||||
padding: 40px 20px;
|
||||
}
|
||||
|
||||
.loading {
|
||||
display: none;
|
||||
text-align: center;
|
||||
color: #667eea;
|
||||
margin: 20px 0;
|
||||
}
|
||||
|
||||
.spinner {
|
||||
border: 4px solid #f3f3f3;
|
||||
border-top: 4px solid #667eea;
|
||||
border-radius: 50%;
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
animation: spin 1s linear infinite;
|
||||
margin: 0 auto 10px;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
0% { transform: rotate(0deg); }
|
||||
100% { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
.back-button {
|
||||
display: inline-block;
|
||||
margin-top: 30px;
|
||||
padding: 10px 20px;
|
||||
background: #999;
|
||||
color: white;
|
||||
text-decoration: none;
|
||||
border-radius: 4px;
|
||||
transition: all 0.3s;
|
||||
}
|
||||
|
||||
.back-button:hover {
|
||||
background: #777;
|
||||
}
|
||||
|
||||
code {
|
||||
background: #f5f5f5;
|
||||
padding: 2px 6px;
|
||||
border-radius: 4px;
|
||||
font-family: monospace;
|
||||
color: #d63384;
|
||||
}
|
||||
|
||||
.counter {
|
||||
background: #667eea;
|
||||
color: white;
|
||||
padding: 8px 12px;
|
||||
border-radius: 20px;
|
||||
display: inline-block;
|
||||
font-size: 0.9rem;
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
.search-section {
|
||||
background: #f0f4ff;
|
||||
border: 2px solid #667eea;
|
||||
padding: 20px;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
|
||||
.search-section h3 {
|
||||
color: #667eea;
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
.search-container {
|
||||
position: relative;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
#userSearch {
|
||||
width: 100%;
|
||||
padding: 12px;
|
||||
border: 2px solid #ddd;
|
||||
border-radius: 4px;
|
||||
font-size: 1rem;
|
||||
transition: border-color 0.3s;
|
||||
}
|
||||
|
||||
#userSearch:focus {
|
||||
outline: none;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
.suggestions {
|
||||
background: white;
|
||||
border: 1px solid #ddd;
|
||||
border-top: none;
|
||||
border-radius: 0 0 4px 4px;
|
||||
max-height: 250px;
|
||||
overflow-y: auto;
|
||||
display: none;
|
||||
}
|
||||
|
||||
.suggestions:not(:empty) {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.suggestion-item {
|
||||
padding: 12px;
|
||||
border-bottom: 1px solid #eee;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
.suggestion-item:hover {
|
||||
background: #f0f4ff;
|
||||
color: #667eea;
|
||||
}
|
||||
|
||||
.suggestion-item:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.selected-user {
|
||||
color: #666;
|
||||
font-size: 0.95rem;
|
||||
padding: 10px 0;
|
||||
}
|
||||
Reference in New Issue
Block a user