File size: 907 Bytes
62c3fe0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const { runQuery, runSelectQuery } = require('./queries');

async function addSubscriber(email) {
    try {
        // Check if email exists
        const results = await runSelectQuery('SELECT * FROM subscribers WHERE email = ?', [email]);
        if (results.length > 0) {
            console.log("Email already exists in subscribers");
            return { status: 400, error: 'You have already subscribed!!' };
        }

        // Insert new subscriber
        await runQuery(
            'INSERT INTO subscribers (date, time, email) VALUES (DATE("now"), TIME("now"), ?)',
            [email]
        );
        console.log('Subscriber added successfully');
        return { status: 200, message: 'Subscriber added' };
    } catch (error) {
        console.error('Database error:', error);
        return { status: 500, error: 'Failed to add subscriber' };
    }
}

module.exports = { addSubscriber };