-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.routes.js
More file actions
156 lines (126 loc) · 3.9 KB
/
user.routes.js
File metadata and controls
156 lines (126 loc) · 3.9 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const express = require('express');
const mongodb = require('mongodb');
const { Database } = require('./database');
const { databaseConfiguration } = require('./config');
const { getWeather } = require('./services/weather.service');
const { databaseName, collectionName } = databaseConfiguration;
const connection = Database.connection;
const database = connection.db(databaseName);
const collection = database.collection(collectionName);
const router = express.Router();
function toObjectId(stringId) {
try {
return mongodb.ObjectId.createFromHexString(stringId);
} catch (error) {
console.error(error);
throw new Error('Invalid ID');
}
}
// Create (POST) - Add a new user
router.post('/', async (req, res) => {
const newDocument = req.body;
let result = await collection.insertOne(newDocument);
return res.status(201).send(result);
});
// Read (GET) - Get all users
router.get('/', async (_req, res) => {
const results = await collection.find().toArray();
return res.status(200).send(results);
});
// Read (GET) - Get a user by ID
router.get('/:id', async (req, res) => {
const id = req?.params?.id;
if (!id) {
return res.status(400).send('\'id\' is required');
}
let _id;
try {
_id = toObjectId(id);
} catch (error) {
return res.status(400).send(error.message);
}
const user = await collection.findOne({ _id });
if (!user) {
return res.status(404).send('Not found');
}
return res.status(200).send(user);
});
// Read (GET) - Get a user's weather by ID
router.get('/:id/weather', async (req, res) => {
const id = req?.params?.id;
if (!id) {
return res.status(400).send('\'id\' is required');
}
let _id;
try {
_id = toObjectId(id);
} catch (error) {
return res.status(400).send(error.message);
}
const user = await collection.findOne({ _id });
if (!user) {
return res.status(404).send('Not found');
}
if (!user.city) {
return res.status(400).send('User does not have a city');
}
try {
const weather = await getWeather(user.city);
return res.status(200).send(weather);
} catch (error) {
return res.status(500).send(error.message);
}
});
// Read (GET) - Get a user by email or name
router.get('/query/:query', async (req, res) => {
const query = req?.params?.query;
if (!query) {
return res.status(400).send('\'query\' is required');
}
const user = await collection.findOne({ $or: [{ email: query }, { name: query }] });
if (!user) {
return res.status(404).send('User not found');
}
return res.status(200).json(user);
});
// Update (PUT) - Update an existing user by ID
router.put('/:id', async (req, res) => {
const id = req?.params?.id;
const updatedDocument = req.body;
if (!id || !updatedDocument) {
return res.status(400).send('Both \'id\' and \'updatedDocument\' are required');
}
let _id;
try {
_id = toObjectId(id);
} catch (error) {
return res.status(400).send(error.message);
}
const result = await collection.updateOne(
{ _id },
{ $set: updatedDocument }
);
if (result.matchedCount === 0) {
return res.status(404).send('User not found');
}
return res.status(200).send('User updated successfully');
});
// Delete (DELETE) - Delete a user by ID
router.delete("/:id", async (req, res) => {
const id = req?.params?.id;
if (!id) {
return res.status(400).send('\'id\' is required');
}
let _id;
try {
_id = toObjectId(id);
} catch (error) {
return res.status(400).send(error.message);
}
const result = await collection.deleteOne({ _id });
if (result.deletedCount === 0) {
return res.status(404).send('User not found');
}
return res.status(200).send(result);
});
module.exports = router;