aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
authorAndrew Lee <andrew@alee14.me>2025-01-11 11:55:18 -0500
committerAndrew Lee <andrew@alee14.me>2025-01-11 11:55:18 -0500
commitf5de90ba89146008af78c16e798e216efccf0c50 (patch)
tree75f2bb7cee8bacce3f92c2b1bc468c7831b3429e /api
parent83dcca0a0279ce6415a3e9d153c13d91284369b0 (diff)
downloadAleeBot-f5de90ba89146008af78c16e798e216efccf0c50.tar.gz
AleeBot-f5de90ba89146008af78c16e798e216efccf0c50.tar.bz2
AleeBot-f5de90ba89146008af78c16e798e216efccf0c50.zip
Ability to add quotes, web interface, pending quotes
Diffstat (limited to 'api')
-rw-r--r--api/server.js78
1 files changed, 78 insertions, 0 deletions
diff --git a/api/server.js b/api/server.js
new file mode 100644
index 0000000..a918308
--- /dev/null
+++ b/api/server.js
@@ -0,0 +1,78 @@
+const express = require('express');
+const cors = require('cors');
+const { pendingQuote, quote: approvedQuote } = require('../models/quote.js');
+
+const app = express();
+const PORT = 3000;
+
+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.get('/api/version', (req, res) => {
+ const { abVersion } = require('../storage/settings.json');
+ res.json(abVersion);
+
+ });
+
+ 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}`);
+ });
+};
+
+module.exports = createServer;