Commit 71aa5b17 authored by Lee Soobeom's avatar Lee Soobeom
Browse files

token user.id ref

parent 9fe5e544
import axios from "axios"; import axios from "axios";
import baseUrl from "./baseUrl"; 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); const { data } = await axios.post(`${baseUrl}/posts/`, post);
return data; return data;
}; };
...@@ -10,7 +10,7 @@ export default function Post({ handleClick, post }: Props) { ...@@ -10,7 +10,7 @@ export default function Post({ handleClick, post }: Props) {
return ( return (
<div className="flex flex-row h-16 divide-x-2 border-2 border-solid"> <div className="flex flex-row h-16 divide-x-2 border-2 border-solid">
<div className="basis-full"> <div className="basis-full">
<button id={post.id} onClick={handleClick}> <button id={post.userId} onClick={handleClick}>
{post.title} {post.title}
</button> </button>
</div> </div>
......
import React, { FormEvent, useState } from "react"; import React, { FormEvent, useState } from "react";
import { useNavigate } from "react-router-dom";
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 { addDataList } from "../board/board";
import { catchErrors } from "../helpers"; import { catchErrors } from "../helpers";
import { PostingType } from "../types"; import { PostType } from "../types";
import { postApi } from "../apis"; import { postApi } from "../apis";
export default function Posting() { export default function Posting() {
...@@ -10,13 +12,15 @@ export default function Posting() { ...@@ -10,13 +12,15 @@ export default function Posting() {
const [theme, setTheme] = useState<string>("질문종류"); const [theme, setTheme] = useState<string>("질문종류");
const [title, setTitle] = useState<string>(""); const [title, setTitle] = useState<string>("");
const [text, setText] = useState<string>(""); const [text, setText] = useState<string>("");
const navigate = useNavigate();
const [user, setUser] = useState<PostingType>({ const [user, setUser] = useState<PostType>({
title: "", title: "",
text: "", text: "",
theme: "", theme: "",
city: "", city: "",
username: "", userId: "",
counts: 0,
}); });
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
...@@ -32,8 +36,10 @@ export default function Posting() { ...@@ -32,8 +36,10 @@ export default function Posting() {
if (postingFormMatch()) { if (postingFormMatch()) {
setLoading(true); setLoading(true);
const res = await postApi.posting(user); const res = await postApi.posting(user);
console.log("서버연결됬나요", res); // addDataList(res);
console.log("user save"); // console.log("서버연결됬나요", res);
// console.log("user save");
navigate("/board", { replace: true });
setSuccess(true); setSuccess(true);
setError(""); setError("");
} }
......
...@@ -9,18 +9,14 @@ export interface LoginUser { ...@@ -9,18 +9,14 @@ export interface LoginUser {
password: string; password: string;
} }
export interface PostType extends PostingType { export interface PostType {
date?: string;
counts: number;
id?: string;
}
export interface PostingType {
title: string; title: string;
text?: string; text?: string;
theme: string; theme: string;
city: string; city: string;
username: string; date?: string;
counts: number;
userId: string;
} }
export interface SignupUser { export interface SignupUser {
......
import { NextFunction, Request, Response } from "express"; 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 { requireLogin } from "./auth.controller"; import { TypedRequestAuth } from "./auth.controller";
import { asyncWrap } from "../helpers"; import { asyncWrap } from "../helpers";
import { postDb } from "../db"; import { postDb } from "../db";
export const posting = asyncWrap(async (req, res) => { export const postCreate = asyncWrap(async (reqExp, res, next) => {
const { title, text, theme, city, username, date, counts } = req.body; 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 빈 문자열인지 확인 // 1) title 빈 문자열인지 확인
if (!isLength(title ?? "", { min: 1 })) { if (!isLength(title ?? "", { min: 1 })) {
...@@ -32,16 +38,16 @@ export const posting = asyncWrap(async (req, res) => { ...@@ -32,16 +38,16 @@ export const posting = asyncWrap(async (req, res) => {
return res.status(422).send("도시를 선택해 주세요"); 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, title,
text, text,
theme, theme,
city, city,
username,
date, date,
counts, user: userId,
}); });
return res.json(newPosting);
return res.json(newPost);
}); });
import { Posting, PostingType } from "../models";
import { Post, PostType } from "../models"; import { Post, PostType } from "../models";
export const createPosting = async (posting: PostingType) => { export const createPost = async (post: PostType) => {
const newPosting = await Posting.create({ const newPosting = await Post.create({
title: posting.title, title: post.title,
text: posting.text, text: post.text,
theme: posting.theme, theme: post.theme,
city: posting.city, city: post.city,
username: posting.username, user: post.user,
date: posting.date, date: post.date,
counts: 0, counts: 0,
}); });
return newPosting; 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, Types } from "mongoose"; import { Document, model, Schema, Types } from "mongoose";
import { PostingType } from "./posting.model"; import { Posting } from ".";
export interface PostType extends PostingType { export interface PostType {
date?: string; title: string;
counts: number; text?: string;
id?: string; theme: string;
city: string;
user?: Types.ObjectId | string;
date?: Date;
counts?: number;
} }
const postSchema = new Schema<PostType>({ const PostSchema = new Schema<PostType>({
title: { type: String }, title: {
theme: { type: String }, type: String,
city: { type: String }, required: true,
username: { type: String }, },
date: { type: String }, text: {
counts: { type: Number }, 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<PostType>("Post", postSchema); export default model<PostType>("Post", PostSchema);
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<PostingType>({
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<PostingType>("Posting", postingSchema);
...@@ -3,6 +3,6 @@ import { postCtrl, authCtrl } from "../controllers"; ...@@ -3,6 +3,6 @@ import { postCtrl, authCtrl } from "../controllers";
const router = express.Router(); const router = express.Router();
router.route("/").post(authCtrl.requireLogin, postCtrl.posting); router.route("/").post(authCtrl.requireLogin, postCtrl.postCreate);
export default router; export default router;
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