forked from anuragverma108/SwapReads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
93 lines (80 loc) · 2.78 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
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" });
});
});
});