-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
124 lines (110 loc) · 3.27 KB
/
example.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
const express = require("express");
const { setRoot, createDatabase } = require("potatodb");
const app = express();
// configure express app
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// set potatodb root
setRoot(__dirname, "databases");
// create project database and users farm
let DB, User;
(async () => {
DB = await createDatabase("DB", false);
User = await DB.createFarm("Users", {
identifications: true,
timestamps: true,
});
// listen to server requests
app.listen(3000, () => {
console.log("Server running on port 3000");
});
})();
// create user
app.post("/create-user", async (req, res) => {
try {
const user = await User.insertOne(req.body);
res.status(200).json({ success: true, userId: user._id });
} catch (err) {
console.log(err);
res.status(400).json({ success: false, error: err.message });
}
});
// get user
app.get("/get-user", async (req, res) => {
try {
const user = await User.findOne(
{ username: req.body.username },
{ project: { password: 0 } }
);
res.status(200).json({ success: true, user });
} catch (err) {
console.log(err);
res.status(400).json({ success: false, error: err.message });
}
});
// get users (implementing pagination)
app.get("/get-users/:pageNumber", async (req, res) => {
const resultsPerPage = 10;
try {
/*
1- implement pagination using skip and limit options
2- show most recent data first
3- sort data according to "user.personalInformation.age" field in ascending order
*/
const users = await User.findMany(
{},
{
skip: resultsPerPage * (req.params.pageNumber - 1),
limit: resultsPerPage,
recent: true,
sort: {
"personalInformation.age": 1,
},
project: {
password: 0,
},
}
);
res.status(200).json({ success: true, users });
} catch (err) {
console.log(err);
res.status(400).json({ success: false, error: err.message });
}
});
// update username
app.patch("/update-username", async (req, res) => {
try {
const updatedUser = await User.updateOne(
{
username: req.body.username,
},
{
username: req.body.newUsername,
},
{
updated: true,
project: {
password: 0,
},
}
);
res.status(200).json({ success: true, updatedUser });
} catch (err) {
console.log(err);
res.status(400).json({ success: false, error: err.message });
}
});
// delete user
app.delete("/delete-user/:userId", async (req, res) => {
try {
const deletedUser = await User.deleteOne(
{ _id: req.params.userId },
{ project: { password: 0 } }
);
res.status(200).json({ success: true, deletedUser });
} catch (err) {
console.log(err);
res.status(400).json({ success: false, error: err.message });
}
});