feat(chats): add delete functionality

This commit is contained in:
ItzCrazyKns 2024-07-02 10:51:47 +05:30
parent 6ae825999a
commit c74e16e01c
No known key found for this signature in database
GPG key ID: 8162927C7CCE3065
3 changed files with 145 additions and 2 deletions

View file

@ -40,4 +40,27 @@ router.get('/:id', async (req, res) => {
}
});
router.delete(`/:id`, async (req, res) => {
try {
const chatExists = await db.query.chats.findFirst({
where: eq(chats.id, req.params.id),
});
if (!chatExists) {
return res.status(404).json({ message: 'Chat not found' });
}
await db.delete(chats).where(eq(chats.id, req.params.id)).execute();
await db
.delete(messages)
.where(eq(messages.chatId, req.params.id))
.execute();
return res.status(200).json({ message: 'Chat deleted successfully' });
} catch (err) {
res.status(500).json({ message: 'An error has occurred.' });
logger.error(`Error in deleting chat: ${err.message}`);
}
});
export default router;