서울
-
-
+
+
diff --git a/src/controllers/post.controller.ts b/src/controllers/post.controller.ts
index 49127bc8e174eb0aaeedd479cf23fff3a166dd9b..3bfee3a3abcd7c5006e41828fb5f721a0de22ca6 100644
--- a/src/controllers/post.controller.ts
+++ b/src/controllers/post.controller.ts
@@ -5,7 +5,7 @@ import { asyncWrap } from "../helpers";
import { postDb, userDb } from "../db";
import { TypedRequest } from "../types";
-export const createImgAndPost = asyncWrap(async (reqExp, res, next) => {
+export const createFileAndPost = asyncWrap(async (reqExp, res, next) => {
const req = reqExp as TypedRequestAuth<{ userId: string }>;
const { userId } = req.auth;
@@ -16,21 +16,9 @@ export const createImgAndPost = asyncWrap(async (reqExp, res, next) => {
multiples: true,
});
- form.parse(req, (err, fields, files) => {
- if (Array.isArray(files.picture)) {
- for (var i = 0; i < files.picture.length; i++) {
- const originalfilename = files.picture?.[i].originalFilename;
- const newfilename = files.picture?.[i].newFilename;
- const filepath = files.picture?.[i].filepath;
-
- const filesRes = postDb.createFilesRow(
- originalfilename,
- newfilename,
- filepath
- );
- }
- } // create fileinfos row
+ const fileIdArr = new Array();
+ form.parse(req, async (err, fields, files) => {
if (!Array.isArray(fields.title)) {
const title = fields.title;
if (!Array.isArray(fields.text)) {
@@ -40,7 +28,35 @@ export const createImgAndPost = asyncWrap(async (reqExp, res, next) => {
if (!Array.isArray(fields.city)) {
const city = fields.city;
- const postRes = postDb.createPostRow({
+ if (Array.isArray(files.picture)) {
+ for (var i = 0; i < files.picture.length; i++) {
+ const originalfilename = files.picture?.[i].originalFilename;
+ const newfilename = files.picture?.[i].newFilename;
+ const filepath = files.picture?.[i].filepath;
+
+ const filesRes = await postDb.createFilesRow(
+ originalfilename,
+ newfilename,
+ filepath
+ );
+
+ fileIdArr.push(filesRes);
+ }
+ } else if (!Array.isArray(files.picture)) {
+ const originalfilename = files.picture.originalFilename;
+ const newfilename = files.picture.newFilename;
+ const filepath = files.picture.filepath;
+
+ const filesRes = await postDb.createFilesRow(
+ originalfilename,
+ newfilename,
+ filepath
+ );
+
+ fileIdArr.push(filesRes);
+ } // file or one or more
+
+ const postRes = await postDb.createPostRow({
title,
text,
theme,
@@ -48,7 +64,8 @@ export const createImgAndPost = asyncWrap(async (reqExp, res, next) => {
date: Date.now(),
counts: 0,
user: userId,
- }); // create posts
+ file: fileIdArr,
+ });
}
}
}
@@ -58,18 +75,25 @@ export const createImgAndPost = asyncWrap(async (reqExp, res, next) => {
export const getAllPost = asyncWrap(async (req, res) => {
const posts = await postDb.getPosts();
- console.log(posts);
+ // console.log(posts);
return res.json(posts);
});
+export const getFiles = asyncWrap(async (req, res) => {
+ const { postId } = req.params;
+ // console.log("나는 말하는 고구마.", postId);
+ const files = await postDb.getFilesByPostId(postId);
+
+ return res.json(files);
+});
+
export const addCounts = asyncWrap(async (req, res) => {
- // console.log(req.body);
const { postId } = req.params;
const { counts } = req.body as {
counts: number;
};
- console.log(postId, counts);
+ // console.log(postId, counts);
const updateCounts = await postDb.addOneCount(postId, counts);
@@ -130,7 +154,7 @@ export const updatePost = asyncWrap(async (reqExp, res) => {
postId
);
- console.log("게시글 수정 후", updatePost);
+ // console.log("게시글 수정 후", updatePost);
return res.json(updatePost);
});
diff --git a/src/db/post.db.ts b/src/db/post.db.ts
index 9ce3319a567ba4af70ddd5034a64d2d297678dbf..d7d8ee3c389484236bfe7d7974d4ef80f2346127 100644
--- a/src/db/post.db.ts
+++ b/src/db/post.db.ts
@@ -1,3 +1,4 @@
+import { Types, ObjectId } from "mongoose";
import { Post, PostType } from "../models";
import { FileInfo, IFileInfo } from "../models";
@@ -10,8 +11,8 @@ export const createPostRow = async (post: PostType) => {
user: post.user,
date: post.date,
counts: 0,
+ file: post.file,
});
- console.log("check", newPostRow);
return newPostRow;
};
@@ -25,22 +26,18 @@ export const createFilesRow = async (
newfilename: newfilename,
picturepath: picturepath,
});
- return newFileRow;
+ // console.log("check", newFileRow);
+ return newFileRow._id;
};
-export const findFile = async (postId: string) => {
- const fileInfo = await Post.findById({ postId }).populate("file");
- return fileInfo;
-};
-
-export const findFileByPostInfo = async (title: string, usreId: string) => {
- const posts = await Post.find({ title: title, userId: usreId });
+export const getPosts = async () => {
+ const posts = await Post.find({}).sort({ date: -1 });
return posts;
};
-export const getPosts = async () => {
- const posts = await Post.find({});
- return posts;
+export const getFilesByPostId = async (postId: string) => {
+ const files = await Post.findOne({ _id: postId }).populate("file");
+ return files?.file;
};
export const addOneCount = async (_id: string, counts: number) => {
@@ -49,8 +46,6 @@ export const addOneCount = async (_id: string, counts: number) => {
{ counts: counts },
{ new: true }
);
- // console.log(newCounts);
-
return newCounts;
};
diff --git a/src/models/fileinfo.model.ts b/src/models/fileinfo.model.ts
index fc05cc8a7b9b524784ccdac5989dca419dd230f7..804c1d8d69290aab2bc2cfdacb8d5879c35299b9 100644
--- a/src/models/fileinfo.model.ts
+++ b/src/models/fileinfo.model.ts
@@ -1,4 +1,4 @@
-import { model, Schema } from "mongoose";
+import { model, ObjectId, Schema } from "mongoose";
export interface IFileInfo {
originalfilename?: string;
diff --git a/src/models/post.model.ts b/src/models/post.model.ts
index 1570fd0abece6bd105078cbf4d04c985da1c40c1..be0df095d0236e4415d822ccb8bcdbe0985e6382 100644
--- a/src/models/post.model.ts
+++ b/src/models/post.model.ts
@@ -8,7 +8,7 @@ export interface PostType {
date: Date | number;
counts?: number;
user: Types.ObjectId | string;
- file?: Types.ObjectId | string;
+ file?: Array
;
}
const PostSchema = new Schema({
@@ -38,10 +38,12 @@ const PostSchema = new Schema({
type: Number,
default: 0,
},
- file: {
- type: Schema.Types.ObjectId,
- ref: "FileInfo",
- },
+ file: [
+ {
+ type: Schema.Types.ObjectId,
+ ref: "FileInfo",
+ },
+ ],
});
export default model("Post", PostSchema);
diff --git a/src/routes/post.route.ts b/src/routes/post.route.ts
index 71aebe8be3f2b465a429af358071472a52357ef8..ad2dc5142e4a32f0f4006fe24fb8adfcf9376bd1 100644
--- a/src/routes/post.route.ts
+++ b/src/routes/post.route.ts
@@ -3,14 +3,17 @@ import { postCtrl, authCtrl, fileInfoCtrl } from "../controllers";
const router = express.Router();
-router.route("/").post(authCtrl.requireLogin, postCtrl.createImgAndPost);
+router.route("/").post(authCtrl.requireLogin, postCtrl.createFileAndPost);
router.route("/").get(postCtrl.getAllPost);
+router.route("/files/:postId").get(authCtrl.requireLogin, postCtrl.getFiles);
+
+// router.param("postId", postCtrl.userByPostId);
router
.route("/:postId")
.post(authCtrl.requireLogin, postCtrl.addCounts)
.get(authCtrl.requireLogin, postCtrl.getOnePost)
- .delete(authCtrl.requireLogin, postCtrl.deleteOnePost) // authenticate
+ .delete(authCtrl.requireLogin, postCtrl.deleteOnePost) // +authenticate
.put(authCtrl.requireLogin, postCtrl.updatePost);
router.param("postId", postCtrl.userByPostId);