diff --git a/frontend/src/apis/post.api.ts b/frontend/src/apis/post.api.ts index 9669e265d62105b9e793ba4e82cef699f492d23d..4fd7aeab497b6d3c89732cafdbc1afcb32891c8e 100644 --- a/frontend/src/apis/post.api.ts +++ b/frontend/src/apis/post.api.ts @@ -1,8 +1,9 @@ import axios from "axios"; import baseUrl from "./baseUrl"; -import { PostingType } from "../types"; +import { PostType } from "../types"; -export const posting = async (post: PostingType) => { +export const posting = async (post: PostType) => { const { data } = await axios.post(`${baseUrl}/posts/`, post); + return data; }; diff --git a/frontend/src/post/post.tsx b/frontend/src/post/post.tsx index ed1a46a00488da85fc2e5b3d6f374471ae9976d8..7ac0a1b78be0029bc11dcbf3ae10c73845efbdbb 100644 --- a/frontend/src/post/post.tsx +++ b/frontend/src/post/post.tsx @@ -10,7 +10,7 @@ export default function Post({ handleClick, post }: Props) { return (
-
diff --git a/frontend/src/post/posting.tsx b/frontend/src/post/posting.tsx index 9222ed7afe918107227e112274300baa39e0e293..35ef90a97b5020a347870d4dd55fbbc7cc188ea4 100644 --- a/frontend/src/post/posting.tsx +++ b/frontend/src/post/posting.tsx @@ -1,8 +1,10 @@ import React, { FormEvent, useState } from "react"; +import { useNavigate } from "react-router-dom"; import isLength from "validator/lib/isLength"; import equals from "validator/lib/equals"; +// import { addDataList } from "../board/board"; import { catchErrors } from "../helpers"; -import { PostingType } from "../types"; +import { PostType } from "../types"; import { postApi } from "../apis"; export default function Posting() { @@ -10,13 +12,15 @@ export default function Posting() { const [theme, setTheme] = useState("질문종류"); const [title, setTitle] = useState(""); const [text, setText] = useState(""); + const navigate = useNavigate(); - const [user, setUser] = useState({ + const [user, setUser] = useState({ title: "", text: "", theme: "", city: "", - username: "", + userId: "", + counts: 0, }); const [loading, setLoading] = useState(false); @@ -32,8 +36,10 @@ export default function Posting() { if (postingFormMatch()) { setLoading(true); const res = await postApi.posting(user); - console.log("서버연결됬나요", res); - console.log("user save"); + // addDataList(res); + // console.log("서버연결됬나요", res); + // console.log("user save"); + navigate("/board", { replace: true }); setSuccess(true); setError(""); } diff --git a/frontend/src/types/index.tsx b/frontend/src/types/index.tsx index d160968b7dc849dbdcfdd25c4b9dce8ab8de9d87..e94755676caaf213cc0ca32d5a6abfd59193acbf 100644 --- a/frontend/src/types/index.tsx +++ b/frontend/src/types/index.tsx @@ -9,18 +9,14 @@ export interface LoginUser { password: string; } -export interface PostType extends PostingType { - date?: string; - counts: number; - id?: string; -} - -export interface PostingType { +export interface PostType { title: string; text?: string; theme: string; city: string; - username: string; + date?: string; + counts: number; + userId: string; } export interface SignupUser { diff --git a/src/controllers/post.controller.ts b/src/controllers/post.controller.ts index 86c182dea9f15536a1f5609934686a7bfd0b5dc9..a4824f0c62e4b0cb60e7eb856309deb71484de02 100644 --- a/src/controllers/post.controller.ts +++ b/src/controllers/post.controller.ts @@ -1,16 +1,22 @@ import { NextFunction, Request, Response } from "express"; import isLength from "validator/lib/isLength"; import equals from "validator/lib/equals"; -import { requireLogin } from "./auth.controller"; +import { TypedRequestAuth } from "./auth.controller"; import { asyncWrap } from "../helpers"; import { postDb } from "../db"; -export const posting = asyncWrap(async (req, res) => { - const { title, text, theme, city, username, date, counts } = req.body; +export const postCreate = asyncWrap(async (reqExp, res, next) => { + const req = reqExp as TypedRequestAuth<{ userId: string }>; - console.log("body", req.body); + const { title, text, theme, city, date } = req.body as { + title: string; + text: string; + theme: string; + city: string; + date: Date; + }; - // 0) 로그인 했는지 확인 requireLogin + console.log("body", req.body); // 1) title 빈 문자열인지 확인 if (!isLength(title ?? "", { min: 1 })) { @@ -32,16 +38,16 @@ export const posting = asyncWrap(async (req, res) => { return res.status(422).send("도시를 선택해 주세요"); } - // 5) username ref: cookie.token._id -> collection users, "User"-> name + const userId = req.auth.userId; - const newPosting = await postDb.createPosting({ + const newPost = await postDb.createPost({ title, text, theme, city, - username, date, - counts, + user: userId, }); - return res.json(newPosting); + + return res.json(newPost); }); diff --git a/src/db/post.db.ts b/src/db/post.db.ts index 23ecb4a369c85a52661eafee3d0037c0b2206408..02eb27056591c7ea5cf2a95d9bb56c75b61fdefe 100644 --- a/src/db/post.db.ts +++ b/src/db/post.db.ts @@ -1,14 +1,13 @@ -import { Posting, PostingType } from "../models"; import { Post, PostType } 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, - date: posting.date, +export const createPost = async (post: PostType) => { + const newPosting = await Post.create({ + title: post.title, + text: post.text, + theme: post.theme, + city: post.city, + user: post.user, + date: post.date, counts: 0, }); return newPosting; diff --git a/src/models/index.ts b/src/models/index.ts index d90d00921712981c307806e09adeac8c9c104791..44b5b8a54e99b65485de80aa064b39770cb7fdb1 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -1,3 +1,2 @@ export { default as User, IUser } from "./user.model"; -export { default as Posting, PostingType } from "./posting.model"; export { default as Post, PostType } from "./post.model"; diff --git a/src/models/post.model.ts b/src/models/post.model.ts index e9264ac65afbde841a3d3d60c63dbc8e33262535..f2c7c1ddbe79997df9213312bbb7e9e7fce4e386 100644 --- a/src/models/post.model.ts +++ b/src/models/post.model.ts @@ -1,19 +1,42 @@ -import { model, Schema, Types } from "mongoose"; -import { PostingType } from "./posting.model"; +import { Document, model, Schema, Types } from "mongoose"; +import { Posting } from "."; -export interface PostType extends PostingType { - date?: string; - counts: number; - id?: string; +export interface PostType { + title: string; + text?: string; + theme: string; + city: string; + user?: Types.ObjectId | string; + date?: Date; + counts?: number; } -const postSchema = new Schema({ - title: { type: String }, - theme: { type: String }, - city: { type: String }, - username: { type: String }, - date: { type: String }, - counts: { type: Number }, +const PostSchema = new Schema({ + title: { + type: String, + required: true, + }, + text: { + type: String, + required: true, + }, + theme: { + type: String, + }, + city: { + type: String, + }, + user: { + type: Schema.Types.ObjectId, + ref: "User", + }, + date: { + type: Date, + default: Date.now, + }, + counts: { + type: Number, + }, }); -export default model("Post", postSchema); +export default model("Post", PostSchema); diff --git a/src/models/posting.model.ts b/src/models/posting.model.ts deleted file mode 100644 index 3ade8f7b7a1898bdac64c8dd2e6cc1da8b52f61e..0000000000000000000000000000000000000000 --- a/src/models/posting.model.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { model, Schema } from "mongoose"; - -export interface PostingType { - title: string; - text?: string; - theme: string; - city: string; - username?: string; - date?: string; - counts?: number; -} - -const postingSchema = new Schema({ - title: { - type: String, - required: true, - }, - text: { - type: String, - required: true, - }, - theme: { - type: String, - }, - city: { - type: String, - }, - username: { - type: String, - // username: travelreport.users.findOne({id: cookie.token._id(Schema.Tpyes.ObjectId)}).name, - }, - date: { - type: Date, - default: Date.now, - }, - counts: { - type: Number, - }, - date: { - type: Date, - dafault: Date.now, - }, - counts: 0, -}); - -export default model("Posting", postingSchema); diff --git a/src/routes/post.route.ts b/src/routes/post.route.ts index 0464fb7cee5c11927af754b8f6fbb62f2b8c549b..304b8348c21a361c883f7583ff62fe2215bef4df 100644 --- a/src/routes/post.route.ts +++ b/src/routes/post.route.ts @@ -3,6 +3,6 @@ import { postCtrl, authCtrl } from "../controllers"; const router = express.Router(); -router.route("/").post(authCtrl.requireLogin, postCtrl.posting); +router.route("/").post(authCtrl.requireLogin, postCtrl.postCreate); export default router;