Add queue APIs
This commit is contained in:
parent
4e74bbb443
commit
3775191fd1
4 changed files with 382 additions and 0 deletions
|
@ -192,6 +192,176 @@ async function sendMolinellaCoordinates() {
|
|||
}
|
||||
}
|
||||
|
||||
// Queue CRUD Operations
|
||||
|
||||
// Add song to queue
|
||||
async function addQueueItem() {
|
||||
try {
|
||||
console.log('🎵 Aggiunta canzone alla queue...');
|
||||
showStatus('🎵 Aggiunta canzone alla queue...', true);
|
||||
|
||||
// Canzone hardcoded
|
||||
const song = {
|
||||
"titolo": "Bohemian Rhapsody",
|
||||
"coverUrl": "https://i.scdn.co/image/abc123",
|
||||
"color": "#3b5998",
|
||||
"artista": "Queen",
|
||||
"voti": 0
|
||||
};
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}/queue/add`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(song)
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (response.ok) {
|
||||
showStatus('✅ Canzone aggiunta alla queue!', true);
|
||||
showResult(data);
|
||||
} else {
|
||||
showStatus(`❌ Errore: ${data.detail || 'Errore sconosciuto'}`, false);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Errore aggiunta canzone:', error);
|
||||
showStatus(`❌ Errore di connessione: ${error.message}`, false);
|
||||
}
|
||||
}
|
||||
|
||||
// Read queue
|
||||
async function readQueue() {
|
||||
try {
|
||||
console.log('📋 Lettura queue...');
|
||||
showStatus('📋 Lettura queue...', true);
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}/queue/read`);
|
||||
const data = await response.json();
|
||||
|
||||
if (response.ok) {
|
||||
showStatus(`✅ Queue letta! ${data.count} canzoni trovate`, true);
|
||||
showResult(data);
|
||||
} else {
|
||||
showStatus(`❌ Errore: ${data.detail || 'Errore sconosciuto'}`, false);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Errore lettura queue:', error);
|
||||
showStatus(`❌ Errore di connessione: ${error.message}`, false);
|
||||
}
|
||||
}
|
||||
|
||||
// Vote up (incrementa voti)
|
||||
async function voteUp() {
|
||||
try {
|
||||
console.log('👍 Voto positivo...');
|
||||
showStatus('👍 Voto positivo...', true);
|
||||
|
||||
// Per semplicità, vota il primo item della queue
|
||||
const response = await fetch(`${API_BASE_URL}/queue/read`);
|
||||
const queueData = await response.json();
|
||||
|
||||
if (response.ok && queueData.data.length > 0) {
|
||||
const firstItemId = queueData.data[0].id;
|
||||
|
||||
const voteResponse = await fetch(`${API_BASE_URL}/queue/vote/${firstItemId}`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ "increment": true })
|
||||
});
|
||||
|
||||
const voteData = await voteResponse.json();
|
||||
|
||||
if (voteResponse.ok) {
|
||||
showStatus('✅ Voto positivo aggiunto!', true);
|
||||
showResult(voteData);
|
||||
} else {
|
||||
showStatus(`❌ Errore: ${voteData.detail || 'Errore sconosciuto'}`, false);
|
||||
}
|
||||
} else {
|
||||
showStatus('❌ Nessuna canzone nella queue da votare', false);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Errore voto positivo:', error);
|
||||
showStatus(`❌ Errore di connessione: ${error.message}`, false);
|
||||
}
|
||||
}
|
||||
|
||||
// Vote down (decrementa voti)
|
||||
async function voteDown() {
|
||||
try {
|
||||
console.log('👎 Voto negativo...');
|
||||
showStatus('👎 Voto negativo...', true);
|
||||
|
||||
// Per semplicità, vota il primo item della queue
|
||||
const response = await fetch(`${API_BASE_URL}/queue/read`);
|
||||
const queueData = await response.json();
|
||||
|
||||
if (response.ok && queueData.data.length > 0) {
|
||||
const firstItemId = queueData.data[0].id;
|
||||
|
||||
const voteResponse = await fetch(`${API_BASE_URL}/queue/vote/${firstItemId}`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ "increment": false })
|
||||
});
|
||||
|
||||
const voteData = await voteResponse.json();
|
||||
|
||||
if (voteResponse.ok) {
|
||||
showStatus('✅ Voto negativo aggiunto!', true);
|
||||
showResult(voteData);
|
||||
} else {
|
||||
showStatus(`❌ Errore: ${voteData.detail || 'Errore sconosciuto'}`, false);
|
||||
}
|
||||
} else {
|
||||
showStatus('❌ Nessuna canzone nella queue da votare', false);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Errore voto negativo:', error);
|
||||
showStatus(`❌ Errore di connessione: ${error.message}`, false);
|
||||
}
|
||||
}
|
||||
|
||||
// Delete item from queue
|
||||
async function deleteQueueItem() {
|
||||
try {
|
||||
console.log('🗑️ Eliminazione item dalla queue...');
|
||||
showStatus('🗑️ Eliminazione item dalla queue...', true);
|
||||
|
||||
// Per semplicità, elimina il primo item della queue
|
||||
const response = await fetch(`${API_BASE_URL}/queue/read`);
|
||||
const queueData = await response.json();
|
||||
|
||||
if (response.ok && queueData.data.length > 0) {
|
||||
const firstItemId = queueData.data[0].id;
|
||||
|
||||
const deleteResponse = await fetch(`${API_BASE_URL}/queue/delete/${firstItemId}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
|
||||
const deleteData = await deleteResponse.json();
|
||||
|
||||
if (deleteResponse.ok) {
|
||||
showStatus('✅ Item eliminato dalla queue!', true);
|
||||
showResult(deleteData);
|
||||
} else {
|
||||
showStatus(`❌ Errore: ${deleteData.detail || 'Errore sconosciuto'}`, false);
|
||||
}
|
||||
} else {
|
||||
showStatus('❌ Nessuna canzone nella queue da eliminare', false);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Errore eliminazione item:', error);
|
||||
showStatus(`❌ Errore di connessione: ${error.message}`, false);
|
||||
}
|
||||
}
|
||||
|
||||
// Test di connessione all'avvio
|
||||
window.addEventListener('load', async () => {
|
||||
try {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue