post.controller.ts 1.38 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 { requireLogin } from "./auth.controller";
Lee Soobeom's avatar
Lee Soobeom committed
5
import { asyncWrap } from "../helpers";
Lee Soobeom's avatar
Lee Soobeom committed
6
import { postDb } from "../db";
Lee Soobeom's avatar
Lee Soobeom committed
7
8

export const posting = asyncWrap(async (req, res) => {
Lee Soobeom's avatar
Lee Soobeom committed
9
  const { title, text, theme, city, username, date, counts } = req.body;
Lee Soobeom's avatar
Lee Soobeom committed
10

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

Lee Soobeom's avatar
Lee Soobeom committed
13
14
  // 0) 로그인 했는지 확인 requireLogin

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

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

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

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

Lee Soobeom's avatar
Lee Soobeom committed
35
  // 5) username ref: cookie.token._id -> collection users, "User"->  name
Lee Soobeom's avatar
Lee Soobeom committed
36
37
38
39
40
41
42

  const newPosting = await postDb.createPosting({
    title,
    text,
    theme,
    city,
    username,
Lee Soobeom's avatar
Lee Soobeom committed
43
44
    date,
    counts,
Lee Soobeom's avatar
Lee Soobeom committed
45
46
  });
  return res.json(newPosting);
47
});