create session route

main
Matthew Huntington 1 month ago
parent 80ef86a0d1
commit 3864be8b91

@ -94,4 +94,43 @@ app.get('/show-category/:id', async (req, res) => {
res.json(entries);
});
app.post('/sessions', async (req, res) => {
const { description, seconds, comments, practice_category_id, songs } = req.body;
let query, params;
if (comments) {
query = 'INSERT INTO practice_sessions (description, seconds, comments, practice_category_id) VALUES (?, ?, ?, ?)';
params = [description, seconds, comments, practice_category_id];
} else {
query = 'INSERT INTO practice_sessions (description, seconds, practice_category_id) VALUES (?, ?, ?)';
params = [description, seconds, practice_category_id];
}
const results = await mysqlConnection.awaitQuery(query, params)
const newSessionId = results.insertId;
if (songs && songs.length > 0) {
const songPromises = songs.map(song => {
return new Promise((resolve, reject) => {
const songQuery = 'INSERT INTO songs_practiced (practice_session_id, song_id) VALUES (?, ?)';
mysqlConnection.query(songQuery, [newSessionId, song.id], (err) => {
if (err) reject(err);
else resolve();
});
});
});
Promise.all(songPromises)
.then(() => {
res.json({ status: 'success' });
})
.catch(err => {
res.status(500).json({ error: err.message });
});
} else {
res.json({ status: 'success' });
}
});
app.listen(18080)

Loading…
Cancel
Save