diff options
| author | Andrew Lee <andrew@alee14.me> | 2025-03-29 22:19:55 -0400 |
|---|---|---|
| committer | Andrew Lee <andrew@alee14.me> | 2025-03-29 22:19:55 -0400 |
| commit | 070825d2b779b114a1c345fbca210d324bf34d53 (patch) | |
| tree | 1a62f7676bc46b8bc799e8b4b486fa0ea2879f6f /bot/src/api/routes/quotes.js | |
| parent | db6df8c2a3817a753a9b903feb6311c620a91a65 (diff) | |
| download | AleeBot-070825d2b779b114a1c345fbca210d324bf34d53.tar.gz AleeBot-070825d2b779b114a1c345fbca210d324bf34d53.tar.bz2 AleeBot-070825d2b779b114a1c345fbca210d324bf34d53.zip | |
Quote submit stats + input validation for author/year; API changes; Use toString for some stuff
Diffstat (limited to 'bot/src/api/routes/quotes.js')
| -rw-r--r-- | bot/src/api/routes/quotes.js | 138 |
1 files changed, 78 insertions, 60 deletions
diff --git a/bot/src/api/routes/quotes.js b/bot/src/api/routes/quotes.js index f362d7e..1b89f67 100644 --- a/bot/src/api/routes/quotes.js +++ b/bot/src/api/routes/quotes.js @@ -2,70 +2,88 @@ import { Router } from 'express'; import { pendingQuote, quote as newQuote } from '../../models/quote.js'; import { verifyToken } from './auth.js'; -export const quoteRouter = Router(); +export function quoteRouter(client) { + const router = Router(); -quoteRouter.get('/quotes/pending', verifyToken, async (req, res) => { - try { - const quotes = await pendingQuote.findAll(); - res.json(quotes); - } catch (error) { - console.error('Error fetching quotes:', error); - res.status(500).send({ message: 'Internal Server Error' }); - } -}); - -quoteRouter.post('/quotes/add', verifyToken, async (req, res) => { - const { author, authorImage, quote, year, submitterID } = req.body; - try { - await newQuote.create({ - author: author, - authorImage: authorImage, - quote: quote, - year: year, - submitter: submitterID - }); - res.status(200).send({ message: 'Added a new quote' }); - } catch (error) { - console.error('Something went wrong:', error); - res.status(500).send({ message: 'Internal Server Error' }); - } -}); + router.get('/quotes/pending', verifyToken, async (req, res) => { + try { + const quotes = await pendingQuote.findAll(); + res.json(quotes); + } catch (error) { + console.error('Error fetching quotes:', error); + res.status(500).send({ message: 'Internal Server Error' }); + } + }); -quoteRouter.post('/quotes/approve', verifyToken, async (req, res) => { - const { id } = req.body; - try { - const quote = await pendingQuote.findByPk(id); - if (quote) { + router.post('/quotes/add', verifyToken, async (req, res) => { + const { author, authorImage, quote, year, submitterID } = req.body; + try { await newQuote.create({ - author: quote.author, - authorImage: quote.authorImage, - quote: quote.quote, - year: quote.year, - submitter: quote.submitterID + author: author, + authorImage: authorImage, + quote: quote, + year: year, + submitter: submitterID }); - await pendingQuote.destroy({ where: { id } }); - res.status(200).send({ message: 'Quote approved' }); - } else { - res.status(404).send({ message: 'Quote not found' }); + res.status(200).send({ message: 'Added a new quote' }); + } catch (error) { + console.error('Something went wrong:', error); + res.status(500).send({ message: 'Internal Server Error' }); + } + }); + + router.post('/quotes/approve', verifyToken, async (req, res) => { + const { id } = req.body; + try { + const quote = await pendingQuote.findByPk(id); + if (quote) { + await newQuote.create({ + author: quote.author, + authorImage: quote.authorImage, + quote: quote.quote, + year: quote.year, + submitter: quote.submitterID + }); + + await pendingQuote.destroy({ where: {id} }); + res.status(200).send({ message: 'Quote approved' }); + } else { + res.status(404).send({ message: 'Quote not found '}); + } + } catch (error) { + console.error('Error approving quote:', error); + res.status(500).send({ message: 'Internal Server Error' }); } - } catch (error) { - console.error('Error approving quote:', error); - res.status(500).send({ message: 'Internal Server Error' }); - } -}); + }); + + router.post('/quotes/reject', verifyToken, async (req, res) => { + const { id } = req.body; + try { + const quote = await pendingQuote.findByPk(id); + if (quote) { + await pendingQuote.destroy({ where: {id} }); -quoteRouter.post('/quotes/reject', verifyToken, async (req, res) => { - const { id } = req.body; - try { - const quote = await pendingQuote.findByPk(id); - if (quote) { - await pendingQuote.destroy({ where: { id } }); - res.status(200).send({ message: 'Quote rejected' }); - } else { - res.status(404).send({ message: 'Quote not found' }); + if (!req.body.silent) { + client.users.fetch(quote.submitterID).then((user) => { + if (req.body.reason) { + user.send(`Hello ${user.displayName},\nYour quote was rejected for the following reason:\n\`\`\`\n${req.body.reason}\n\`\`\``); + } else { + user.send(`Hello ${user.displayName},\nYour quote was rejected.`); + } + }).catch((err) => { + console.error('Error sending rejection message:', err); + }); + } + + res.status(200).send({ message: 'Quote rejected', reason: req.body.reason }); + } else { + res.status(404).send({ message: 'Quote not found' }); + } + } catch (error) { + console.error('Error rejecting quote:', error); + res.status(500).send({ message: 'Internal Server Error' }); } - } catch (error) { - console.error('Error rejecting quote:', error); - res.status(500).send({ message: 'Internal Server Error' }); - } -}); + }); + + return router; +} |
