post.controller.ts 3.19 KB
Newer Older
Lee Soobeom's avatar
Lee Soobeom committed
1
import { NextFunction, Request, Response } from "express";
2
3
import isLength from "validator/lib/isLength";
import equals from "validator/lib/equals";
Lee Soobeom's avatar
Lee Soobeom committed
4
import { TypedRequestAuth } from "./auth.controller";
Lee Soobeom's avatar
Lee Soobeom committed
5
import { asyncWrap } from "../helpers";
Lee Soobeom's avatar
Lee Soobeom committed
6
7
import { postDb, userDb } from "../db";
import { TypedRequest } from "../types";
Lee Soobeom's avatar
Lee Soobeom committed
8

Lee Soobeom's avatar
Lee Soobeom committed
9
10
export const postCreate = asyncWrap(async (reqExp, res, next) => {
  const req = reqExp as TypedRequestAuth<{ userId: string }>;
Lee Soobeom's avatar
Lee Soobeom committed
11

Lee Soobeom's avatar
Lee Soobeom committed
12
13
14
15
16
17
  const { title, text, theme, city, date } = req.body as {
    title: string;
    text: string;
    theme: string;
    city: string;
    date: Date;
Lee Soobeom's avatar
Lee Soobeom committed
18
    counts: number;
Lee Soobeom's avatar
Lee Soobeom committed
19
  };
Lee Soobeom's avatar
Lee Soobeom committed
20

21
22
23
24
  // 1) title 빈 문자열인지 확인
  if (!isLength(title ?? "", { min: 1 })) {
    return res.status(422).send("제목을 한 글자 이상 입력해주세요");
  }
Lee Soobeom's avatar
Lee Soobeom committed
25

26
27
28
29
  // 2) body 빈 문자열인지 확인
  if (!isLength(text ?? "", { min: 1 })) {
    return res.status(422).send("제목을 한 글자 이상 입력해주세요");
  }
Lee Soobeom's avatar
Lee Soobeom committed
30

Lee Soobeom's avatar
Lee Soobeom committed
31
  // 3) theme dropdown default-value "테마"일 경우 에러
32
33
34
  if (equals(theme, "질문종류")) {
    return res.status(422).send("테마를 입력해 주세요");
  }
Lee Soobeom's avatar
Lee Soobeom committed
35

Lee Soobeom's avatar
Lee Soobeom committed
36
  // 4) city dropdown default-value "도시"일 경우 에러
37
38
39
  if (equals(city, "질문종류")) {
    return res.status(422).send("도시를 선택해 주세요");
  }
Lee Soobeom's avatar
Lee Soobeom committed
40

Lee Soobeom's avatar
Lee Soobeom committed
41
  const userId = req.auth.userId;
Lee Soobeom's avatar
Lee Soobeom committed
42

Lee Soobeom's avatar
Lee Soobeom committed
43
  const newPost = await postDb.createPost({
Lee Soobeom's avatar
Lee Soobeom committed
44
45
46
47
    title,
    text,
    theme,
    city,
Lee Soobeom's avatar
Lee Soobeom committed
48
    date: Date.now(),
Lee Soobeom's avatar
Lee Soobeom committed
49
    user: userId,
Lee Soobeom's avatar
Lee Soobeom committed
50
  });
Lee Soobeom's avatar
Lee Soobeom committed
51

Lee Soobeom's avatar
Lee Soobeom committed
52
53
  console.log("post", newPost);

Lee Soobeom's avatar
Lee Soobeom committed
54
  return res.json(newPost);
55
});
Lee Soobeom's avatar
Lee Soobeom committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

export const getAllPost = asyncWrap(async (req, res) => {
  const posts = await postDb.getPosts();
  // console.log(posts);

  return res.json(posts);
});

export const addCounts = asyncWrap(async (req, res) => {
  // console.log(req.body);
  const { postId } = req.params;
  const { counts } = req.body as {
    counts: number;
  };
  console.log(postId, counts);

  const updateCounts = await postDb.addOneCount(postId, counts);

  return res.json(updateCounts);
});

export const userByPostId = (
  reqExp: Request,
  res: Response,
  next: NextFunction,
  postId: string
) => {
  const req = reqExp as TypedRequest;
  req.user = userDb.findUserByPostId(postId);
  next();
};

export const getOnePost = asyncWrap(async (req, res) => {
  const { postId } = req.params;
  const post = await postDb.getPost(postId);

  return res.json(post);
});
Lee Soobeom's avatar
Lee Soobeom committed
94
95
96

export const deleteOnePost = asyncWrap(async (req, res) => {
  const { postId } = req.params;
Lee Soobeom's avatar
Lee Soobeom committed
97
  // console.log(postId);
Lee Soobeom's avatar
Lee Soobeom committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  const deleteCount = await postDb.deletePost(postId);

  return res.json(deleteCount);
});

export const updatePost = asyncWrap(async (reqExp, res) => {
  const req = reqExp as TypedRequestAuth<{ userId: string }>;

  const { title, text, theme, city, date } = req.body as {
    title: string;
    text: string;
    theme: string;
    city: string;
    date: Date;
    counts: number;
  };

  const userId = req.auth.userId;
  const { postId } = req.params;

  const updatePost = await postDb.updateOnePost(
    {
      title,
      text,
      theme,
      city,
      date: Date.now(),
      counts: req.body.counts,
      user: userId,
    },
    postId
  );

  console.log("게시글 수정 후", updatePost);

  return res.json(updatePost);
});