-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
106 lines (93 loc) · 2.43 KB
/
index.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
const express = require("express");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const morgan = require("morgan");
const cors = require("cors");
dotenv.config();
const app = express();
const port = process.env.PORT || 3000;
const userSchema = new mongoose.Schema({
avatar: String,
name: String,
headline: String,
tags: [String],
id: String,
});
const User = mongoose.model("User", userSchema);
app.use(express.json());
app.use(morgan("tiny"));
app.use(cors());
// POST route to add a new user
app.post("/api/users", async (req, res) => {
try {
const newUser = new User(req.body);
await newUser.save();
res.status(201).json(newUser);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
// POST route to get all users
app.get("/api/users", async (req, res) => {
try {
const users = await User.find();
res.json(users);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
//Search route to find users with a given tag
app.get('/api/users/tag/:tag', async (req, res) => {
try {
const tag = req.params.tag;
const users = await User.find({
$or: [{ tags: new RegExp(tag, "i") }]
});
res.json(users);
} catch (error) {
res.status(400).json({error: error.message});
}
})
// SEARCH route to find users by tag or any other attribute
app.get("/api/users/search", async (req, res) => {
try {
const { query } = req.query;
const users = await User.find({
$or: [
{ name: new RegExp(query, "i") },
{ headline: new RegExp(query, "i") },
{ tags: new RegExp(query, "i") },
],
});
res.json(users);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// endpoint to get mostly used tags and their number
app.get("/api/mostUsedTags", async (req, res) => {
try {
const tagsAggregation = await User.aggregate([
{ $unwind: "$tags" },
{
$group: {
_id: "$tags",
count: { $sum: 1 },
},
},
{ $sort: { count: -1 } },
{ $limit: 5 },
]);
const mostUsedTags = tagsAggregation.map(({ _id, count }) => ({
tag: _id,
count,
}));
res.json(mostUsedTags);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
app.listen(port, () => {
mongoose.connect(process.env.MONGODB_URI);
console.log(`Server is running on port ${port} && database is connected`);
});