Commit 208862f5 authored by Lee Soobeom's avatar Lee Soobeom
Browse files

DB에 posting table 저장

parent 18cf7518
...@@ -2,9 +2,10 @@ import { NextFunction, Request, Response } from "express"; ...@@ -2,9 +2,10 @@ import { NextFunction, Request, Response } from "express";
import isLength from "validator/lib/isLength"; import isLength from "validator/lib/isLength";
import equals from "validator/lib/equals"; import equals from "validator/lib/equals";
import { asyncWrap } from "../helpers"; import { asyncWrap } from "../helpers";
import { postDb } from "../db";
export const posting = asyncWrap(async (req, res) => { export const posting = asyncWrap(async (req, res) => {
const { title, text, date, theme, city } = req.body; const { title, text, theme, city, username } = req.body;
console.log("body", req.body); console.log("body", req.body);
...@@ -18,15 +19,25 @@ export const posting = asyncWrap(async (req, res) => { ...@@ -18,15 +19,25 @@ export const posting = asyncWrap(async (req, res) => {
return res.status(422).send("제목을 한 글자 이상 입력해주세요"); return res.status(422).send("제목을 한 글자 이상 입력해주세요");
} }
// 3) submit 이벤트 발생시 date값 입력 // 3) theme dropdown default-value "테마"일 경우 에러
// 4) theme dropdown default-value "테마"일 경우 에러
if (equals(theme, "질문종류")) { if (equals(theme, "질문종류")) {
return res.status(422).send("테마를 입력해 주세요"); return res.status(422).send("테마를 입력해 주세요");
} }
// 5) city dropdown default-value "도시"일 경우 에러 // 4) city dropdown default-value "도시"일 경우 에러
if (equals(city, "질문종류")) { if (equals(city, "질문종류")) {
return res.status(422).send("도시를 선택해 주세요"); return res.status(422).send("도시를 선택해 주세요");
} }
// 5) username 확인 필요 없음
// 6)
const newPosting = await postDb.createPosting({
title,
text,
theme,
city,
username,
});
return res.json(newPosting);
}); });
export * as userDb from "./user.db"; export * as userDb from "./user.db";
// export * as postDb from "./post.db"; export * as postDb from "./post.db";
import { Posting, PostingType } from "../models";
export const createPosting = async (posting: PostingType) => {
const newPosting = await Posting.create({
title: posting.title,
text: posting.text,
theme: posting.theme,
city: posting.city,
username: posting.username,
});
return newPosting;
};
export { default as User, IUser } from "./user.model"; export { default as User, IUser } from "./user.model";
export { default as Posting, PostingType } from "./posting.model";
export { default as Post, PostType } from "./post.model"; export { default as Post, PostType } from "./post.model";
import { model, Schema } from "mongoose"; import { model, Schema, Types } from "mongoose";
import { PostingType } from "./posting.model";
export interface PostType extends PostingType { export interface PostType extends PostingType {
date?: string; date?: string;
...@@ -6,23 +7,13 @@ export interface PostType extends PostingType { ...@@ -6,23 +7,13 @@ export interface PostType extends PostingType {
id?: string; id?: string;
} }
export interface PostingType { const postSchema = new Schema<PostType>({
title: string;
text?: string;
theme: string;
city: string;
username: string;
}
const schema = new Schema<PostType>({
id: { type: String },
title: { type: String }, title: { type: String },
date: { type: Date },
text: { type: String },
counts: { type: Number },
theme: { type: String }, theme: { type: String },
city: { type: String }, city: { type: String },
username: { type: String }, username: { type: String },
date: { type: String },
counts: { type: Number },
}); });
export default model<PostType>("Post", schema); export default model<PostType>("Post", postSchema);
import { model, Schema } from "mongoose";
export interface PostingType {
title: string;
text?: string;
theme: string;
city: string;
username?: string;
}
const postingSchema = new Schema<PostingType>(
{
title: {
type: String,
required: true,
unique: true,
},
text: {
type: String,
required: true,
unique: true,
},
theme: {
type: String,
unique: true,
},
city: {
type: String,
unique: true,
},
// username: {
// type: String,
// unique: true,
// },
} // username 때문에 duplicate key error 발생
);
export default model<PostingType>("Posting", postingSchema);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment