espresss 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ETe/EJS/Student-dashboard.ejs +37 -0
- package/ETe/EJS/Student-details.ejs +34 -0
- package/ETe/EJS/dynamic_user.ejs +7 -0
- package/ETe/EJS/error-handling.ejs +19 -0
- package/ETe/EJS/include-header.ejs +29 -0
- package/ETe/EJS/pagination-system.ejs +77 -0
- package/ETe/Express Session/admin panel.js +68 -0
- package/ETe/Express Session/basic authentication.js +83 -0
- package/ETe/Express Session/session expiration.js +42 -0
- package/ETe/Express Session/session persistance.js +83 -0
- package/ETe/JWT/monitoring panel.js +87 -0
- package/ETe/JWT/protected profile.js +66 -0
- package/ETe/JWT/token based.js +68 -0
- package/ETe/JWT/token expiration.js +66 -0
- package/ETe/Multer/ImageUpload.js +55 -0
- package/ETe/Multer/file management api.js +266 -0
- package/ETe/Multer/file size and validation.js +62 -0
- package/ETe/Multer/from data.js +70 -0
- package/ETe/Multer/multiple.js +57 -0
- package/ete 2/mongo db/company-systsem.js +127 -0
- package/ete 2/mongo db/student-management.js +65 -0
- package/ete 2/mongoose/e-commerce.js +217 -0
- package/ete 2/mongoose/employee.js +99 -0
- package/ete 2/mongoose/event-management.js +87 -0
- package/package.json +13 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
//MongoDb - Company Management System using MongoDB
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
const express = require("express");
|
|
6
|
+
const { MongoClient } = require("mongodb");
|
|
7
|
+
|
|
8
|
+
const app = express();
|
|
9
|
+
const url = "mongodb://127.0.0.1:27017";
|
|
10
|
+
const dbName = "CompanyDB";
|
|
11
|
+
|
|
12
|
+
let db, emp, dept;
|
|
13
|
+
|
|
14
|
+
MongoClient.connect(url).then((client) => {
|
|
15
|
+
db = client.db(dbName);
|
|
16
|
+
emp = db.collection("employees");
|
|
17
|
+
dept = db.collection("departments");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
/* Employee Routes */
|
|
21
|
+
|
|
22
|
+
// seed employees
|
|
23
|
+
app.get("/seed-employees", async (req, res) => {
|
|
24
|
+
await emp.insertMany([
|
|
25
|
+
{ empid: 1, name: "Amit", salary: 20000, deptid: 1 },
|
|
26
|
+
{ empid: 2, name: "Ravi", salary: 25000, deptid: 1 },
|
|
27
|
+
{ empid: 3, name: "Neha", salary: 30000, deptid: 2 },
|
|
28
|
+
{ empid: 4, name: "Pooja", salary: 28000, deptid: 2 },
|
|
29
|
+
{ empid: 5, name: "Rahul", salary: 35000, deptid: 3 },
|
|
30
|
+
{ empid: 6, name: "Kiran", salary: 22000, deptid: 3 },
|
|
31
|
+
]);
|
|
32
|
+
res.send("Employees Seeded");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// view employees
|
|
36
|
+
app.get("/view-employees", async (req, res) => {
|
|
37
|
+
res.json(await emp.find().toArray());
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// add employee
|
|
41
|
+
app.get("/add-employee/:id/:name/:salary/:deptid", async (req, res) => {
|
|
42
|
+
await emp.insertOne({
|
|
43
|
+
empid: +req.params.id,
|
|
44
|
+
name: req.params.name,
|
|
45
|
+
salary: +req.params.salary,
|
|
46
|
+
deptid: +req.params.deptid,
|
|
47
|
+
});
|
|
48
|
+
res.send("Employee Added");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// delete employee
|
|
52
|
+
app.get("/delete-employee/:id", async (req, res) => {
|
|
53
|
+
await emp.deleteOne({ empid: +req.params.id });
|
|
54
|
+
res.send("Deleted");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// delete empid < id
|
|
58
|
+
app.get("/delete-employee-lt/:id", async (req, res) => {
|
|
59
|
+
await emp.deleteMany({ empid: { $lt: +req.params.id } });
|
|
60
|
+
res.send("Deleted");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// update salary
|
|
64
|
+
app.get("/update-salary/:id/:amount", async (req, res) => {
|
|
65
|
+
await emp.updateOne(
|
|
66
|
+
{ empid: +req.params.id },
|
|
67
|
+
{ $set: { salary: +req.params.amount } }
|
|
68
|
+
);
|
|
69
|
+
res.send("Updated");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// update salary range
|
|
73
|
+
app.get("/update-salary-range/:id/:amount", async (req, res) => {
|
|
74
|
+
await emp.updateMany(
|
|
75
|
+
{ empid: { $gte: +req.params.id } },
|
|
76
|
+
{ $inc: { salary: +req.params.amount } }
|
|
77
|
+
);
|
|
78
|
+
res.send("Updated");
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// search employee
|
|
82
|
+
app.get("/search-employee/:name", async (req, res) => {
|
|
83
|
+
res.json(await emp.find({ name: req.params.name }).toArray());
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
/* Department Routes */
|
|
87
|
+
|
|
88
|
+
// seed departments
|
|
89
|
+
app.get("/seed-departments", async (req, res) => {
|
|
90
|
+
await dept.insertMany([
|
|
91
|
+
{ deptid: 1, deptname: "HR" },
|
|
92
|
+
{ deptid: 2, deptname: "IT" },
|
|
93
|
+
{ deptid: 3, deptname: "Sales" },
|
|
94
|
+
]);
|
|
95
|
+
res.send("Departments Seeded");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// view departments
|
|
99
|
+
app.get("/view-departments", async (req, res) => {
|
|
100
|
+
res.json(await dept.find().toArray());
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// add department
|
|
104
|
+
app.get("/add-department/:id/:name", async (req, res) => {
|
|
105
|
+
await dept.insertOne({
|
|
106
|
+
deptid: +req.params.id,
|
|
107
|
+
deptname: req.params.name,
|
|
108
|
+
});
|
|
109
|
+
res.send("Department Added");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// delete department
|
|
113
|
+
app.get("/delete-department/:id", async (req, res) => {
|
|
114
|
+
await dept.deleteOne({ deptid: +req.params.id });
|
|
115
|
+
res.send("Deleted");
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// update department
|
|
119
|
+
app.get("/update-department/:id/:newname", async (req, res) => {
|
|
120
|
+
await dept.updateOne(
|
|
121
|
+
{ deptid: +req.params.id },
|
|
122
|
+
{ $set: { deptname: req.params.newname } }
|
|
123
|
+
);
|
|
124
|
+
res.send("Updated");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
module.exports = app;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
//MongoDb & Ejs - Student Management System with Pagination
|
|
2
|
+
// Code Name: Student Management System with Pagination
|
|
3
|
+
|
|
4
|
+
const express = require("express");
|
|
5
|
+
const { MongoClient, ObjectId } = require("mongodb");
|
|
6
|
+
const app = express();
|
|
7
|
+
|
|
8
|
+
app.use(express.urlencoded({ extended: true }));
|
|
9
|
+
app.use(express.json());
|
|
10
|
+
app.set("view engine", "ejs");
|
|
11
|
+
|
|
12
|
+
let db, students;
|
|
13
|
+
MongoClient.connect("mongodb://127.0.0.1:27017").then((c) => {
|
|
14
|
+
db = c.db("testingproject");
|
|
15
|
+
students = db.collection("students");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
/* Dashboard + Pagination */
|
|
19
|
+
app.get("/", async (req, res) => {
|
|
20
|
+
const page = +req.query.page || 1;
|
|
21
|
+
const limit = 5;
|
|
22
|
+
const data = await students.find().skip((page - 1) * limit).limit(limit).toArray();
|
|
23
|
+
res.render("dashboard", { students: data, page });
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
/* Add Student */
|
|
27
|
+
app.post("/addStudent", async (req, res) => {
|
|
28
|
+
await students.insertOne(req.body);
|
|
29
|
+
res.redirect("/");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
/* Filter */
|
|
33
|
+
app.post("/filter", async (req, res) => {
|
|
34
|
+
const { type, marks } = req.body;
|
|
35
|
+
const q =
|
|
36
|
+
type === ">"
|
|
37
|
+
? { marks: { $gt: marks } }
|
|
38
|
+
: type === "<"
|
|
39
|
+
? { marks: { $lt: marks } }
|
|
40
|
+
: { marks };
|
|
41
|
+
const data = await students.find(q).toArray();
|
|
42
|
+
res.render("dashboard", { students: data, page: 1 });
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
/* Get student */
|
|
46
|
+
app.get("/updateStudent/:id", async (req, res) => {
|
|
47
|
+
res.json(await students.findOne({ _id: new ObjectId(req.params.id) }));
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
/* Update */
|
|
51
|
+
app.patch("/updateStudent/:id", async (req, res) => {
|
|
52
|
+
await students.updateOne(
|
|
53
|
+
{ _id: new ObjectId(req.params.id) },
|
|
54
|
+
{ $set: req.body }
|
|
55
|
+
);
|
|
56
|
+
res.send("Updated");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
/* Delete */
|
|
60
|
+
app.delete("/deleteStudent/:id", async (req, res) => {
|
|
61
|
+
await students.deleteOne({ _id: new ObjectId(req.params.id) });
|
|
62
|
+
res.send("Deleted");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
app.listen(3000);
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
//Aggregate - E-commerce Order Analytics
|
|
2
|
+
const express = require("express");
|
|
3
|
+
const mongoose = require("mongoose");
|
|
4
|
+
|
|
5
|
+
const app = express();
|
|
6
|
+
|
|
7
|
+
const orderSchema = new mongoose.Schema({
|
|
8
|
+
orderId: String,
|
|
9
|
+
userId: String,
|
|
10
|
+
city: String,
|
|
11
|
+
category: String,
|
|
12
|
+
product: String,
|
|
13
|
+
quantity: Number,
|
|
14
|
+
price: Number,
|
|
15
|
+
discount: Number,
|
|
16
|
+
status: String,
|
|
17
|
+
orderDate: Date,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const Order = mongoose.model("Order", orderSchema);
|
|
21
|
+
|
|
22
|
+
// Common revenue field
|
|
23
|
+
const revenueStage = {
|
|
24
|
+
$addFields: {
|
|
25
|
+
totalAmount: {
|
|
26
|
+
$subtract: [
|
|
27
|
+
{ $multiply: ["$price", "$quantity"] },
|
|
28
|
+
{
|
|
29
|
+
$multiply: [
|
|
30
|
+
{ $multiply: ["$price", "$quantity"] },
|
|
31
|
+
{ $divide: ["$discount", 100] },
|
|
32
|
+
],
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/* Task 1: Top 3 Cities by Revenue */
|
|
40
|
+
app.get("/task1", async (req, res) => {
|
|
41
|
+
res.json(
|
|
42
|
+
await Order.aggregate([
|
|
43
|
+
{ $match: { status: { $ne: "cancelled" } } },
|
|
44
|
+
revenueStage,
|
|
45
|
+
{
|
|
46
|
+
$group: {
|
|
47
|
+
_id: "$city",
|
|
48
|
+
totalRevenue: { $sum: "$totalAmount" },
|
|
49
|
+
avgOrderValue: { $avg: "$totalAmount" },
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
{ $sort: { totalRevenue: -1 } },
|
|
53
|
+
{ $limit: 3 },
|
|
54
|
+
{ $project: { _id: 0, city: "$_id", totalRevenue: 1, avgOrderValue: 1 } },
|
|
55
|
+
])
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
/* Task 2: Category Performance */
|
|
60
|
+
app.get("/task2", async (req, res) => {
|
|
61
|
+
res.json(
|
|
62
|
+
await Order.aggregate([
|
|
63
|
+
{ $match: { status: "delivered" } },
|
|
64
|
+
revenueStage,
|
|
65
|
+
{
|
|
66
|
+
$group: {
|
|
67
|
+
_id: "$category",
|
|
68
|
+
totalOrders: { $sum: 1 },
|
|
69
|
+
totalQuantity: { $sum: "$quantity" },
|
|
70
|
+
avgOrderValue: { $avg: "$totalAmount" },
|
|
71
|
+
maxOrderValue: { $max: "$totalAmount" },
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
])
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
/* Task 3: User Spending Analysis */
|
|
79
|
+
app.get("/task3", async (req, res) => {
|
|
80
|
+
res.json(
|
|
81
|
+
await Order.aggregate([
|
|
82
|
+
revenueStage,
|
|
83
|
+
{
|
|
84
|
+
$group: {
|
|
85
|
+
_id: "$userId",
|
|
86
|
+
totalSpent: { $sum: "$totalAmount" },
|
|
87
|
+
avgOrderValue: { $avg: "$totalAmount" },
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
{ $match: { totalSpent: { $gt: 20000 } } },
|
|
91
|
+
{ $sort: { totalSpent: -1 } },
|
|
92
|
+
{
|
|
93
|
+
$project: {
|
|
94
|
+
_id: 0,
|
|
95
|
+
userId: "$_id",
|
|
96
|
+
totalSpent: 1,
|
|
97
|
+
averageOrderValue: "$avgOrderValue",
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
])
|
|
101
|
+
);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
/* Task 4: Monthly Revenue Trends */
|
|
105
|
+
app.get("/task4", async (req, res) => {
|
|
106
|
+
const year = new Date().getFullYear();
|
|
107
|
+
res.json(
|
|
108
|
+
await Order.aggregate([
|
|
109
|
+
{
|
|
110
|
+
$match: {
|
|
111
|
+
orderDate: {
|
|
112
|
+
$gte: new Date(`${year}-01-01`),
|
|
113
|
+
$lte: new Date(`${year}-12-31`),
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
revenueStage,
|
|
118
|
+
{
|
|
119
|
+
$group: {
|
|
120
|
+
_id: { $month: "$orderDate" },
|
|
121
|
+
totalRevenue: { $sum: "$totalAmount" },
|
|
122
|
+
avgOrderValue: { $avg: "$totalAmount" },
|
|
123
|
+
totalOrders: { $sum: 1 },
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
])
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
/* Task 5: Expensive Orders */
|
|
131
|
+
app.get("/task5", async (req, res) => {
|
|
132
|
+
res.json(
|
|
133
|
+
await Order.aggregate([
|
|
134
|
+
revenueStage,
|
|
135
|
+
{
|
|
136
|
+
$group: {
|
|
137
|
+
_id: null,
|
|
138
|
+
avgAmount: { $avg: "$totalAmount" },
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
$lookup: {
|
|
143
|
+
from: "orders",
|
|
144
|
+
pipeline: [
|
|
145
|
+
revenueStage,
|
|
146
|
+
{
|
|
147
|
+
$match: {
|
|
148
|
+
$expr: { $gt: ["$totalAmount", "$$AVG"] },
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
{ $sort: { totalAmount: -1 } },
|
|
152
|
+
{ $limit: 5 },
|
|
153
|
+
],
|
|
154
|
+
as: "result",
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
{ $project: { _id: 0, result: 1 } },
|
|
158
|
+
])
|
|
159
|
+
);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
/* Task 6: Daily Order Summary */
|
|
163
|
+
app.get("/task6", async (req, res) => {
|
|
164
|
+
res.json(
|
|
165
|
+
await Order.aggregate([
|
|
166
|
+
revenueStage,
|
|
167
|
+
{
|
|
168
|
+
$group: {
|
|
169
|
+
_id: {
|
|
170
|
+
$dateToString: { format: "%Y-%m-%d", date: "$orderDate" },
|
|
171
|
+
},
|
|
172
|
+
totalOrders: { $sum: 1 },
|
|
173
|
+
totalQuantity: { $sum: "$quantity" },
|
|
174
|
+
totalRevenue: { $sum: "$totalAmount" },
|
|
175
|
+
minOrder: { $min: "$totalAmount" },
|
|
176
|
+
maxOrder: { $max: "$totalAmount" },
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
])
|
|
180
|
+
);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
/* Task 7: City-Wise Distribution */
|
|
184
|
+
app.get("/task7", async (req, res) => {
|
|
185
|
+
res.json(
|
|
186
|
+
await Order.aggregate([
|
|
187
|
+
{ $group: { _id: "$city", totalOrders: { $sum: 1 } } },
|
|
188
|
+
{ $match: { totalOrders: { $gt: 10 } } },
|
|
189
|
+
])
|
|
190
|
+
);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
/* Task 8: Category Popularity Index */
|
|
194
|
+
app.get("/task8", async (req, res) => {
|
|
195
|
+
res.json(
|
|
196
|
+
await Order.aggregate([
|
|
197
|
+
{
|
|
198
|
+
$group: {
|
|
199
|
+
_id: "$category",
|
|
200
|
+
totalQuantity: { $sum: "$quantity" },
|
|
201
|
+
avgPrice: { $avg: "$price" },
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
$addFields: {
|
|
206
|
+
popularityScore: {
|
|
207
|
+
$multiply: ["$totalQuantity", "$avgPrice"],
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
{ $sort: { popularityScore: -1 } },
|
|
212
|
+
{ $limit: 5 },
|
|
213
|
+
])
|
|
214
|
+
);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
module.exports = app;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
//Mongoose - Employee Payroll System
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const mongoose = require("mongoose");
|
|
5
|
+
|
|
6
|
+
const app = express();
|
|
7
|
+
app.use(express.json());
|
|
8
|
+
|
|
9
|
+
// MongoDB connection
|
|
10
|
+
mongoose.connect("mongodb://127.0.0.1:27017/payroll");
|
|
11
|
+
|
|
12
|
+
// Schema
|
|
13
|
+
const employeeSchema = new mongoose.Schema({
|
|
14
|
+
name: {
|
|
15
|
+
type: String,
|
|
16
|
+
required: true,
|
|
17
|
+
minlength: 3,
|
|
18
|
+
maxlength: 50,
|
|
19
|
+
},
|
|
20
|
+
employeeId: {
|
|
21
|
+
type: String,
|
|
22
|
+
required: true,
|
|
23
|
+
unique: true,
|
|
24
|
+
},
|
|
25
|
+
departments: [String],
|
|
26
|
+
salary: {
|
|
27
|
+
type: Number,
|
|
28
|
+
min: 10000,
|
|
29
|
+
validate: {
|
|
30
|
+
validator: (v) => v % 1000 === 0,
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
joiningDate: {
|
|
34
|
+
type: Date,
|
|
35
|
+
default: Date.now,
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// Hook
|
|
40
|
+
employeeSchema.pre("save", function (next) {
|
|
41
|
+
this.name = this.name.toUpperCase();
|
|
42
|
+
next();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const Employee = mongoose.model("Employee", employeeSchema);
|
|
46
|
+
|
|
47
|
+
// Routes
|
|
48
|
+
|
|
49
|
+
// Add employee
|
|
50
|
+
app.post("/employees", async (req, res) => {
|
|
51
|
+
try {
|
|
52
|
+
const emp = new Employee(req.body);
|
|
53
|
+
await emp.save();
|
|
54
|
+
res.json(emp);
|
|
55
|
+
} catch (e) {
|
|
56
|
+
res.status(400).json({ error: e.message });
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// View all employees
|
|
61
|
+
app.get("/employees", async (req, res) => {
|
|
62
|
+
const employees = await Employee.find();
|
|
63
|
+
res.json(employees);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// Get employee by ID
|
|
67
|
+
app.get("/employees/:id", async (req, res) => {
|
|
68
|
+
const emp = await Employee.findOne({ employeeId: req.params.id });
|
|
69
|
+
if (!emp) return res.status(404).json({ error: "Not found" });
|
|
70
|
+
res.json(emp);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// Update salary or departments
|
|
74
|
+
app.put("/employees/:id", async (req, res) => {
|
|
75
|
+
const emp = await Employee.findOneAndUpdate(
|
|
76
|
+
{ employeeId: req.params.id },
|
|
77
|
+
req.body,
|
|
78
|
+
{ new: true }
|
|
79
|
+
);
|
|
80
|
+
if (!emp) return res.status(404).json({ error: "Not found" });
|
|
81
|
+
res.json(emp);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// Delete employee
|
|
85
|
+
app.delete("/employees/:id", async (req, res) => {
|
|
86
|
+
await Employee.findOneAndDelete({ employeeId: req.params.id });
|
|
87
|
+
res.json({ message: "Deleted" });
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// Bonus: Average salary by department
|
|
91
|
+
app.get("/average/:dept", async (req, res) => {
|
|
92
|
+
const result = await Employee.aggregate([
|
|
93
|
+
{ $match: { departments: req.params.dept } },
|
|
94
|
+
{ $group: { _id: null, avgSalary: { $avg: "$salary" } } },
|
|
95
|
+
]);
|
|
96
|
+
res.json({ averageSalary: result[0]?.avgSalary || 0 });
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
module.exports = app;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
//mongoose - Event Management System
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const mongoose = require("mongoose");
|
|
5
|
+
|
|
6
|
+
const app = express();
|
|
7
|
+
app.use(express.json());
|
|
8
|
+
|
|
9
|
+
mongoose.connect("mongodb://127.0.0.1:27017/events");
|
|
10
|
+
|
|
11
|
+
// Schema
|
|
12
|
+
const eventSchema = new mongoose.Schema({
|
|
13
|
+
eventName: {
|
|
14
|
+
type: String,
|
|
15
|
+
required: true,
|
|
16
|
+
minlength: 3,
|
|
17
|
+
maxlength: 50,
|
|
18
|
+
},
|
|
19
|
+
date: {
|
|
20
|
+
type: Date,
|
|
21
|
+
validate: {
|
|
22
|
+
validator: (v) => v > new Date(),
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
venue: String,
|
|
26
|
+
participants: {
|
|
27
|
+
type: [String],
|
|
28
|
+
validate: (v) => v.length > 0,
|
|
29
|
+
},
|
|
30
|
+
ticketPrice: {
|
|
31
|
+
type: Number,
|
|
32
|
+
min: 1,
|
|
33
|
+
max: 9999,
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Hooks
|
|
38
|
+
eventSchema.pre("save", function (next) {
|
|
39
|
+
this.eventName = this.eventName.replace(/\b\w/g, (c) => c.toUpperCase());
|
|
40
|
+
next();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
eventSchema.post("save", function () {
|
|
44
|
+
console.log("Event added successfully.");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const Event = mongoose.model("Event", eventSchema);
|
|
48
|
+
|
|
49
|
+
// Routes
|
|
50
|
+
|
|
51
|
+
// Add event
|
|
52
|
+
app.post("/events", async (req, res) => {
|
|
53
|
+
try {
|
|
54
|
+
const event = new Event(req.body);
|
|
55
|
+
await event.save();
|
|
56
|
+
res.json(event);
|
|
57
|
+
} catch (e) {
|
|
58
|
+
res.status(400).json({ error: e.message });
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// View all events
|
|
63
|
+
app.get("/events", async (req, res) => {
|
|
64
|
+
res.json(await Event.find());
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// Update event
|
|
68
|
+
app.put("/events/:id", async (req, res) => {
|
|
69
|
+
const event = await Event.findByIdAndUpdate(req.params.id, req.body, {
|
|
70
|
+
new: true,
|
|
71
|
+
});
|
|
72
|
+
if (!event) return res.status(404).json({ error: "Not found" });
|
|
73
|
+
res.json(event);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// Delete event
|
|
77
|
+
app.delete("/events/:id", async (req, res) => {
|
|
78
|
+
await Event.findByIdAndDelete(req.params.id);
|
|
79
|
+
res.json({ message: "Deleted" });
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Bonus: Events by venue
|
|
83
|
+
app.get("/events/venue/:venue", async (req, res) => {
|
|
84
|
+
res.json(await Event.find({ venue: req.params.venue }));
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
module.exports = app;
|
package/package.json
ADDED