forked from github/copilot-cli-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.js
More file actions
54 lines (46 loc) · 1.18 KB
/
Copy pathusers.js
File metadata and controls
54 lines (46 loc) · 1.18 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
/**
* User API endpoints
*/
const express = require('express');
const router = express.Router();
const User = require('../models/User');
// Get all users
router.get('/', async (req, res) => {
try {
const users = await User.findAll();
res.json(users);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Get user by ID
router.get('/:id', async (req, res) => {
const user = await User.findById(req.params.id);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
res.json(user);
});
// Create new user
router.post('/', async (req, res) => {
const { name, email, password } = req.body;
// TODO: Add input validation
const user = await User.create({ name, email, password });
res.status(201).json(user);
});
// Update user
router.put('/:id', async (req, res) => {
const user = await User.findById(req.params.id);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
Object.assign(user, req.body);
await user.save();
res.json(user);
});
// Delete user
router.delete('/:id', async (req, res) => {
await User.deleteById(req.params.id);
res.status(204).send();
});
module.exports = router;