validated inputs in all routes

parent a6a8164e
...@@ -54,6 +54,9 @@ router.get('/:id', async (req: Request, res: Response) => { ...@@ -54,6 +54,9 @@ router.get('/:id', async (req: Request, res: Response) => {
const album = await Album.findById(req.params.id).populate<{ const album = await Album.findById(req.params.id).populate<{
artist: IArtist; artist: IArtist;
}>('artist'); }>('artist');
if (album === null) {
res.send('Nothing such album found');
}
res.send(album); res.send(album);
} catch (err: unknown) { } catch (err: unknown) {
res.send('No such artist exists'); res.send('No such artist exists');
...@@ -64,6 +67,10 @@ router.post( ...@@ -64,6 +67,10 @@ router.post(
'/', '/',
upload.single('albumImage'), upload.single('albumImage'),
async (req: Request, res: Response) => { async (req: Request, res: Response) => {
if (!req.body.name || !req.file || !req.body.year || !req.body.artist) {
res.send('Name, year, artist or albumimage is required');
return;
}
try { try {
const album: HydratedDocument<IAlbum> = new Album({ const album: HydratedDocument<IAlbum> = new Album({
name: req.body.name, name: req.body.name,
......
...@@ -43,6 +43,10 @@ router.post( ...@@ -43,6 +43,10 @@ router.post(
'/', '/',
upload.single('image'), upload.single('image'),
async (req: Request, res: Response) => { async (req: Request, res: Response) => {
if (!req.body.name || !req.body.description || !req.file) {
res.send('Name, description or image cannot be emtpty');
return;
}
try { try {
const artist: HydratedDocument<IArtist> = new Artist({ const artist: HydratedDocument<IArtist> = new Artist({
name: req.body.name, name: req.body.name,
......
...@@ -41,6 +41,10 @@ router.get('/', async (req: Request, res: Response) => { ...@@ -41,6 +41,10 @@ router.get('/', async (req: Request, res: Response) => {
}); });
router.post('/', async (req: Request, res: Response) => { router.post('/', async (req: Request, res: Response) => {
if (!req.body.name || !req.body.album || !req.body.duration) {
res.send('Name, album or duration cannot be emtpty');
return;
}
try { try {
const track: HydratedDocument<ITrack> = new Track({ const track: HydratedDocument<ITrack> = new Track({
name: req.body.name, name: req.body.name,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment