Fix some server logic

This commit is contained in:
Kevin Thomas
2021-08-18 20:37:21 -07:00
parent 1af31d9573
commit 880864ee5e
4 changed files with 107 additions and 56 deletions

View File

@@ -30,7 +30,7 @@ app.use(session({
resave: true, resave: true,
saveUninitialized: true saveUninitialized: true
})) }))
app.use(function (req, res, next) { app.use((req, res, next) => {
const msgs = req.session.messages || [] const msgs = req.session.messages || []
res.locals.messages = msgs res.locals.messages = msgs
res.locals.hasMessages = !!msgs.length res.locals.hasMessages = !!msgs.length

View File

@@ -51,17 +51,23 @@ router.post('/profiles', (req, res) => {
} }
profileID = this.lastID profileID = this.lastID
})
req.body.samples.forEach(s => { req.body.samples.forEach(s => {
db.run('INSERT INTO profiles_samples (profile, sample) VALUES (?, ?)', [ db.run('INSERT INTO profiles_samples (profile, sample) VALUES (?, ?)', [
profileID, profileID,
s.id s.id
], ],
(err) => { (err) => {
if (err) { if (err) {
return res.sendStatus(500) return res.sendStatus(500)
} }
})
db.run('UPDATE samples SET volume = ? WHERE id = ?', [s.volume, s.id], (err) => {
if (err) {
return res.sendStatus(500)
}
})
}) })
}) })
}) })
@@ -100,49 +106,83 @@ router.get('/profiles/:profileId', (req, res) => {
const profile = {} const profile = {}
db.get(`SELECT db.serialize(() => {
name, db.get(`SELECT
user, name,
timer_enabled as isTimerEnabled, user,
duration, timer_enabled as isTimerEnabled,
volume, duration,
noise_color as noiseColor, volume,
filter_enabled as isFilterEnabled, noise_color as noiseColor,
filter_type as filterType, filter_enabled as isFilterEnabled,
filter_cutoff as filterCutoff, filter_type as filterType,
lfo_filter_cutoff_enabled as isLFOFilterCutoffEnabled, filter_cutoff as filterCutoff,
lfo_filter_cutoff_frequency as lfoFilterCutoffFrequency, lfo_filter_cutoff_enabled as isLFOFilterCutoffEnabled,
lfo_filter_cutoff_low as lfoFilterCutoffLow, lfo_filter_cutoff_frequency as lfoFilterCutoffFrequency,
lfo_filter_cutoff_high as lfoFilterCutoffHigh, lfo_filter_cutoff_low as lfoFilterCutoffLow,
tremolo_enabled as isTremoloEnabled, lfo_filter_cutoff_high as lfoFilterCutoffHigh,
tremolo_frequency as tremoloFrequency, tremolo_enabled as isTremoloEnabled,
tremolo_depth as tremoloDepth tremolo_frequency as tremoloFrequency,
FROM profiles WHERE id = ?`, [req.params.profileId], (err, row) => { tremolo_depth as tremoloDepth
if (err) { FROM profiles WHERE id = ?`, [req.params.profileId], (err, row) => {
return res.sendStatus(500) if (err) {
} return res.sendStatus(500)
}
if (row.user.toString() !== req.user.id) { if (row.user.toString() !== req.user.id) {
return res.sendStatus(401) return res.sendStatus(401)
} }
profile.name = row.name profile.name = row.name
profile.isTimerEnabled = row.isTimerEnabled === 1 profile.isTimerEnabled = row.isTimerEnabled === 1
profile.duration = row.duration profile.duration = row.duration
profile.volume = row.volume profile.volume = row.volume
profile.noiseColor = row.noiseColor profile.noiseColor = row.noiseColor
profile.isFilterEnabled = row.isFilterEnabled === 1 profile.isFilterEnabled = row.isFilterEnabled === 1
profile.filterType = row.filterType profile.filterType = row.filterType
profile.filterCutoff = row.filterCutoff profile.filterCutoff = row.filterCutoff
profile.isLFOFilterCutoffEnabled = row.isLFOFilterCutoffEnabled === 1 profile.isLFOFilterCutoffEnabled = row.isLFOFilterCutoffEnabled === 1
profile.lfoFilterCutoffFrequency = row.lfoFilterCutoffFrequency profile.lfoFilterCutoffFrequency = row.lfoFilterCutoffFrequency
profile.lfoFilterCutoffLow = row.lfoFilterCutoffLow profile.lfoFilterCutoffLow = row.lfoFilterCutoffLow
profile.lfoFilterCutoffHigh = row.lfoFilterCutoffHigh profile.lfoFilterCutoffHigh = row.lfoFilterCutoffHigh
profile.isTremoloEnabled = row.isTremoloEnabled === 1 profile.isTremoloEnabled = row.isTremoloEnabled === 1
profile.tremoloFrequency = row.tremoloFrequency profile.tremoloFrequency = row.tremoloFrequency
profile.tremoloDepth = row.tremoloDepth profile.tremoloDepth = row.tremoloDepth
res.json({ profile: profile }) const sampleIds = []
db.all('SELECT sample FROM profiles_samples WHERE profile = ?', [req.params.profileId], (err, rows) => {
if (err) {
return res.sendStatus(500)
}
const samples = []
rows.forEach(row => {
sampleIds.push(row.sample)
})
db.all('SELECT id, name, volume FROM samples WHERE id IN ( ' + sampleIds.map(() => { return '?' }).join(',') + ' )', sampleIds, (err, rows) => {
if (err) {
return res.sendStatus(500)
}
rows.forEach(row => {
const sample = {}
sample.id = row.id
sample.name = row.name
sample.volume = row.volume
samples.push(sample)
})
profile.samples = samples
res.json({ profile: profile })
})
})
})
}) })
}) })
@@ -163,6 +203,12 @@ router.delete('/profiles/:profileId', (req, res) => {
}) })
db.run('DELETE FROM profiles WHERE id = ?', [req.params.profileId], (err) => { db.run('DELETE FROM profiles WHERE id = ?', [req.params.profileId], (err) => {
if (err) {
return res.sendStatus(500)
}
})
db.run('DELETE FROM profiles_samples WHERE profile = ?', [req.params.profileId], (err) => {
if (err) { if (err) {
return res.sendStatus(500) return res.sendStatus(500)
} else { } else {

View File

@@ -323,18 +323,16 @@
class="mx-3 mb-5" class="mx-3 mb-5"
v-on="on" v-on="on"
> >
Upload Sample Add Sample
</v-btn> </v-btn>
</template> </template>
<v-card> <v-card>
<v-card-title> <v-card-title>
<span class="text-h5">Add a Sample</span> <span class="text-h5">Add Sample</span>
</v-card-title> </v-card-title>
<v-card-text> <v-card-text>
<v-container> <v-container>
<v-row> <v-divider />
<p><strong>WARNING:</strong> Uploaded samples are publicly accessible.</p>
</v-row>
<v-row> <v-row>
<v-file-input <v-file-input
v-model="selectedSample" v-model="selectedSample"
@@ -342,6 +340,11 @@
label="Upload a sample!" label="Upload a sample!"
/> />
</v-row> </v-row>
<v-row>
<v-col cols="12">
<p><strong>WARNING:</strong> Uploaded samples are publicly accessible.</p>
</v-col>
</v-row>
<v-row> <v-row>
<v-col cols="12"> <v-col cols="12">
<v-text-field <v-text-field

View File

@@ -219,6 +219,8 @@ export default {
this.isTremoloEnabled = profile.isTremoloEnabled this.isTremoloEnabled = profile.isTremoloEnabled
this.tremoloFrequency = profile.tremoloFrequency this.tremoloFrequency = profile.tremoloFrequency
this.tremoloDepth = profile.tremoloDepth this.tremoloDepth = profile.tremoloDepth
this.samples = profile.samples
} }
}) })
.catch((error) => { .catch((error) => {