user.controller.ts 2.5 KB
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
import { userDb } from "../db";
import { asyncWrap } from "../helpers/asyncWrap";
Kim, MinGyu's avatar
Kim, MinGyu committed
3
import { Request } from "express";
백승민's avatar
백승민 committed
4
import formidable from "formidable";
5
6
import { ObjectId } from "mongoose";
import fs from "fs";
Kim, MinGyu's avatar
Kim, MinGyu committed
7
import { TypedRequest } from "../types";
Kim, MinGyu's avatar
Kim, MinGyu committed
8
9
10
11

export interface TypedRequestAuth<T> extends Request {
  auth: T;
}
Yoon, Daeki's avatar
Yoon, Daeki committed
12
13
14
15
16
17
18
19
20
21

export const getUsers = asyncWrap(async (req, res) => {
  const users = await userDb.getUsers();
  return res.json(users);
});

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
22
  return res.json(newUser);
Yoon, Daeki's avatar
Yoon, Daeki committed
23
});
Kim, MinGyu's avatar
Kim, MinGyu committed
24
25

export const getProfile = asyncWrap(async (reqExp, res) => {
26
  const req = reqExp as TypedRequestAuth<{ userId: string }>; // 앞에서는 토큰으로써 사용하기 때문에 JwtPayload 를 사용하고 여기서는 verify 에서 토큰을 디코딩했기에 ObjectId 타입의 string으로 바뀌게 된다.
Kim, MinGyu's avatar
Kim, MinGyu committed
27

28
  const { userId } = req.auth;
Kim, MinGyu's avatar
Kim, MinGyu committed
29
  const profile = await userDb.getProfile(userId);
30
31
32
  res.json(profile);
});

33
export const updateProfile = asyncWrap(async (reqExp, res) => {
Kim, MinGyu's avatar
Kim, MinGyu committed
34
  const req = reqExp as TypedRequest;
35
  const { userId } = req.auth;
36
37
  const { name } = req.body;
  const { avatar }: { avatar: formidable.File } = req.files;
38

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  const user = await userDb.updateProfile(userId, name, avatar);

  // if (!Array.isArray(file.avatar)) {
  //   //파일 좁히기 중
  //   if (!Array.isArray(field.nickname)) {
  //     const nickname = field.nickname;
  //     if (!(file.avatar === undefined)) {
  //       const originalfilename = file.avatar.originalFilename;
  //       const newfilename = file.avatar.newFilename;
  //       const picturepath = file.avatar.filepath;
  //       userDb.updateProfile(
  //         userId,
  //         nickname,
  //         originalfilename,
  //         newfilename,
  //         picturepath
  //       );
  //     } else {
  //       userDb.updateProfile(userId, nickname);
  //     }
  //   }
  // }
Kim, MinGyu's avatar
Kim, MinGyu committed
61

62
  res.json(user);
63
});
Kim, MinGyu's avatar
Kim, MinGyu committed
64
65

export const deleteUser = asyncWrap(async (reqExp, res) => {
Kim, MinGyu's avatar
Kim, MinGyu committed
66
  const req = reqExp as TypedRequestAuth<{ userId: string }>; // 앞에서는 토큰으로써 사용하기 때문에 JwtPayload 를 사용하고 여기서는 verify 에서 토큰을 디코딩했기에 ObjectId 타입의 string으로 바뀌게 된다.
Kim, MinGyu's avatar
Kim, MinGyu committed
67
68

  const { userId } = req.auth;
Kim, MinGyu's avatar
Kim, MinGyu committed
69
70
71
  const finish = await userDb.deleteUser(userId);
  if (finish?.deletedCount === 1) {
    res.json(true);
Kim, MinGyu's avatar
Kim, MinGyu committed
72
73
  } else {
    res.status(422).send("삭제에 실패하였습니다.");
Kim, MinGyu's avatar
Kim, MinGyu committed
74
  }
Kim, MinGyu's avatar
Kim, MinGyu committed
75
});