-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
132 lines (111 loc) · 3.29 KB
/
app.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
const express = require("express")
const mongoose = require("mongoose")
const port = 4000;
const app = express()
// middleware for json response
app.use(express.json())
// database configuration
const db = async () => {
try {
await mongoose.connect("mongodb://localhost:27017/lap4dev");
console.log("database coneected...");
} catch (error) {
console.log(error);
}
}
db()
// applicants schema model
const Applicants = new mongoose.Schema({
firstname : {
type: String,
required: true
},
lastname : {
type: String,
required: true
},
email: {
type: String,
required: true
},
phoneNo : {
type: Number,
required: true
},
purpose: {
type: String,
required: true
}
})
const participant = mongoose.model('Applicants', Applicants)
// route for viewing all the participants who showed interest for the program
app.get("/", async (req, res, next) => {
const part = await participant.find();
if (!part){
return res.status(200).json({ message: "No participants found"});
}
return res.status(200).json({message: part});
})
// route for registering interest for the program
app.post("/register", async (req, res, next) => {
const { firstname, lastname, email, phoneNo, purpose } = req.body;
if(!firstname || !lastname || !email || !phoneNo || !purpose){
return res.status(400).json({
message: "firstname, lastname, email, phoneNo and purpose fields are required!",
Fields: {
"firstname": "",
"lastname": "",
"email": "",
"phoneNo": "",
"purpose": ""}
}
)
}
const participantsExist = await participant.findOne({email: email});
if(participantsExist){
return res.status(400).json({message: "participants already exist,", data: participantsExist})
}
const newParticipants = new participant({
firstname : firstname,
lastname : lastname,
email : email,
phoneNo : phoneNo,
purpose : purpose
})
const participantCreated = await newParticipants.save();
if(!participantCreated){
console.log("could not save participant")
return res.status(400).json({message: "Registration failed"});
}
return res.status(201).json({
message: "Registration successful",
data: participantCreated
});
})
// route for inspecting number of participants that showed interest
app.get("/participants/count", async (req, res, next) => {
const participants = await participant.find().estimatedDocumentCount()
if(!participants){
return res.status(500).json({message: "Internal Server Error"});
}
return res.status(200).send({count: participants});
})
// Error handler for internal server error
app.use((req, res, next) => {
const error = new Error('Resource Not Found');
error.status = 404;
next(error);
});
app.use((error, req, res, next) => {
res.status(error.status || 500);
res.json({
error: {
message: error.message
}
});
});
// the listener configuration
app.listen(port, (err)=>{
if(err) console.log(err);
console.log(`Server is running on port ${port}`);
})