post.controller.ts 2.31 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
18
  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
19

Lee Soobeom's avatar
Lee Soobeom committed
20
  console.log("body", req.body);
Lee Soobeom's avatar
Lee Soobeom committed
21

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

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

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

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

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

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

  return res.json(newPost);
54
});
Lee Soobeom's avatar
Lee Soobeom committed
55
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

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);
});