forked from Ananya-te/MusicTrackerAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsongController.js
More file actions
35 lines (31 loc) · 1.2 KB
/
songController.js
File metadata and controls
35 lines (31 loc) · 1.2 KB
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
const db = require('../db');
exports.getSongs = (req, res) => {
db.all('SELECT * FROM songs', (err, rows) => {
if (err) return res.status(500).json({ error: err.message });
res.json(rows);
});
};
exports.addSong = (req, res) => {
const { title, artist, genre } = req.body;
if (!title || !artist || !genre) {
return res.status(400).json({ error: 'Missing song fields' });
}
db.run('INSERT INTO songs (title, artist, genre) VALUES (?, ?, ?)', [title, artist, genre], function (err) {
if (err) return res.status(500).json({ error: err.message });
res.status(201).json({ id: this.lastID, title, artist, genre });
});
};
exports.getPlaylists = (req, res) => {
db.all('SELECT * FROM playlists', (err, rows) => {
if (err) return res.status(500).json({ error: err.message });
res.json(rows);
});
};
exports.createPlaylist = (req, res) => {
const { name } = req.body;
if (!name) return res.status(400).json({ error: 'Playlist name is required' });
db.run('INSERT INTO playlists (name) VALUES (?)', [name], function (err) {
if (err) return res.status(500).json({ error: err.message });
res.status(201).json({ id: this.lastID, name });
});
};