Skip to content

Commit

Permalink
fix: sql query
Browse files Browse the repository at this point in the history
  • Loading branch information
adhimrahman committed May 13, 2024
1 parent f95c94a commit 93843dd
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 54 deletions.
8 changes: 4 additions & 4 deletions utils/UserInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function getAllMyUserArtistFollow(userId) {
async function getAllMyUserAlbumFollow(userId) {
return new Promise((resolve, reject) => {
const sql = `
SELECT a.name, ar.name as artistName
SELECT a.name, ar.name artistName
FROM user_album_follow uaf
JOIN album a ON uaf.album_id = a.id
JOIN album_artist_has aah ON a.id = aah.album_id
Expand All @@ -46,7 +46,7 @@ async function getAllMyUserAlbumFollow(userId) {
async function getAllMyUserCreatePlaylist(userId) {
return new Promise((resolve, reject) => {
const sql = `
SELECT p.name, us.name as creator_name
SELECT p.name, us.name creator_name
FROM user_playlist_create upc
JOIN playlist p ON upc.playlist_id = p.id
JOIN user us ON upc.user_id = us.id
Expand All @@ -62,7 +62,7 @@ async function getAllMyUserCreatePlaylist(userId) {
async function getAllMyUserFollowPlaylist(userId) {
return new Promise((resolve, reject) => {
const sql = `
SELECT p.name, u.name AS creator_name
SELECT p.name, u.name creator_name
FROM user_playlist_follow upf
JOIN user_playlist_create upc ON upf.playlist_id = upc.playlist_id
JOIN playlist p ON upf.playlist_id = p.id
Expand All @@ -79,7 +79,7 @@ async function getAllMyUserFollowPlaylist(userId) {
async function getAllMyUserLikedSong(userId) {
return new Promise((resolve, reject) => {
const sql = `
SELECT s.name, ar.name AS artistName
SELECT s.name, ar.name artistName
FROM user_song_liked usl
JOIN song s ON usl.song_id = s.id
JOIN song_artist_sing sas ON s.id = sas.song_id
Expand Down
10 changes: 5 additions & 5 deletions utils/callTables.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const connection = mysql.createConnection({ host: 'localhost', user: 'root', pas
async function getAllPlaylists() {
return new Promise((resolve, reject) => {
const sql = `
SELECT p.id, p.name, u.name AS creator_name
SELECT p.id, p.name, u.name creator_name
FROM playlist p
JOIN user_playlist_create upc ON p.id = upc.playlist_id
JOIN user u ON u.id = upc.user_id
Expand All @@ -22,7 +22,7 @@ async function getAllPlaylists() {
async function getAllAlbums() {
return new Promise((resolve, reject) => {
const sql = `
SELECT a.id, a.name, artist.name AS artistName
SELECT a.id, a.name, artist.name artistName
FROM album_artist_has aa
JOIN album a ON aa.album_id = a.id
JOIN artist ON aa.artist_id = artist.id
Expand All @@ -36,7 +36,7 @@ async function getAllAlbums() {

async function getAllArtists() {
return new Promise((resolve, reject) => {
const sql = 'SELECT id, name FROM artist ORDER BY followers DESC';
const sql = 'SELECT id, name FROM artist ORDER BY followers DESC;';
// const sql = 'SELECT id, name FROM artist';
connection.query(sql, (err, results) => {
if (err) { reject(err);
Expand All @@ -48,11 +48,11 @@ async function getAllArtists() {
async function getAllSongs() {
return new Promise((resolve, reject) => {
const sql = `
SELECT s.id, s.name, artist.name AS artistName
SELECT s.id, s.name, artist.name artistName
FROM song_artist_sing sa
JOIN song s ON sa.song_id = s.id
JOIN artist ON sa.artist_id = artist.id
LIMIT 50
LIMIT 0, 50;
`;
connection.query(sql, (err, results) => {
if (err) { reject(err);
Expand Down
8 changes: 4 additions & 4 deletions utils/callTables2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const connection = mysql.createConnection({ host: 'localhost', user: 'root', pas
async function getAllMySongs(artistId) {
return new Promise((resolve, reject) => {
const sql = `
SELECT s.id, s.name, s.genre, s.duration, artist.name AS artist_name
SELECT s.id, s.name, s.genre, s.duration, ar.name artist_name
FROM song_artist_sing sa
JOIN song s ON sa.song_id = s.id
JOIN artist ON sa.artist_id = artist.id
JOIN artist ar ON sa.artist_id = ar.id
WHERE sa.artist_id = ?
`;
connection.query(sql, [artistId], (err, results) => {
Expand All @@ -20,10 +20,10 @@ async function getAllMySongs(artistId) {
async function getAllMyAlbums(artistId) {
return new Promise((resolve, reject) => {
const sql = `
SELECT a.id, a.name, a.num_song, a.duration, artist.name AS artist_name
SELECT a.id, a.name, a.num_song, a.duration, ar.name artist_name
FROM album_artist_has aa
JOIN album a ON aa.album_id = a.id
JOIN artist ON aa.artist_id = artist.id
JOIN artist ar ON aa.artist_id = ar.id
WHERE aa.artist_id = ?
`;
connection.query(sql, [artistId], (err, results) => {
Expand Down
10 changes: 5 additions & 5 deletions utils/checkDupeEntity.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ function checkCredentials(table, email, password) {

function checkIfEmailExists(email) {
return new Promise((resolve, reject) => {
const sql = "SELECT COUNT(*) AS count FROM user WHERE email = ?";
const sql = "SELECT COUNT(id) count FROM user WHERE email = ?";
connection.query(sql, [email], (err, result) => { if (err) { reject(err); } else { resolve(result[0].count > 0); } });
});
}

function checkIfUsernameExists(username) {
return new Promise((resolve, reject) => {
const sql = "SELECT COUNT(*) AS count FROM user WHERE name = ?";
const sql = "SELECT COUNT(id) count FROM user WHERE name = ?";
connection.query(sql, [username], (err, result) => { if (err) { reject(err); } else { resolve(result[0].count > 0); } });
});
}

async function checkDuplicatePlaylist(name) {
return new Promise((resolve, reject) => {
const sql = 'SELECT COUNT(*) AS count FROM playlist WHERE name = ?';
const sql = 'SELECT COUNT(id) count FROM playlist WHERE name = ?';
connection.query(sql, [name], (err, result) => {
if (err) { reject(err);
} else { resolve(result[0].count > 0); }
Expand All @@ -39,7 +39,7 @@ async function checkDuplicatePlaylist(name) {

async function checkDuplicateAlbum(name) {
return new Promise((resolve, reject) => {
const sql = 'SELECT COUNT(*) AS count FROM album WHERE name = ?';
const sql = 'SELECT COUNT(id) count FROM album WHERE name = ?';
connection.query(sql, [name], (err, result) => {
if (err) { reject(err);
} else { resolve(result[0].count > 0); }
Expand All @@ -49,7 +49,7 @@ async function checkDuplicateAlbum(name) {

async function checkDuplicateSong(name) {
return new Promise((resolve, reject) => {
const sql = 'SELECT COUNT(*) AS count FROM song WHERE name = ?';
const sql = 'SELECT COUNT(id) count FROM song WHERE name = ?';
connection.query(sql, [name], (err, result) => {
if (err) { reject(err);
} else { resolve(result[0].count > 0); }
Expand Down
10 changes: 5 additions & 5 deletions utils/checkDupeRelation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const mysql = require('mysql');
const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'spotitip' });

async function checkIfUserAlbumFollow(userId, albumId) {
const sql = 'SELECT COUNT(*) AS count FROM user_album_follow WHERE user_id = ? AND album_id = ?';
const sql = 'SELECT COUNT(*) count FROM user_album_follow WHERE user_id = ? AND album_id = ?';
return new Promise((resolve, reject) => {
connection.query(sql, [userId, albumId], (err, rows) => {
if (err) {
Expand All @@ -15,7 +15,7 @@ async function checkIfUserAlbumFollow(userId, albumId) {
}

async function checkIfUserArtistFollow(userId, artistId) {
const sql = 'SELECT COUNT(*) AS count FROM user_artist_follow WHERE user_id = ? AND artist_id = ?';
const sql = 'SELECT COUNT(*) count FROM user_artist_follow WHERE user_id = ? AND artist_id = ?';
return new Promise((resolve, reject) => {
connection.query(sql, [userId, artistId], (err, rows) => {
if (err) {
Expand All @@ -28,20 +28,20 @@ async function checkIfUserArtistFollow(userId, artistId) {
}

async function checkIfUserPlaylistFollow(userId, playlistId) {
const sql = 'SELECT COUNT(*) AS count FROM user_playlist_follow WHERE user_id = ? AND playlist_id = ?';
const sql = 'SELECT COUNT(*) count FROM user_playlist_follow WHERE user_id = ? AND playlist_id = ?';
return new Promise((resolve, reject) => {
connection.query(sql, [userId, playlistId], (err, rows) => {
if (err) {
reject(err);
} else {
} else {
resolve(rows[0].count > 0);
}
});
});
}

async function checkIfUserLikedSong(userId, songId) {
const sql = 'SELECT COUNT(*) AS count FROM user_song_liked WHERE user_id = ? AND song_id = ?';
const sql = 'SELECT COUNT(*) count FROM user_song_liked WHERE user_id = ? AND song_id = ?';
return new Promise((resolve, reject) => {
connection.query(sql, [userId, songId], (err, rows) => {
if (err) {
Expand Down
4 changes: 2 additions & 2 deletions utils/colsUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const mysql = require('mysql');
const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'spotitip' });

async function countSongsInPlaylist(playlistId) {
const query = `SELECT COUNT(*) AS count FROM song_playlist_contains WHERE playlist_id = ?;`;
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) => {
Expand All @@ -29,7 +29,7 @@ async function updateNumSongsInPlaylist(playlistId, numSongsPlaylist) {
}

async function countSongsInAlbum(albumId) {
const query = 'SELECT COUNT(*) AS count FROM song_album_contains WHERE album_id = ?';
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) => {
Expand Down
8 changes: 4 additions & 4 deletions utils/generateID.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const connection = mysql.createConnection({ host: 'localhost', user: 'root', pas
function generateNewID(table, type) {
return new Promise((resolve, reject) => {
const prefix = type === "user" ? "US" : "AR";
const sql = `SELECT MAX(id) AS maxId FROM ${table} WHERE id LIKE '${prefix}%'`;
const sql = `SELECT MAX(id)maxId FROM ${table} WHERE id LIKE '${prefix}%'`;
connection.query(sql, (err, result) => {
if (err) { reject(err);
} else {
Expand All @@ -23,7 +23,7 @@ function generateNewID(table, type) {

function generatePlaylistID() {
return new Promise((resolve, reject) => {
const sql = "SELECT MAX(id) AS maxId FROM playlist";
const sql = "SELECT MAX(id) maxId FROM playlist";
connection.query(sql, (err, result) => {
if (err) {
reject(err);
Expand All @@ -42,7 +42,7 @@ function generatePlaylistID() {

function generateAlbumID() {
return new Promise((resolve, reject) => {
const sql = "SELECT MAX(id) AS maxId FROM album";
const sql = "SELECT MAX(id) maxId FROM album";
connection.query(sql, (err, result) => {
if (err) {
reject(err);
Expand All @@ -61,7 +61,7 @@ function generateAlbumID() {

function generateSongID() {
return new Promise((resolve, reject) => {
const sql = "SELECT MAX(id) AS maxId FROM song";
const sql = "SELECT MAX(id) maxId FROM song";
connection.query(sql, (err, result) => {
if (err) {
reject(err);
Expand Down
4 changes: 2 additions & 2 deletions utils/searchDatas.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ async function getAllDatas(keyword) {
SELECT name, 'user' AS type, NULL AS creator_name
FROM user
WHERE name LIKE ?
UNION
UNION
SELECT name, 'artist' AS type, NULL AS creator_name
FROM artist
FROM artist
WHERE name LIKE ?
UNION
SELECT p.name AS name, 'playlist' AS type, u.name AS creator_name
Expand Down
35 changes: 12 additions & 23 deletions utils/songContains.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ async function getAllPlaylists2(userId) {
return new Promise((resolve, reject) => {
const sql = `
SELECT p.id, p.name,
COUNT(spc.song_id) AS num_song, p.duration, upc.date_created
COUNT(spc.song_id) num_song, p.duration, upc.date_created
FROM playlist p
JOIN user_playlist_create upc ON p.id = upc.playlist_id
LEFT JOIN song_playlist_contains spc ON p.id = spc.playlist_id
Expand All @@ -25,7 +25,7 @@ async function getAllPlaylists2(userId) {
async function getSongsForPlaylist(playlistId) {
return new Promise((resolve, reject) => {
const sql = `
SELECT s.id, s.name AS title, s.genre, s.duration, s.listeners, a.name AS artist
SELECT s.id, s.name title, s.genre, s.duration, s.listeners, a.name artist
FROM song s
JOIN song_playlist_contains spc ON s.id = spc.song_id
JOIN song_artist_sing sas ON s.id = sas.song_id
Expand Down Expand Up @@ -63,12 +63,13 @@ async function getAllAlbums2(artistId) {
return new Promise((resolve, reject) => {
const sql = `
SELECT a.id, a.name,
COUNT(sac.song_id) AS num_song, a.duration, aah.date_created
COUNT(sac.song_id) num_song, a.duration, aah.date_created
FROM album a
JOIN album_artist_has aah ON a.id = aah.album_id
LEFT JOIN song_album_contains sac ON a.id = sac.album_id
JOIN song_album_contains sac ON a.id = sac.album_id
WHERE aah.artist_id = ?
GROUP BY a.id, a.name, a.duration, aah.date_created;
-- GROUP BY a.id, a.name, a.duration, aah.date_created;
GROUP BY 1, 2, 4, 5;
`;
connection.query(sql, [artistId], (err, results) => {
if (err) {
Expand All @@ -80,7 +81,6 @@ async function getAllAlbums2(artistId) {
});
}


// async function getSongsForAlbum(albumId) {
// return new Promise((resolve, reject) => {
// const sql = `
Expand All @@ -101,23 +101,12 @@ async function getAllAlbums2(artistId) {
async function getSongsForAlbum(albumId, artistId) {
return new Promise((resolve, reject) => {
const sql = `
SELECT
s.id,
s.name AS title,
s.genre,
s.duration,
s.listeners,
a.name AS artist
FROM
song s
JOIN
song_artist_sing sas ON s.id = sas.song_id
JOIN
artist a ON sas.artist_id = a.id
JOIN
song_album_contains sac ON s.id = sac.song_id
WHERE
sac.album_id = ? AND sas.artist_id = ?;
SELECT s.id, s.name title, s.genre, s.duration, s.listeners, a.name artist
FROM song s
JOIN song_artist_sing sas ON s.id = sas.song_id
JOIN artist a ON sas.artist_id = a.id
JOIN song_album_contains sac ON s.id = sac.song_id
WHERE sac.album_id = ? AND sas.artist_id = ?;
`;
connection.query(sql, [albumId, artistId], (err, results) => {
if (err) {
Expand Down

0 comments on commit 93843dd

Please sign in to comment.