import { NextFunction, Request, Response } from "express"; import isLength from "validator/lib/isLength"; import equals from "validator/lib/equals"; import { asyncWrap } from "../helpers"; import { postDb } from "../db"; export const posting = asyncWrap(async (req, res) => { const { title, text, theme, city, username } = req.body; console.log("body", req.body); // 1) title 빈 문자열인지 확인 if (!isLength(title ?? "", { min: 1 })) { return res.status(422).send("제목을 한 글자 이상 입력해주세요"); } // 2) body 빈 문자열인지 확인 if (!isLength(text ?? "", { min: 1 })) { return res.status(422).send("제목을 한 글자 이상 입력해주세요"); } // 3) theme dropdown default-value "테마"일 경우 에러 if (equals(theme, "질문종류")) { return res.status(422).send("테마를 입력해 주세요"); } // 4) city dropdown default-value "도시"일 경우 에러 if (equals(city, "질문종류")) { return res.status(422).send("도시를 선택해 주세요"); } // 5) username 확인 필요 없음 // 6) const newPosting = await postDb.createPosting({ title, text, theme, city, username, }); return res.json(newPosting); });