<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Peanut's ONLINE MUSIC Shop</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #fff5e1;
}
.container {
text-align: center;
margin: 30px;
} input[type="text"] {
padding: 10px;
} button {
padding: 10px;
background-color: #ffa726;
border: none;
color: white;
} ul {
list-style: none;
padding: 0;
} li {
margin: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>Peanut's ONLINE MUSIC Shop</h1>
<input type="text" id="title" placeholder="Song Title">
<input type="text" id="artist" placeholder="Artist">
<button onclick="addSong()">Add to Playlist</button>
<button onclick="fetchPlaylist()">Display Playlist</button>
<ul id="playlist"></ul>
</div> <script>
async function addSong() {
const title = document.getElementById('title').value;
const artist = document.getElementById('artist').value;
if (title && artist) {
const response = await fetch('/api/addSong', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ title, artist })
}); if (response.ok) {
document.getElementById('title').value = '';
document.getElementById('artist').value = '';
}
}
} async function fetchPlaylist() {
const response = await fetch('/api/getPlaylist');
if (response.ok) {
const playlist = await response.json();
const playlistElement = document.getElementById('playlist');
playlistElement.innerHTML = ''; // Clear the list before re-rendering
playlist.forEach(song => {
const li = document.createElement('li');
li.textContent = `${song.title} by ${song.artist}`;
playlistElement.appendChild(li);
});
}
} // Initial playlist fetch
fetchPlaylist();
</script>
</body>
</html>