|
1 | | -// import req and res types for type safety |
2 | | -import { Request, Response } from 'express'; |
3 | | -import { User } from '../models/user_model'; // imports user model created with mongoose |
| 1 | +import { Request, Response } from "express"; |
| 2 | +import { User } from "../models/user_model"; |
4 | 3 |
|
5 | 4 | // controller: GET /api/users |
6 | 5 | // This function handles GET reqs to /api/users |
7 | 6 | // It fetches all users from MongoDB and sends them as json |
8 | 7 |
|
9 | 8 | export const getUsers = async (_req: Request, res: Response) => { |
10 | | - try { |
11 | | - // retrieves all docs from "users" collection |
12 | | - const users = await User.find().lean(); // .lean() returns plain JS objects instead of Mongoose docs, may change incase we need extra model methods later |
13 | | - res.json(users); // sends list of users back as JSON response |
14 | | - } catch (err) { // log the error if something goes wrong |
15 | | - console.error('GET /api/users error:', err); |
16 | | - res.status(500).json({ error: 'Failed to fetch users' }); |
17 | | - } |
| 9 | + try { |
| 10 | + const users = await User.find().lean(); |
| 11 | + res.json(users); |
| 12 | + } catch (err: any) { |
| 13 | + console.error("GET /api/users error:", err); |
| 14 | + res.status(500).json({ |
| 15 | + error: "Failed to fetch users", |
| 16 | + message: err.message, |
| 17 | + }); |
| 18 | + } |
18 | 19 | }; |
19 | 20 |
|
20 | | - |
21 | 21 | // controller: POST /api/users |
22 | 22 | // reads data from req body, vailidates it and creates a new user |
23 | 23 | export const createUser = async (req: Request, res: Response) => { |
24 | | - try { |
25 | | - // Destructure the req body sent by the client |
26 | | - // The ?? {} ensures we don't get error if req.body is undefined |
27 | | - const { name, email } = req.body ?? {}; |
| 24 | + try { |
| 25 | + const { name, email } = req.body ?? {}; |
28 | 26 |
|
29 | | - // Basic validation to ensure both name and email are provided |
30 | | - // if not respond with bad request and stop further processes |
31 | | - if (!name || !email) { |
32 | | - return res.status(400).json({ error: 'name and email required' }); |
33 | | - } |
| 27 | + if (!name || !email) { |
| 28 | + return res.status(400).json({ error: "name and email required" }); |
| 29 | + } |
34 | 30 |
|
35 | | - // create a new user doc in DB using Mongoose's .create() |
36 | | - const user = await User.create({ name, email }); |
37 | | - // respond with "created" and send back created user as JSON |
38 | | - res.status(201).json(user); |
39 | | - } catch(err: any) { |
40 | | - // Handle duplicate email error |
41 | | - // Mongo DB rejects duplicat value since we have email marked as 'unique' |
42 | | - if (err?.code === 11000) { |
43 | | - return res.status(409).json({ error: 'email already exists' }); |
44 | | - } |
45 | | - |
46 | | - // for all other errors, log them and return generic 500 response |
47 | | - console.error('POST /api/users error:', err); |
48 | | - res.status(500).json({error: 'Failed to create user' }); |
| 31 | + const user = await User.create({ name, email }); |
| 32 | + res.status(201).json(user); |
| 33 | + } catch (err: any) { |
| 34 | + if (err?.code === 11000) { |
| 35 | + return res.status(409).json({ error: "email already exists" }); |
49 | 36 | } |
| 37 | + |
| 38 | + console.error("POST /api/users error:", err); |
| 39 | + res.status(500).json({ error: "Failed to create user" }); |
| 40 | + } |
50 | 41 | }; |
0 commit comments