populated album on get by id request

parent 9da4cf60
...@@ -32,14 +32,39 @@ const Album = model<IAlbum>( ...@@ -32,14 +32,39 @@ const Album = model<IAlbum>(
); );
router.get('/', async (req: Request, res: Response) => { router.get('/', async (req: Request, res: Response) => {
if (!req.query.artist) {
try {
const albums = await Album.find().populate<{artist: IArtist}>('artist'); const albums = await Album.find().populate<{artist: IArtist}>('artist');
res.send(albums); res.send(albums);
return;
} catch (err: unknown) {
res.send('Could not get albums');
}
}
try {
const artistsAlbums = await Album.find({artist: req.query.artist});
res.send(artistsAlbums);
} catch (err: unknown) {
res.send(`Could not find albums of ${req.query.artist} `);
}
});
router.get('/:id', async (req: Request, res: Response) => {
try {
const album = await Album.findById(req.params.id).populate<{
artist: IArtist;
}>('artist');
res.send(album);
} catch (err: unknown) {
res.send('No such artist exists');
}
}); });
router.post( router.post(
'/', '/',
upload.single('albumImage'), upload.single('albumImage'),
async (req: Request, res: Response) => { async (req: Request, res: Response) => {
try {
const album: HydratedDocument<IAlbum> = new Album({ const album: HydratedDocument<IAlbum> = new Album({
name: req.body.name, name: req.body.name,
albumImage: { albumImage: {
...@@ -54,6 +79,9 @@ router.post( ...@@ -54,6 +79,9 @@ router.post(
await album.save(); await album.save();
res.send('Sucessfully saved'); res.send('Sucessfully saved');
} catch (err: unknown) {
res.send('Could not post album');
}
} }
); );
......
...@@ -31,14 +31,19 @@ const artistSchema = new Schema<IArtist>({ ...@@ -31,14 +31,19 @@ const artistSchema = new Schema<IArtist>({
const Artist = model<IArtist>('Artist', artistSchema); const Artist = model<IArtist>('Artist', artistSchema);
router.get('/', async (req: Request, res: Response) => { router.get('/', async (req: Request, res: Response) => {
try {
const artists = await Artist.find(); const artists = await Artist.find();
res.send(artists); res.send(artists);
} catch (err: unknown) {
res.send('could not get artists');
}
}); });
router.post( router.post(
'/', '/',
upload.single('image'), upload.single('image'),
async (req: Request, res: Response) => { async (req: Request, res: Response) => {
try {
const artist: HydratedDocument<IArtist> = new Artist({ const artist: HydratedDocument<IArtist> = new Artist({
name: req.body.name, name: req.body.name,
description: req.body.description, description: req.body.description,
...@@ -52,6 +57,9 @@ router.post( ...@@ -52,6 +57,9 @@ router.post(
await artist.save(); await artist.save();
res.send('Sucessfully saved'); res.send('Sucessfully saved');
} catch (err: unknown) {
res.send('Could not post');
}
} }
); );
......
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