user.controller.ts 1.35 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

Yoon, Daeki's avatar
Yoon, Daeki committed
6
7
8
9
10
11
12
13
14
interface TypedRequest extends Request {
  auth: any;
  user: any;
  files: any;
}

export const createUser = asyncWrap(async (reqExp, res) => {
  const req = reqExp as TypedRequest;

15
16
  const user = req.body;
  console.log("user body", user);
Yoon, Daeki's avatar
Yoon, Daeki committed
17
  console.log("files ", req.files);
18
  const newUser = await userDb.createUser(user);
Yoon, Daeki's avatar
Yoon, Daeki committed
19
20
21
22
23
24
25
26
  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);
27
});
Yoon, Daeki's avatar
Yoon, Daeki committed
28

Yoon, Daeki's avatar
Yoon, Daeki committed
29
30
31
32
33
export const getUsers = asyncWrap(async (req, res) => {
  const users = await userDb.getUsers();
  return res.json(users);
});

Yoon, Daeki's avatar
Yoon, Daeki committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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 || "사용자 찾기 중 오류");
  }
};