forked from anuragverma108/SwapReads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
138 lines (119 loc) · 4.06 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const RegisterSchema = require("./assets/validation/zodschema");
const validate = require("./assets/validation/validate.schema");
const nodemailer = require("nodemailer");
const app = express();
app.use(bodyParser.json());
const MONGO_URI = "mongodb://localhost:27017/swapread";
const dbConnect = async () => {
try {
await mongoose.connect(MONGO_URI, {
// useNewUrlParser: true,
// useUnifiedTopology: true,
serverSelectionTimeoutMS: 30000, // 30 seconds timeout
});
console.log("DB connected");
} catch (err) {
console.log("DB failed", err);
}
};
dbConnect().then(() => {
const User = mongoose.model("User", { username: String, password: String });
app.post("/signup", validate(RegisterSchema), async (req, res) => {
const { username, password } = req.body;
const userExists = await User.findOne({ username });
if (userExists) {
res.json({ success: false, message: "Username already exists." });
} else {
const newUser = new User({ username, password });
await newUser.save();
res.json({ success: true });
}
});
app.post("/login", validate(RegisterSchema), async (req, res) => {
const { username, password } = req.body;
const user = await User.findOne({ username, password });
if (user) {
res.json({ success: true });
} else {
res.json({ success: false, message: "Invalid username or password." });
}
});
// Book Exchange/Selling
const bookSchema = new mongoose.Schema({
title: String,
author: String,
price: Number,
sellerEmail: String,
});
const Book = mongoose.model("Book", bookSchema);
app.post("/sellBook", async (req, res) => {
const { title, author, price, sellerEmail } = req.body;
const newBook = new Book({ title, author, price, sellerEmail });
newBook.save()
.then((book) => {
sendListingEmailToSeller(sellerEmail, book.title);
res.json({ success: true, message: "Book listing added successfully!" });
})
.catch((err) => {
console.log(err);
res.json({ success: false, message: "Internal Server Error" });
});
});
app.post("/buyBook", async (req, res) => {
const { bookID, buyerEmail } = req.body;
Book.findById(bookID)
.then((book) => {
if (!book) {
return res.json({ success: false, message: "Book Not Found." });
}
sendBuyingEmailToSeller(book.sellerEmail, book.title, book.price, book.author, buyerEmail);
res.json({ success: true, message: "Email Sent to Seller" });
})
.catch((err) => {
console.log(err);
res.json({ success: false, message: "Internal Server Error" });
});
});
// Configure Nodemailer transporter
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "[email protected]",
pass: "password",
},
});
function sendBuyingEmailToSeller(sellerEmail, bookTitle, bookPrice, bookAuthor, buyerEmail) {
const mailOptions = {
from: "[email protected]",
to: sellerEmail,
subject: "Someone is interested in your book!",
text: `Congratulations! Your book "${bookTitle}" by ${bookAuthor} at ${bookPrice} has a Buyer ${buyerEmail}.`,
};
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
console.error("Error sending email:", err);
} else {
console.log("Email sent:", info.response);
}
});
}
function sendListingEmailToSeller(sellerEmail, bookTitle) {
const mailOptions = {
from: "[email protected]",
to: sellerEmail,
subject: "Your book listing is live!",
text: `Congratulations! Your book "${bookTitle}" is now listed for sale.`,
};
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
console.error("Error sending email:", err);
} else {
console.log("Email sent:", info.response);
}
});
}
app.listen(3000, () => console.log("Server is running on port 3000"));
});