From 1d6338fd640f21fd30ad03db2f614ea669bec7ca Mon Sep 17 00:00:00 2001 From: Nikola Petrov Date: Tue, 16 Jul 2024 22:40:15 +0200 Subject: [PATCH] Fix bug --- controllers/mediaController.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/controllers/mediaController.ts b/controllers/mediaController.ts index ca76ff3..56b6087 100644 --- a/controllers/mediaController.ts +++ b/controllers/mediaController.ts @@ -31,7 +31,7 @@ export default { * mediaController.create() */ create: async function (req: Request, res: Response) { - const mediaCode = req.body.code; + const mediaCode: string = req.body.code; const omdb_key = await UserModel.getValue(values.omdb_key); @@ -39,10 +39,14 @@ export default { return res.status(500).json({ message: 'Error when creating media' }); } + // remove the tt in front in DB its stored as number + const sub = mediaCode.substring(2); + const cleanCode = parseInt(sub); + try { const mediaTable = req.baseUrl.includes('movies') ? Table.movies : Table.series; - const mediaFound = await MediaModel.findOne(mediaTable, mediaCode); - if (mediaFound) { + const mediaFound = await MediaModel.findOne(mediaTable, cleanCode); + if (mediaFound.length != 0) { return res.status(409).json({ message: 'Media already exists' }); } @@ -61,7 +65,7 @@ export default { webImg: mData.Poster, }; - const savedMedia = await MediaModel.save(mediaTable, mediaCode, mData.Title, mData.Released, mData.Poster); + const savedMedia = await MediaModel.save(mediaTable, cleanCode, mData.Title, mData.Released, mData.Poster); return res.status(201).json(media); } catch (err) { return res.status(500).json({ message: 'Error when creating media' });