forked from Ananya-te/MusicTrackerAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusicAPI.html
More file actions
169 lines (148 loc) Ā· 4.1 KB
/
MusicAPI.html
File metadata and controls
169 lines (148 loc) Ā· 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>šµ Music Tracker</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Inter', sans-serif;
background: #f7f8fc;
color: #333;
margin: 0;
padding: 20px;
}
h1 {
text-align: center;
color: #4a4e69;
margin-bottom: 10px;
}
form {
background-color: #fff;
max-width: 400px;
margin: 0 auto;
padding: 20px;
border-radius: 12px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
}
input {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 8px;
font-size: 16px;
}
button {
width: 100%;
padding: 10px;
background-color: #6c63ff;
color: white;
font-weight: 600;
border: none;
border-radius: 8px;
cursor: pointer;
transition: background 0.3s;
}
button:hover {
background-color: #574b90;
}
#songList {
list-style-type: none;
padding: 0;
max-width: 600px;
margin: 30px auto;
}
#songList li {
background: #fff;
margin-bottom: 10px;
padding: 15px;
border-left: 5px solid #6c63ff;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
h2 {
text-align: center;
margin-top: 40px;
color: #333;
}
.message {
text-align: center;
font-weight: 600;
margin-top: 10px;
}
.success {
color: green;
}
.error {
color: red;
}
</style>
</head>
<body>
<h1>šµ Music Tracker</h1>
<form id="songForm">
<input type="text" id="title" placeholder="š¼ Song Title" required>
<input type="text" id="artist" placeholder="š¤ Artist Name" required>
<input type="text" id="genre" placeholder="š¶ Genre" required>
<button type="submit">ā Add Song</button>
<div id="message" class="message"></div>
</form>
<h2>š§ All Songs</h2>
<ul id="songList"></ul>
<script>
function loadSongs() {
fetch('http://localhost:5000/api/songs')
.then(response => {
if (!response.ok) throw new Error('Failed to fetch songs');
return response.json();
})
.then(data => {
const songList = document.getElementById('songList');
songList.innerHTML = '';
data.forEach(song => {
const li = document.createElement('li');
li.textContent = `${song.title} by ${song.artist} (${song.genre})`;
songList.appendChild(li);
});
})
.catch(err => {
console.error('Error fetching songs:', err);
showMessage('Failed to fetch songs.', 'error');
});
}
function showMessage(text, type) {
const message = document.getElementById('message');
message.textContent = text;
message.className = `message ${type}`;
setTimeout(() => message.textContent = '', 3000);
}
document.getElementById('songForm').addEventListener('submit', function(e) {
e.preventDefault();
const title = document.getElementById('title').value;
const artist = document.getElementById('artist').value;
const genre = document.getElementById('genre').value;
fetch('http://localhost:5000/api/songs', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title, artist, genre })
})
.then(response => {
if (!response.ok) throw new Error('Failed to add song');
return response.json();
})
.then(data => {
showMessage('ā
Song added successfully!', 'success');
loadSongs();
document.getElementById('songForm').reset();
})
.catch(error => {
console.error('Error adding song:', error);
showMessage('ā Could not add song.', 'error');
});
});
window.onload = loadSongs;
</script>
</body>
</html>