forked from external-repos/noisedash
Add sample support, re-organize repo
This commit is contained in:
@@ -8,7 +8,9 @@ module.exports = function () {
|
||||
hashed_password BLOB,
|
||||
salt BLOB,
|
||||
name TEXT,
|
||||
is_admin INTEGER)`
|
||||
is_admin INTEGER,
|
||||
dark_mode INTEGER,
|
||||
can_upload INTEGER)`
|
||||
)
|
||||
|
||||
db.run(`CREATE TABLE IF NOT EXISTS profiles (
|
||||
@@ -35,7 +37,6 @@ module.exports = function () {
|
||||
db.run(`CREATE TABLE IF NOT EXISTS samples (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT UNIQUE,
|
||||
volume INTEGER,
|
||||
user INTEGER,
|
||||
FOREIGN KEY(user) REFERENCES users(id))`
|
||||
)
|
||||
@@ -44,6 +45,7 @@ module.exports = function () {
|
||||
id INTEGER PRIMARY KEY,
|
||||
profile INTEGER,
|
||||
sample INTEGER,
|
||||
volume INTEGER,
|
||||
FOREIGN KEY(profile) REFERENCES profiles(id),
|
||||
FOREIGN KEY(sample) REFERENCES samples(id))`
|
||||
)
|
||||
|
||||
@@ -47,27 +47,26 @@ router.post('/profiles', (req, res) => {
|
||||
],
|
||||
function (err) {
|
||||
if (err) {
|
||||
return res.sendStatus(500)
|
||||
if (err.code === 'SQLITE_CONSTRAINT') {
|
||||
return res.sendStatus(409)
|
||||
} else {
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
}
|
||||
|
||||
profileID = this.lastID
|
||||
|
||||
req.body.samples.forEach(s => {
|
||||
db.run('INSERT INTO profiles_samples (profile, sample) VALUES (?, ?)', [
|
||||
db.run('INSERT INTO profiles_samples (profile, sample, volume) VALUES (?, ?, ?)', [
|
||||
profileID,
|
||||
s.id
|
||||
s.id,
|
||||
s.volume
|
||||
],
|
||||
(err) => {
|
||||
if (err) {
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
})
|
||||
|
||||
db.run('UPDATE samples SET volume = ? WHERE id = ?', [s.volume, s.id], (err) => {
|
||||
if (err) {
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
return res.json({ id: profileID })
|
||||
@@ -75,6 +74,88 @@ router.post('/profiles', (req, res) => {
|
||||
})
|
||||
})
|
||||
|
||||
router.put('/profiles/:profileId', (req, res) => {
|
||||
if (!req.user) {
|
||||
return res.sendStatus(401)
|
||||
}
|
||||
|
||||
db.serialize(() => {
|
||||
db.get('SELECT user FROM profiles WHERE id = ?', [req.params.profileId], (err, row) => {
|
||||
if (err) {
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
|
||||
if (row.user.toString() !== req.user.id) {
|
||||
return res.sendStatus(401)
|
||||
}
|
||||
})
|
||||
|
||||
db.run(`UPDATE profiles SET
|
||||
timer_enabled = ?,
|
||||
duration = ?,
|
||||
volume = ?,
|
||||
noise_color = ?,
|
||||
filter_enabled = ?,
|
||||
filter_type = ?,
|
||||
filter_cutoff = ?,
|
||||
lfo_filter_cutoff_enabled = ?,
|
||||
lfo_filter_cutoff_frequency = ?,
|
||||
lfo_filter_cutoff_low = ?,
|
||||
lfo_filter_cutoff_high = ?,
|
||||
tremolo_enabled = ?,
|
||||
tremolo_frequency = ?,
|
||||
tremolo_depth = ?
|
||||
WHERE id = ?`, [
|
||||
req.body.isTimerEnabled ? 1 : 0,
|
||||
req.body.duration,
|
||||
req.body.volume,
|
||||
req.body.noiseColor,
|
||||
req.body.isFilterEnabled ? 1 : 0,
|
||||
req.body.filterType,
|
||||
req.body.filterCutoff,
|
||||
req.body.isLFOFilterCutoffEnabled ? 1 : 0,
|
||||
req.body.lfoFilterCutoffFrequency,
|
||||
req.body.lfoFilterCutoffLow,
|
||||
req.body.lfoFilterCutoffHigh,
|
||||
req.body.isTremoloEnabled ? 1 : 0,
|
||||
req.body.tremoloFrequency,
|
||||
req.body.tremoloDepth,
|
||||
req.params.profileId
|
||||
],
|
||||
(err) => {
|
||||
if (err) {
|
||||
console.log(err)
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
|
||||
db.serialize(() => {
|
||||
db.run('DELETE FROM profiles_samples WHERE profile = ?', [
|
||||
req.params.profileId
|
||||
],
|
||||
(err) => {
|
||||
if (err) {
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
})
|
||||
|
||||
req.body.samples.forEach(s => {
|
||||
db.run('INSERT INTO profiles_samples (profile, sample, volume) VALUES (?, ?, ?)', [
|
||||
req.params.profileId,
|
||||
s.id,
|
||||
s.volume
|
||||
],
|
||||
(err) => {
|
||||
if (err) {
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
return res.sendStatus(200)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
router.post('/profiles/default', (req, res) => {
|
||||
if (!req.user) {
|
||||
return res.sendStatus(401)
|
||||
@@ -117,13 +198,13 @@ router.get('/profiles', (req, res) => {
|
||||
return res.sendStatus(401)
|
||||
}
|
||||
|
||||
const profiles = []
|
||||
|
||||
db.all('SELECT id, name FROM profiles WHERE user = ?', [req.user.id], (err, rows) => {
|
||||
if (err) {
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
|
||||
const profiles = []
|
||||
|
||||
rows.forEach(row => {
|
||||
const profile = {}
|
||||
|
||||
@@ -142,8 +223,6 @@ router.get('/profiles/:profileId', (req, res) => {
|
||||
return res.sendStatus(401)
|
||||
}
|
||||
|
||||
const profile = {}
|
||||
|
||||
db.serialize(() => {
|
||||
db.get(`SELECT
|
||||
name,
|
||||
@@ -164,6 +243,7 @@ router.get('/profiles/:profileId', (req, res) => {
|
||||
tremolo_depth as tremoloDepth
|
||||
FROM profiles WHERE id = ?`, [req.params.profileId], (err, row) => {
|
||||
if (err) {
|
||||
console.log(err)
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
|
||||
@@ -171,6 +251,8 @@ router.get('/profiles/:profileId', (req, res) => {
|
||||
return res.sendStatus(401)
|
||||
}
|
||||
|
||||
const profile = {}
|
||||
|
||||
profile.name = row.name
|
||||
profile.isTimerEnabled = row.isTimerEnabled === 1
|
||||
profile.duration = row.duration
|
||||
@@ -187,24 +269,34 @@ router.get('/profiles/:profileId', (req, res) => {
|
||||
profile.tremoloFrequency = row.tremoloFrequency
|
||||
profile.tremoloDepth = row.tremoloDepth
|
||||
|
||||
const sampleIds = []
|
||||
|
||||
db.all('SELECT sample FROM profiles_samples WHERE profile = ?', [req.params.profileId], (err, rows) => {
|
||||
if (err) {
|
||||
console.log(err)
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
|
||||
const samples = []
|
||||
const sampleQueryArgs = []
|
||||
|
||||
sampleQueryArgs.push(req.params.profileId)
|
||||
|
||||
rows.forEach(row => {
|
||||
sampleIds.push(row.sample)
|
||||
sampleQueryArgs.push(row.sample)
|
||||
})
|
||||
|
||||
db.all('SELECT id, name, volume FROM samples WHERE id IN ( ' + sampleIds.map(() => { return '?' }).join(',') + ' )', sampleIds, (err, rows) => {
|
||||
db.all(`SELECT samples.id, name, profiles_samples.volume
|
||||
FROM samples
|
||||
INNER JOIN profiles_samples
|
||||
ON profiles_samples.sample = samples.id
|
||||
AND profiles_samples.profile = ?
|
||||
WHERE samples.id IN ( ` +
|
||||
sampleQueryArgs.map(() => { return '?' }).join(',') + ' )', sampleQueryArgs, (err, rows) => {
|
||||
if (err) {
|
||||
console.log(err)
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
|
||||
const samples = []
|
||||
|
||||
rows.forEach(row => {
|
||||
const sample = {}
|
||||
|
||||
|
||||
@@ -3,8 +3,8 @@ const config = require('config')
|
||||
const multer = require('multer')
|
||||
const storage = multer.diskStorage({
|
||||
destination: config.get('Server.sampleUploadPath'),
|
||||
filename: function (req, file, cb) {
|
||||
cb(null, req.body.name)
|
||||
filename: (req, file, cb) => {
|
||||
cb(null, req.user.id + '_' + req.body.name)
|
||||
}
|
||||
})
|
||||
const upload = multer({ storage: storage })
|
||||
@@ -16,17 +16,33 @@ router.post('/samples', upload.single('sample'), (req, res, next) => {
|
||||
return res.sendStatus(401)
|
||||
}
|
||||
|
||||
db.run('INSERT INTO samples (name, volume, user) VALUES (?, ?, ?)', [
|
||||
req.body.name,
|
||||
0,
|
||||
req.user.id
|
||||
],
|
||||
(err) => {
|
||||
if (err) {
|
||||
return res.sendStatus(500)
|
||||
} else {
|
||||
return res.sendStatus(200)
|
||||
}
|
||||
db.serialize(() => {
|
||||
db.get('SELECT can_upload FROM users WHERE id = ?', [req.user.id], (err, row) => {
|
||||
if (err) {
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
|
||||
if (row.can_upload === 0) {
|
||||
return res.sendStatus(401)
|
||||
}
|
||||
})
|
||||
|
||||
db.run('INSERT INTO samples (name, user) VALUES (?, ?)', [
|
||||
req.body.name,
|
||||
req.user.id
|
||||
],
|
||||
(err) => {
|
||||
if (err) {
|
||||
console.log(err)
|
||||
if (err.code === 'SQLITE_CONSTRAINT') {
|
||||
return res.sendStatus(409)
|
||||
} else {
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
} else {
|
||||
return res.sendStatus(200)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -37,8 +53,9 @@ router.get('/samples', (req, res) => {
|
||||
|
||||
const samples = []
|
||||
|
||||
db.all('SELECT id, name, volume FROM samples WHERE user = ?', [req.user.id], (err, rows) => {
|
||||
db.all('SELECT id, name FROM samples WHERE user = ?', [req.user.id], (err, rows) => {
|
||||
if (err) {
|
||||
console.log(err)
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
|
||||
@@ -47,7 +64,7 @@ router.get('/samples', (req, res) => {
|
||||
|
||||
sample.id = row.id
|
||||
sample.name = row.name
|
||||
sample.volume = row.volume
|
||||
sample.user = req.user.id
|
||||
|
||||
samples.push(sample)
|
||||
})
|
||||
|
||||
@@ -8,17 +8,21 @@ router.get('/users/current', (req, res) => {
|
||||
return res.sendStatus(401)
|
||||
}
|
||||
|
||||
db.get('SELECT is_admin as isAdmin, * FROM users WHERE id = ?', [req.user.id], (err, row) => {
|
||||
db.get('SELECT is_admin as isAdmin, dark_mode as darkMode, can_upload as canUpload, * FROM users WHERE id = ?', [req.user.id], (err, row) => {
|
||||
if (err) {
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
|
||||
const user = {}
|
||||
|
||||
user.id = row.id
|
||||
user.username = row.username
|
||||
user.name = row.name
|
||||
user.isAdmin = row.isAdmin === 1
|
||||
if (row) {
|
||||
user.id = row.id
|
||||
user.username = row.username
|
||||
user.name = row.name
|
||||
user.isAdmin = row.isAdmin === 1
|
||||
user.darkMode = row.darkMode === 1
|
||||
user.canUpload = row.canUpload === 1
|
||||
}
|
||||
|
||||
res.json({ user: user })
|
||||
})
|
||||
@@ -31,7 +35,7 @@ router.get('/users', (req, res) => {
|
||||
|
||||
const users = []
|
||||
|
||||
db.all('SELECT id, username, name, is_admin as isAdmin FROM users', (err, rows) => {
|
||||
db.all('SELECT id, username, name, is_admin as isAdmin, can_upload as canUpload FROM users', (err, rows) => {
|
||||
if (err) {
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
@@ -43,6 +47,7 @@ router.get('/users', (req, res) => {
|
||||
user.username = row.username
|
||||
user.name = row.name
|
||||
user.isAdmin = row.isAdmin === 1
|
||||
user.canUpload = row.canUpload === 1
|
||||
|
||||
users.push(user)
|
||||
})
|
||||
@@ -58,12 +63,15 @@ router.post('/users', (req, res) => {
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
|
||||
db.run('INSERT INTO users (username, hashed_password, salt, name, is_admin) VALUES (?, ?, ?, ?, ?)', [
|
||||
db.run(`INSERT INTO users (username, hashed_password, salt, name, is_admin, dark_mode, can_upload)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`, [
|
||||
req.body.username,
|
||||
hashedPassword,
|
||||
salt,
|
||||
req.body.name,
|
||||
req.body.isAdmin
|
||||
req.body.isAdmin,
|
||||
req.body.darkMode,
|
||||
req.body.canUpload
|
||||
], function (err) {
|
||||
if (err) {
|
||||
if (err.code === 'SQLITE_CONSTRAINT') {
|
||||
@@ -89,7 +97,7 @@ router.post('/users', (req, res) => {
|
||||
})
|
||||
})
|
||||
|
||||
router.patch('/users/:userId', (req, res) => {
|
||||
router.patch('/users/admin/:userId', (req, res) => {
|
||||
if (!req.user) {
|
||||
return res.sendStatus(401)
|
||||
}
|
||||
@@ -115,6 +123,48 @@ router.patch('/users/:userId', (req, res) => {
|
||||
})
|
||||
})
|
||||
|
||||
router.patch('/users/upload/:userId', (req, res) => {
|
||||
if (!req.user) {
|
||||
return res.sendStatus(401)
|
||||
}
|
||||
|
||||
db.serialize(() => {
|
||||
db.get('SELECT is_admin FROM users WHERE id = ?', [req.user.id], (err, row) => {
|
||||
if (err) {
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
|
||||
if (row.is_admin === 0) {
|
||||
return res.sendStatus(401)
|
||||
}
|
||||
})
|
||||
|
||||
db.run('UPDATE users SET can_upload = ? WHERE id = ?', [req.body.canUpload ? 1 : 0, req.params.userId], (err) => {
|
||||
if (err) {
|
||||
return res.sendStatus(500)
|
||||
} else {
|
||||
return res.sendStatus(200)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
router.patch('/users/dark-mode', (req, res) => {
|
||||
if (!req.user) {
|
||||
return res.sendStatus(401)
|
||||
}
|
||||
|
||||
db.serialize(() => {
|
||||
db.run('UPDATE users SET dark_mode = ? WHERE id = ?', [req.body.darkMode ? 1 : 0, req.user.id], (err) => {
|
||||
if (err) {
|
||||
return res.sendStatus(500)
|
||||
} else {
|
||||
return res.sendStatus(200)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
router.delete('/users/:userId', (req, res) => {
|
||||
if (!req.user) {
|
||||
return res.sendStatus(401)
|
||||
|
||||
Reference in New Issue
Block a user