Esercizio 11 API

This commit is contained in:
2026-02-05 11:57:28 +01:00
parent f4d3e40059
commit 86611fd6e2
37 changed files with 5057 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Esercizio 1 - GET Singolo Utente</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>🔍 GET Singolo Utente</h1>
<p class="subtitle">Recupera un utente dal server</p>
<!-- SEZIONE CONFIGURAZIONE -->
<div class="config-box">
<label for="userId">ID Utente (1-40):</label>
<div class="input-group">
<input type="number" id="userId" min="1" max="40" value="1">
<button id="btnFetch">Carica</button>
</div>
</div>
<!-- LOADING SPINNER -->
<div id="loading" class="loading nascosto">
⏳ Caricamento...
</div>
<!-- RISULTATO -->
<div id="result" class="result-container"></div>
</div>
<script src="script.js"></script>
</body>
</html>

View File

@@ -0,0 +1,102 @@
// ⚠️ COMPILARE PRIMA DI INIZIARE
// Inserisci l'URL del server corretto
const BASE_URL = 'http://192.168.1.7:3000/api';
// ELEMENTI DEL DOM (controlla che siano corretti)
const userId = document.querySelector('#userId');
const loading = document.querySelector('#loading');
const result = document.querySelector('#result');
/**
* FUNZIONE: Crea utente card
*
* Crea la card completa dell'utente e la inserisce nell'elemento result
* L'oggetto user ha questa struttura:
* {
* id: number,
* nome: string,
* cognome: string,
* email: string,
* avatar: string (url),
* dataNascita: string (formato YYYY-MM-DD),
* comune: string,
* attivo: boolean
* }
*/
function createUserCard(user) {
result.innerHTML = `
<div class="user-card">
<div class="card-header">
<img src="${user.avatar}" alt="Avatar" class="avatar">
<div class="user-info">
<h2>${user.nome} ${user.cognome}</h2>
<p class="email">${user.email}</p>
<p class="comune">${user.comune}</p>
</div>
</div>
<div class="card-body">
<div class="detail">
<strong>Data Nascita:</strong>
<span>${user.dataNascita}</span>
</div>
<div class="detail">
<strong class="status ${user.attivo ? 'attivo' : 'inattivo'}">
Status:
</strong>
<span>${user.attivo ? 'Attivo' : 'Inattivo'}</span>
</div>
</div>
</div>
`;
}
/**
* FUNZIONE: Gestione errori
*
* Mostra un messaggio di errore nell'elemento result
* e logga l'errore in console (per debug)
*/
function handleError(message) {
result.innerHTML = '';
let div = document.createElement('div');
div.className = 'error';
let strong = document.createElement('strong');
strong.textContent = `${message}`;
div.appendChild(strong);
result.appendChild(div);
console.error('Errore:', message);
}
/**
* FUNZIONE: Fetch singolo utente
*
* Questa funzione deve recuperare l'ID utente dall'input,
* fare una chiamata GET a BASE_URL + "/users/" + id e mostrare i dati
*
* Passi:
* 1. Leggi l'ID utente dall'input
* 2. Controlla che l'ID sia valido, ovvero un numero tra 1 e 40
* In caso contrario, mostra un messaggio di errore (usa handleError()) e return
* 3. Mostra lo spinner di caricamento (rimuovi la classe nascosto)
* 4. Fai una fetch GET a /users/{id}
* 5. Se la risposta non è OK, usa handleError() per mostrare un messaggio e return
* 6. Converti la risposta in JSON
* 7. Mostra i dati dell'utente chiamando createUserCard(user)
* 8. Nascondi lo spinner di caricamento (aggiungi la classe nascosto)
*/
async function fetchUser() {
// PLACEHOLDER - Rimuovi questa riga quando completi
handleError('Codice non implementato - Completa la funzione fetchUser()');
}
// COLLEGA IL BOTTONE AL CLICK
document.getElementById('btnFetch').addEventListener('click', fetchUser);

View File

@@ -0,0 +1,264 @@
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: center;
min-height: 100vh;
margin: 0;
padding: 20px;
}
.app-container {
background: white;
width: 100%;
max-width: 450px;
padding: 30px;
border-radius: 15px;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
}
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 {
background: #f9f9f9;
padding: 20px;
border-radius: 10px;
margin-bottom: 25px;
}
.config-box label {
display: block;
color: #555;
font-weight: 600;
margin-bottom: 10px;
font-size: 0.95rem;
}
.input-group {
display: flex;
gap: 10px;
}
.input-group input {
flex: 1;
padding: 10px;
border: 2px solid #ddd;
border-radius: 6px;
font-size: 15px;
font-family: inherit;
}
.input-group input:focus {
outline: none;
border-color: #007bff;
box-shadow: 0 0 5px rgba(0, 123, 255, 0.2);
}
.input-group button {
padding: 10px 20px;
background: #007bff;
color: white;
border: none;
border-radius: 6px;
font-weight: bold;
cursor: pointer;
transition: background 0.2s;
font-size: 15px;
}
.input-group button:hover {
background: #0056b3;
}
.loading {
text-align: center;
color: #666;
font-size: 1.1rem;
padding: 30px;
animation: pulse 1s infinite;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.6; }
}
.result-container {
margin-bottom: 30px;
}
.error {
background: #fee;
color: #c00;
padding: 15px;
border-radius: 6px;
border-left: 4px solid #c00;
font-weight: 500;
}
.user-card {
background: #f9f9f9;
border-radius: 10px;
overflow: hidden;
box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
animation: slideIn 0.3s ease-out;
}
@keyframes slideIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.card-header {
background: linear-gradient(135deg, #007bff 0%, #0056b3 100%);
color: white;
padding: 20px;
display: flex;
gap: 15px;
align-items: center;
}
.avatar {
width: 70px;
height: 70px;
border-radius: 50%;
border: 3px solid white;
object-fit: cover;
flex-shrink: 0;
}
.user-info {
flex: 1;
}
.user-info h2 {
font-size: 1.3em;
margin: 0 0 5px 0;
}
.user-info p {
margin: 3px 0;
opacity: 0.95;
font-size: 0.9em;
}
.card-body {
padding: 20px;
}
.detail {
display: flex;
justify-content: space-between;
padding: 10px 0;
border-bottom: 1px solid #eee;
}
.detail:last-child {
border-bottom: none;
}
.detail strong {
color: #333;
}
.detail span {
color: #666;
text-align: right;
}
.status {
display: inline-block;
padding: 4px 10px;
border-radius: 15px;
font-size: 0.85em;
}
.status.attivo {
background: #d4edda;
color: #155724;
}
.status.inattivo {
background: #f8d7da;
color: #721c24;
}
.instructions-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 ol {
margin-left: 20px;
color: #555;
line-height: 1.7;
}
.instructions-box li {
margin-bottom: 10px;
}
.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.85em;
color: #333;
margin: 0;
}
.nascosto {
display: none;
}