-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Song api #1362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v5
Are you sure you want to change the base?
Song api #1362
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,12 @@ | ||||||
|
||||||
|
||||||
import mongoose from 'mongoose'; | ||||||
|
||||||
const SongSchema = new mongoose.Schema({ | ||||||
title: { type: String, required: true }, | ||||||
artist: { type: String }, | ||||||
url: { type: String, required: true }, | ||||||
addedAt: { type: Date, default: Date.now } | ||||||
}); | ||||||
|
||||||
export default mongoose.model('Song', SongSchema); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix export syntax for CommonJS compatibility This export uses ES Module syntax, but needs to use CommonJS syntax to be compatible with the routes file. -export default mongoose.model('Song', SongSchema);
+module.exports = mongoose.model('Song', SongSchema); 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,37 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const express = require('express'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const Song = require('../models/Song.js'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+1
to
+2
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix import path and module compatibility There are two critical issues with the imports:
-const Song = require('../models/Song.js');
+const Song = require('../models/Songs.js'); Alternatively, make both files use the same module system (either both CommonJS or both ES Modules). 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const router = express.Router(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
router.post('/add', async (req, res) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const { title, artist, url } = req.body; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const song = new Song({ title, artist, url }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
await song.save(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res.status(201).json({ success: true, song }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res.status(500).json({ success: false, error: err.message }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+6
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add input validation for the add endpoint The current implementation relies only on Mongoose schema validation. Consider adding explicit validation for the request body. router.post('/add', async (req, res) => {
try {
const { title, artist, url } = req.body;
+ // Validate required fields
+ if (!title || !url) {
+ return res.status(400).json({ success: false, error: 'Title and URL are required' });
+ }
+ // Validate URL format
+ if (!url.match(/^(http|https):\/\/[^ "]+$/)) {
+ return res.status(400).json({ success: false, error: 'Invalid URL format' });
+ }
const song = new Song({ title, artist, url });
await song.save();
res.status(201).json({ success: true, song });
} catch (err) {
res.status(500).json({ success: false, error: err.message });
}
}); 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//post delete a song | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
router.post('/delete', async (req, res) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const { id } = req.body; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Validate MongoDB ObjectId Without validation, invalid ID formats will cause Mongoose to throw an error. Add validation to provide better error messages. try {
const { id } = req.body;
+ // Validate MongoDB ObjectId format
+ if (!id.match(/^[0-9a-fA-F]{24}$/)) {
+ return res.status(400).json({ success: false, error: 'Invalid song ID format' });
+ }
const deleted = await Song.findByIdAndDelete(id); 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const deleted = await Song.findByIdAndDelete(id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!deleted) return res.status(404).json({ success: false, message: 'Song not found' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res.json({ success: true, message: 'Song deleted' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res.status(500).json({ success: false, error: err.message }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+16
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Use RESTful conventions for delete operation Using POST for delete operations doesn't follow RESTful conventions. Consider using DELETE HTTP method and route parameters instead of a request body. -//post delete a song
-router.post('/delete', async (req, res) => {
+// Delete a song by ID
+router.delete('/:id', async (req, res) => {
try {
- const { id } = req.body;
+ const { id } = req.params;
const deleted = await Song.findByIdAndDelete(id);
if (!deleted) return res.status(404).json({ success: false, message: 'Song not found' });
res.json({ success: true, message: 'Song deleted' });
} catch (err) {
res.status(500).json({ success: false, error: err.message });
}
}); 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
router.get('/queue', async (req, res) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const queue = await Song.find().sort({ addedAt: 1 }); // oldest to newest | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res.json({ success: true, queue }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res.status(500).json({ success: false, error: err.message }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
module.exports = router; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix import/export inconsistency
This file uses ES Module syntax (
import
/export
), but the routes file uses CommonJS (require
/module.exports
). This will cause runtime errors.🤖 Prompt for AI Agents