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

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

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

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

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

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

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

  // 5) username 확인 필요 없음

  // 6)
  const newPosting = await postDb.createPosting({
    title,
    text,
    theme,
    city,
    username,
  });
  return res.json(newPosting);
43
});