-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
110 lines (90 loc) · 3.58 KB
/
server.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
const PORT = 3000;
app.use(cors());
app.use(bodyParser.json());
// Connect to the "petitions" MongoDB
mongoose
.connect("mongodb://localhost:27017/petitions", { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("Connected to petitions MongoDB"))
.catch((err) => console.error("Could not connect to petitions MongoDB:", err));
// Connect to the "donations" MongoDB using createConnection
const donationDb = mongoose.createConnection("mongodb://localhost:27017/donations", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
donationDb.on('connected', () => {
console.log("Connected to donations MongoDB");
});
donationDb.on('error', (err) => {
console.error("Could not connect to donations MongoDB:", err);
});
// Define the Petition schema and model
const petitionSchema = new mongoose.Schema({
email: { type: String, required: true, unique: true },
firstName: { type: String, required: true },
lastName: { type: String, required: true },
country: { type: String, required: true },
postalCode: { type: String, required: true },
});
const Petition = mongoose.model("Petition", petitionSchema);
// Define the Donation schema and model for donations database
const donationSchema = new mongoose.Schema({
amount: { type: Number, required: true },
});
const Donation = donationDb.model("Donation", donationSchema);
// Middleware
app.use(bodyParser.json());
app.use(cors());
// API endpoint to handle petition signups
app.post("/api/add-petition", async (req, res) => {
try {
const { email, firstName, lastName, country, postalCode } = req.body;
// Check if the user is already registered
const existingUser = await Petition.findOne({ email });
if (existingUser) {
return res.status(200).json({ message: "User is already registered!" });
}
// Add a new user
const newPetition = new Petition({ email, firstName, lastName, country, postalCode });
await newPetition.save();
res.status(201).json({ message: "User registered successfully!" });
} catch (err) {
console.error(err);
res.status(500).json({ message: "Server error" });
}
});
// API endpoint to handle donations
app.post("/donate", async (req, res) => {
try {
const { amount } = req.body;
// Validate the donation amount
if (!amount || amount <= 0) {
return res.status(400).json({ message: "Invalid donation amount." });
}
// Create a new donation record
const newDonation = new Donation({ amount });
await newDonation.save();
res.status(201).json({ message: "Donation received!" });
} catch (err) {
console.error("Error processing donation:", err);
res.status(500).json({ message: "Error processing donation." });
}
});
// API endpoint to get the total amount donated
app.get("/total-donated", async (req, res) => {
try {
const totalDonations = await Donation.aggregate([
{ $group: { _id: null, total: { $sum: "$amount" } } }
]);
res.status(200).json({ total: totalDonations[0] ? totalDonations[0].total : 0 });
} catch (err) {
console.error("Error fetching total donations:", err);
res.status(500).json({ message: "Error fetching total donations." });
}
});
// Start server
app.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`));