-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateID.js
81 lines (76 loc) · 2.78 KB
/
generateID.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const mysql = require('mysql');
const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'spotitip' });
function generateNewID(table, type) {
return new Promise((resolve, reject) => {
const prefix = type === "user" ? "US" : "AR";
const sql = `SELECT MAX(id)maxId FROM ${table} WHERE id LIKE '${prefix}%'`;
connection.query(sql, (err, result) => {
if (err) { reject(err);
} else {
let maxId = result[0].maxId;
if (!maxId) { maxId = prefix + '01';
} else {
const prefixLength = prefix.length;
const numericPart = parseInt(maxId.slice(prefixLength), 10) + 1;
maxId = prefix + (numericPart < 10 ? '0' : '') + numericPart;
}
resolve(maxId);
}
});
});
}
function generatePlaylistID() {
return new Promise((resolve, reject) => {
const sql = "SELECT MAX(id) maxId FROM playlist";
connection.query(sql, (err, result) => {
if (err) {
reject(err);
} else {
let maxId = result[0].maxId;
if (!maxId) { maxId = 'PL01';
} else {
const numericPart = parseInt(maxId.slice(2), 10) + 1;
maxId = 'PL' + (numericPart < 10 ? '0' : '') + numericPart;
}
resolve(maxId);
}
});
});
}
function generateAlbumID() {
return new Promise((resolve, reject) => {
const sql = "SELECT MAX(id) maxId FROM album";
connection.query(sql, (err, result) => {
if (err) {
reject(err);
} else {
let maxId = result[0].maxId;
if (!maxId) { maxId = 'AL01';
} else {
const numericPart = parseInt(maxId.slice(2), 10) + 1;
maxId = 'AL' + (numericPart < 10 ? '0' : '') + numericPart;
}
resolve(maxId);
}
});
});
}
function generateSongID() {
return new Promise((resolve, reject) => {
const sql = "SELECT MAX(id) maxId FROM song";
connection.query(sql, (err, result) => {
if (err) {
reject(err);
} else {
let maxId = result[0].maxId;
if (!maxId) { maxId = 'SO01';
} else {
const numericPart = parseInt(maxId.slice(2), 10) + 1;
maxId = 'SO' + (numericPart < 10 ? '0' : '') + numericPart;
}
resolve(maxId);
}
});
});
}
module.exports = { generateNewID, generatePlaylistID, generateAlbumID, generateSongID }