aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
Diffstat (limited to 'api')
-rw-r--r--api/routes/quotes.js58
-rw-r--r--api/server.js56
2 files changed, 60 insertions, 54 deletions
diff --git a/api/routes/quotes.js b/api/routes/quotes.js
new file mode 100644
index 0000000..39aba7b
--- /dev/null
+++ b/api/routes/quotes.js
@@ -0,0 +1,58 @@
+const express = require('express');
+const quoteDB = require('../../models/quote.js');
+
+const router = express.Router();
+
+const pendingQuote = quoteDB.pendingQuote;
+const approvedQuote = quoteDB.quote;
+
+router.get('/pending-quotes', 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');
+ }
+});
+
+router.post('/approve-quote', async (req, res) => {
+ const { id } = req.body;
+ try {
+ const quote = await pendingQuote.findByPk(id);
+ if (quote) {
+ await approvedQuote.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');
+ }
+});
+
+router.post('/reject-quote', 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');
+ }
+});
+
+module.exports = router;
diff --git a/api/server.js b/api/server.js
index a918308..a6cb48c 100644
--- a/api/server.js
+++ b/api/server.js
@@ -1,6 +1,6 @@
const express = require('express');
const cors = require('cors');
-const { pendingQuote, quote: approvedQuote } = require('../models/quote.js');
+const quotesRouter = require('./routes/quotes');
const app = express();
const PORT = 3000;
@@ -9,54 +9,7 @@ const createServer = () => {
app.use(cors()); // Allow cross-origin requests
app.use(express.json());
- // Endpoint to get all pending quotes
- app.get('/api/pending-quotes', 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');
- }
- });
-
- app.post('/api/approve-quote', async (req, res) => {
- const { id } = req.body;
- try {
- const quote = await pendingQuote.findByPk(id);
- if (quote) {
- await approvedQuote.create({
- author: quote.author,
- quote: quote.quote,
- year: quote.year,
- authorImage: quote.authorImage
- });
- 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');
- }
- });
-
- app.post('/api/reject-quote', 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');
- }
- });
+ app.use('/api', quotesRouter);
app.get('/api/version', (req, res) => {
const { abVersion } = require('../storage/settings.json');
@@ -64,11 +17,6 @@ const createServer = () => {
});
- app.get('/' , (req, res) => {
- res.send('API for AleeBot');
- // Most likely going to redirect to the frontend
- });
-
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);