diff options
Diffstat (limited to 'bot/src/api')
| -rw-r--r-- | bot/src/api/routes/quotes.js | 70 | ||||
| -rw-r--r-- | bot/src/api/server.js | 4 |
2 files changed, 74 insertions, 0 deletions
diff --git a/bot/src/api/routes/quotes.js b/bot/src/api/routes/quotes.js new file mode 100644 index 0000000..d39bb28 --- /dev/null +++ b/bot/src/api/routes/quotes.js @@ -0,0 +1,70 @@ +import { Router } from 'express'; +import { pendingQuote, quote as newQuote } from '../../models/quote.js'; + +export const quoteRouter = Router(); + +quoteRouter.get('/quotes/pending', async (req, res) => { + try { + const quotes = await pendingQuote.findAll(); + res.json(quotes); + } catch (error) { + console.error('Error fetching quotes:', error); + res.status(500).send('Internal Server Error'); + } +}); + +quoteRouter.post('/quotes/add', 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('Added a new quote'); + } catch (error) { + console.error('Something went wrong:', error); + res.status(500).send('Internal Server Error'); + } +}); + +quoteRouter.post('/quotes/approve', 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('Quote approved'); + } else { + res.status(404).send('Quote not found'); + } + } catch (error) { + console.error('Error approving quote:', error); + res.status(500).send('Internal Server Error'); + } +}); + +quoteRouter.post('/quotes/reject', 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('Quote rejected'); + } else { + res.status(404).send('Quote not found'); + } + } catch (error) { + console.error('Error rejecting quote:', error); + res.status(500).send('Internal Server Error'); + } +}); diff --git a/bot/src/api/server.js b/bot/src/api/server.js index ac4f8ca..0b0397e 100644 --- a/bot/src/api/server.js +++ b/bot/src/api/server.js @@ -4,12 +4,16 @@ import cors from 'cors'; import 'dotenv/config'; import { readFileSync } from 'node:fs'; +import { quoteRouter } from './routes/quotes.js'; + const app = express(); export const apiServer = (client) => { app.use(cors()); // Allow cross-origin requests app.use(express.json()); + app.use('/api', quoteRouter); + app.get('/api/version', (req, res) => { const { version } = JSON.parse(readFileSync('./package.json', 'utf-8')); res.json({ |
