aboutsummaryrefslogtreecommitdiff
path: root/api/server.js
blob: a9183088bebc8d1920442ec8acca56fd12222f7e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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;