user.controller.ts 1.2 KB
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
import { NextFunction, Request, Response } from "express";
2
3
import { userDb } from "../db";
import { asyncWrap } from "../helpers/asyncWrap";
Yoon, Daeki's avatar
Yoon, Daeki committed
4
import { TypedRequestAuth } from "./auth.controller";
5
6
7
8
9

export const createUser = asyncWrap(async (req, res) => {
  const user = req.body;
  console.log("user body", user);
  const newUser = await userDb.createUser(user);
Yoon, Daeki's avatar
Yoon, Daeki committed
10
11
12
13
14
15
16
17
  return res.json(newUser);
});

export const deleteUser = asyncWrap(async (req, res) => {
  const { userId } = req.params;
  console.log("user id:", userId);
  const deletedUser = await userDb.deleteUserById(userId);
  return res.json(deletedUser);
18
});
Yoon, Daeki's avatar
Yoon, Daeki committed
19

Yoon, Daeki's avatar
Yoon, Daeki committed
20
21
22
23
24
export const getUsers = asyncWrap(async (req, res) => {
  const users = await userDb.getUsers();
  return res.json(users);
});

Yoon, Daeki's avatar
Yoon, Daeki committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
export const userById = async (
  reqExp: Request,
  res: Response,
  next: NextFunction,
  userId: string
) => {
  try {
    const req = reqExp as TypedRequestAuth<{ userId: string }>;
    let user = await userDb.findUserById(userId);
    if (!user) {
      return res.status(404).send("사용자를 찾을 수 없습니다");
    }
    req.user = user;
    next();
  } catch (error: any) {
    return res.status(500).send(error.message || "사용자 찾기 중 오류");
  }
};