-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolsUpdate.js
61 lines (56 loc) · 2.14 KB
/
colsUpdate.js
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
const mysql = require('mysql');
const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'spotitip' });
async function countSongsInPlaylist(playlistId) {
const query = `SELECT COUNT(*) count FROM song_playlist_contains WHERE playlist_id = ?;`;
try {
const result = await new Promise((resolve, reject) => {
connection.query(query, [playlistId], (error, results) => {
if (error) { reject(error);
} else { resolve(results); }
});
});
const numSongsPlaylist = result[0].count;
return numSongsPlaylist;
} catch (error) {
console.error('Error counting songs in playlist:', error);
throw error;
}
}
async function updateNumSongsInPlaylist(playlistId, numSongsPlaylist) {
const query = `UPDATE playlist SET num_song = ? WHERE id = ?;`;
try {
await connection.query(query, [numSongsPlaylist, playlistId]);
} catch (error) {
console.error('Error updating num_song in playlist:', error);
throw error;
}
}
async function countSongsInAlbum(albumId) {
const query = 'SELECT COUNT(*) count FROM song_album_contains WHERE album_id = ?';
try {
const result = await new Promise((resolve, reject) => {
connection.query(query, [albumId], (error, results) => {
if (error) { reject(error); }
else { resolve(results); }
});
});
const numSongsAlbum = result[0].count;
return numSongsAlbum;
} catch (error) {
console.error('Error counting songs in album:', error);
throw error;
}
}
async function updateNumSongsInAlbum(albumId, numSongsAlbum) {
const query = 'UPDATE album SET num_song = ? WHERE id = ?';
return new Promise((resolve, reject) => {
connection.query(query, [numSongsAlbum, albumId], (error, results) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
}
module.exports = { countSongsInPlaylist, updateNumSongsInPlaylist, countSongsInAlbum, updateNumSongsInAlbum }