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' });