Esercizio 11 API
This commit is contained in:
62
javascript/JS_Esercizi 11 - API/03_utente_e_post/index.html
Normal file
62
javascript/JS_Esercizi 11 - API/03_utente_e_post/index.html
Normal file
@@ -0,0 +1,62 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="it">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Esercizio 3 - Utente + Post</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>📝 Utente + Post</h1>
|
||||
<p class="subtitle">Fetch multipli e relazioni dati</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 -->
|
||||
<div id="loading" class="loading" style="display: none;">
|
||||
⏳ Caricamento...
|
||||
</div>
|
||||
|
||||
<!-- PROFILO UTENTE -->
|
||||
<div id="userProfile" class="user-profile"></div>
|
||||
|
||||
<!-- POST -->
|
||||
<div id="postsContainer" class="posts-container"></div>
|
||||
|
||||
<!-- ISTRUZIONI -->
|
||||
<div class="instructions-box" style="margin-top: 30px;">
|
||||
<h2>📝 Istruzioni</h2>
|
||||
<ol>
|
||||
<li>Apri <code>script.js</code> e completa <code>fetchUserAndPosts()</code></li>
|
||||
<li>Compila <code>BASE_URL</code></li>
|
||||
<li>Fai due fetch: <code>/users/{id}</code> e <code>/posts</code></li>
|
||||
<li>Usa <code>filter()</code> per relazionare i dati</li>
|
||||
</ol>
|
||||
|
||||
<div class="hint">
|
||||
<strong>💡 Suggerimento:</strong>
|
||||
<pre>const user = await fetch(...);
|
||||
const allPosts = await fetch(...);
|
||||
const userPosts = allPosts.filter(post => post.userId === user.id);</pre>
|
||||
</div>
|
||||
|
||||
<div class="challenge">
|
||||
<strong>🎯 Bonus Challenge:</strong>
|
||||
<p>Ordina i post dal più recente al meno recente usando <code>sort()</code>.</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="script.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
124
javascript/JS_Esercizi 11 - API/03_utente_e_post/script.js
Normal file
124
javascript/JS_Esercizi 11 - API/03_utente_e_post/script.js
Normal file
@@ -0,0 +1,124 @@
|
||||
// ⚠️ COMPILARE PRIMA DI INIZIARE
|
||||
const BASE_URL = 'http://localhost:3000/api';
|
||||
|
||||
/**
|
||||
* ESERCIZIO 3: Recupera un utente E tutti i suoi post
|
||||
*
|
||||
* Devi completare questa funzione:
|
||||
* 1. Leggi l'ID dell'utente
|
||||
* 2. Fai DUE fetch:
|
||||
* - GET /users/{id}
|
||||
* - GET /posts
|
||||
* 3. Filtra i post per trovare solo quelli di questo utente (usando userId)
|
||||
* 4. Mostra i risultati
|
||||
* 5. Gestisci gli errori
|
||||
*/
|
||||
async function fetchUserAndPosts() {
|
||||
const userId = document.getElementById('userId').value;
|
||||
const loading = document.getElementById('loading');
|
||||
const userProfile = document.getElementById('userProfile');
|
||||
const postsContainer = document.getElementById('postsContainer');
|
||||
|
||||
if (!userId || userId < 1 || userId > 40) {
|
||||
userProfile.innerHTML = '<div class="error">❌ Inserisci un ID valido tra 1 e 40</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
loading.style.display = 'block';
|
||||
userProfile.innerHTML = '';
|
||||
postsContainer.innerHTML = '';
|
||||
|
||||
try {
|
||||
// 👇 SCRIVI QUI IL TUO CODICE 👇
|
||||
// 1. Fetch utente
|
||||
// const userResponse = await fetch(...);
|
||||
// const user = await userResponse.json();
|
||||
|
||||
// 2. Fetch tutti i post
|
||||
// const postsResponse = await fetch(...);
|
||||
// const allPosts = await postsResponse.json();
|
||||
|
||||
// 3. Filtra i post di questo utente
|
||||
// const userPosts = allPosts.filter(post => post.userId === user.id);
|
||||
|
||||
// 4. Visualizza
|
||||
// displayUserWithPosts(user, userPosts);
|
||||
|
||||
// PLACEHOLDER - Rimuovi questa riga quando completi
|
||||
throw new Error('Codice non implementato - Completa la funzione fetchUserAndPosts()');
|
||||
|
||||
} catch (error) {
|
||||
userProfile.innerHTML = `
|
||||
<div class="error">
|
||||
<strong>❌ Errore:</strong> ${error.message}
|
||||
</div>
|
||||
`;
|
||||
console.error('Errore:', error);
|
||||
} finally {
|
||||
loading.style.display = 'none';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Visualizza utente e post
|
||||
* (Questa funzione è già fatta - non modificare)
|
||||
*/
|
||||
function displayUserWithPosts(user, posts) {
|
||||
const userProfile = document.getElementById('userProfile');
|
||||
const postsContainer = document.getElementById('postsContainer');
|
||||
|
||||
// CARD UTENTE
|
||||
const userHTML = `
|
||||
<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="location">📍 ${user.comune}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
userProfile.innerHTML = userHTML;
|
||||
|
||||
// POST DELL'UTENTE
|
||||
if (!posts || posts.length === 0) {
|
||||
postsContainer.innerHTML = `
|
||||
<div class="posts-section">
|
||||
<h2>📄 Post (0)</h2>
|
||||
<div class="empty">Questo utente non ha scritto nessun post</div>
|
||||
</div>
|
||||
`;
|
||||
return;
|
||||
}
|
||||
|
||||
const postsHTML = `
|
||||
<div class="posts-section">
|
||||
<h2>📄 Post (${posts.length})</h2>
|
||||
${posts.map(post => `
|
||||
<div class="post-card">
|
||||
<div class="post-header">
|
||||
<h3>${post.titolo}</h3>
|
||||
<span class="post-date">📅 ${post.data}</span>
|
||||
</div>
|
||||
<p class="post-content">${post.contenuto}</p>
|
||||
<div class="post-footer">
|
||||
<span class="likes">❤️ ${post.likes} likes</span>
|
||||
</div>
|
||||
</div>
|
||||
`).join('')}
|
||||
</div>
|
||||
`;
|
||||
|
||||
postsContainer.innerHTML = postsHTML;
|
||||
}
|
||||
|
||||
// COLLEGA IL BOTTONE
|
||||
document.getElementById('btnFetch').addEventListener('click', fetchUserAndPosts);
|
||||
|
||||
// PERMETTI ENTER
|
||||
document.getElementById('userId').addEventListener('keypress', (e) => {
|
||||
if (e.key === 'Enter') fetchUserAndPosts();
|
||||
});
|
||||
249
javascript/JS_Esercizi 11 - API/03_utente_e_post/style.css
Normal file
249
javascript/JS_Esercizi 11 - API/03_utente_e_post/style.css
Normal file
@@ -0,0 +1,249 @@
|
||||
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: 600px;
|
||||
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 {
|
||||
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;
|
||||
}
|
||||
|
||||
.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;
|
||||
}
|
||||
|
||||
.input-group button:hover {
|
||||
background: #0056b3;
|
||||
}
|
||||
|
||||
.loading {
|
||||
text-align: center;
|
||||
color: #666;
|
||||
font-size: 1rem;
|
||||
padding: 30px;
|
||||
animation: pulse 1s infinite;
|
||||
}
|
||||
|
||||
@keyframes pulse {
|
||||
0%, 100% { opacity: 1; }
|
||||
50% { opacity: 0.6; }
|
||||
}
|
||||
|
||||
.error {
|
||||
background: #fee;
|
||||
color: #c00;
|
||||
padding: 15px;
|
||||
border-radius: 6px;
|
||||
border-left: 4px solid #c00;
|
||||
}
|
||||
|
||||
.user-card {
|
||||
background: #f9f9f9;
|
||||
border-radius: 10px;
|
||||
overflow: hidden;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.card-header {
|
||||
background: linear-gradient(135deg, #007bff 0%, #0056b3 100%);
|
||||
color: white;
|
||||
padding: 20px;
|
||||
display: flex;
|
||||
gap: 15px;
|
||||
}
|
||||
|
||||
.avatar {
|
||||
width: 70px;
|
||||
height: 70px;
|
||||
border-radius: 50%;
|
||||
border: 2px solid white;
|
||||
object-fit: cover;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.user-info h2 {
|
||||
margin: 0 0 5px 0;
|
||||
font-size: 1.2rem;
|
||||
}
|
||||
|
||||
.user-info p {
|
||||
margin: 3px 0;
|
||||
opacity: 0.95;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.posts-section h2 {
|
||||
color: #333;
|
||||
margin: 25px 0 15px 0;
|
||||
font-size: 1.2rem;
|
||||
border-bottom: 2px solid #007bff;
|
||||
padding-bottom: 10px;
|
||||
}
|
||||
|
||||
.post-card {
|
||||
background: #f9f9f9;
|
||||
padding: 15px;
|
||||
border-radius: 8px;
|
||||
border-left: 4px solid #007bff;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.post-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.post-header h3 {
|
||||
color: #333;
|
||||
margin: 0;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.post-date {
|
||||
color: #999;
|
||||
font-size: 0.8rem;
|
||||
}
|
||||
|
||||
.post-content {
|
||||
color: #555;
|
||||
line-height: 1.5;
|
||||
font-size: 0.95rem;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.post-footer {
|
||||
color: #d63384;
|
||||
font-weight: 600;
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
.empty {
|
||||
color: #999;
|
||||
text-align: center;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.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;
|
||||
}
|
||||
Reference in New Issue
Block a user