From 18f1a3f8519847b3a3a6b6df7c7bf532cc54a1d5 Mon Sep 17 00:00:00 2001 From: Lee Soobeom Date: Wed, 27 Jul 2022 10:26:22 +0900 Subject: [PATCH] update debugging --- frontend/src/auth/profile.tsx | 2 +- frontend/src/board/board.tsx | 2 +- frontend/src/types/index.tsx | 1 + src/controllers/post.controller.ts | 145 +++++++++++++++++++---------- src/routes/post.route.ts | 3 +- 5 files changed, 102 insertions(+), 51 deletions(-) diff --git a/frontend/src/auth/profile.tsx b/frontend/src/auth/profile.tsx index ccf4863..07ef7af 100644 --- a/frontend/src/auth/profile.tsx +++ b/frontend/src/auth/profile.tsx @@ -59,7 +59,7 @@ export default function Profile() { const user: Profile = await profileApi.profile(); console.log("user in effect", user); setEmail(user.email); - setAvatarUrl(user.avatar?.newfilename); + setAvatarUrl(user.fileInfo?.newfilename); setProfile({ ...profile, name: user.name }); }; getProfile(); diff --git a/frontend/src/board/board.tsx b/frontend/src/board/board.tsx index 2f1c786..6d3db5d 100644 --- a/frontend/src/board/board.tsx +++ b/frontend/src/board/board.tsx @@ -18,7 +18,7 @@ export default function BoardPage() { // posts const getDataList = async () => { const res = await postApi.getData(); - setPosts(res); + setPosts(res); //posts = res }; const titleHandleClick = async (event: MouseEvent) => { diff --git a/frontend/src/types/index.tsx b/frontend/src/types/index.tsx index a4617ba..59a1ca3 100644 --- a/frontend/src/types/index.tsx +++ b/frontend/src/types/index.tsx @@ -39,6 +39,7 @@ export interface SignupUser { export interface Profile { _id: string; email: string; + name: string; fileInfo: { originalfilename: string; newfilename: string; diff --git a/src/controllers/post.controller.ts b/src/controllers/post.controller.ts index 4a79fb4..ecb92dc 100644 --- a/src/controllers/post.controller.ts +++ b/src/controllers/post.controller.ts @@ -209,49 +209,53 @@ export const updateOnePost = asyncWrap(async (reqExp, res) => { ); console.log("no files update", postRes2); } else { - const oldFilesId = await postDb.getFilesByPostId(postId); - if (!(oldFilesId === undefined)) { - for (var i = 0; i < oldFilesId?.length; i++) { - const name = await postDb.getOriginalFileName(oldFilesId[i]); - if (!(name === undefined)) { - oldSet.add(name); + if (Array.isArray(files.picture)) { + const oldFilesId = await postDb.getFilesByPostId(postId); + if (!(oldFilesId === undefined)) { + for (var i = 0; i < oldFilesId?.length; i++) { + const name = await postDb.getOriginalFileName( + oldFilesId[i] + ); + if (!(name === undefined)) { + oldSet.add(name); + } } } - } - console.log("OldSet", oldSet); + console.log("OldSet", oldSet); - if (Array.isArray(files.picture)) { - for (var i = 0; i < files.picture.length; i++) { - const newFileName = files.picture?.[i].originalFilename; - if (!(newFileName === undefined || newFileName === null)) { - newSet.add(newFileName); + if (Array.isArray(files.picture)) { + for (var i = 0; i < files.picture.length; i++) { + const newFileName = files.picture?.[i].originalFilename; + if (!(newFileName === undefined || newFileName === null)) { + newSet.add(newFileName); + } } } - } - console.log("NewSet", newSet); + console.log("NewSet", newSet); - //유지, 삭제, 추가 구분하기 - const trdPart = subTract(oldSet, newSet); + //유지, 삭제, 추가 구분하기 + const trdPart = subTract(oldSet, newSet); - console.log("keep", trdPart.keep); - console.log("drop", trdPart.drop); - console.log("add", trdPart.add); + console.log("keep", trdPart.keep); + console.log("drop", trdPart.drop); + console.log("add", trdPart.add); - // 삭제 - for (var i = 0; i < trdPart.drop.length; i++) { - const dropRes = await postDb.deleteFileByName(trdPart.drop[i]); - console.log("delete counts", dropRes); - } + // 삭제 + for (var i = 0; i < trdPart.drop.length; i++) { + const dropRes = await postDb.deleteFileByName( + trdPart.drop[i] + ); + console.log("delete counts", dropRes); + } - //유지 - for (var i = 0; i < trdPart.keep.length; i++) { - const keepRes = await postDb.findByName(trdPart.keep[i]); - fileIdArr.push(keepRes[0]._id); - // console.log("keep Id", keepRes[0]._id); - } + //유지 + for (var i = 0; i < trdPart.keep.length; i++) { + const keepRes = await postDb.findByName(trdPart.keep[i]); + fileIdArr.push(keepRes[0]._id); + // console.log("keep Id", keepRes[0]._id); + } - //추가 - 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; @@ -269,22 +273,69 @@ export const updateOnePost = asyncWrap(async (reqExp, res) => { } } } - } - console.log("all fileId", fileIdArr); + } else { + const oldFilesId = await postDb.getFilesByPostId(postId); + if (!(oldFilesId === undefined)) { + for (var i = 0; i < oldFilesId?.length; i++) { + const name = await postDb.getOriginalFileName( + oldFilesId[i] + ); + if (!(name === undefined)) { + oldSet.add(name); + } + } + } + console.log("OldSet", oldSet); - //post정보 + file정보 update - const postRes1 = await postDb.updatePostRow( - { - title, - text, - theme, - city, - date: Date.now(), - file: fileIdArr, - }, - postId - ); + const newFileName = files.picture.originalFilename; + if (!(newFileName === undefined || newFileName === null)) { + newSet.add(newFileName); + } + console.log("NewSet", newSet); + + //유지, 삭제, 추가 구분하기 + const trdPart = subTract(oldSet, newSet); + + //삭제 + for (var i = 0; i < trdPart.drop.length; i++) { + const dropRes = await postDb.deleteFileByName( + trdPart.drop[i] + ); + console.log("delete counts", dropRes); + } + + //추가 + const originalfilename = files.picture.originalFilename; + const newfilename = files.picture.newFilename; + const filepath = files.picture.filepath; + for (var j = 0; j < trdPart.add.length; j++) { + const check = trdPart.add[j]; + if (originalfilename === check) { + const addRes = await postDb.createFilesRow( + originalfilename, + newfilename, + filepath + ); + + fileIdArr.push(addRes._id); + } + } + } } + console.log("all fileId", fileIdArr); + + //post정보 + file정보 update + const postRes1 = await postDb.updatePostRow( + { + title, + text, + theme, + city, + date: Date.now(), + file: fileIdArr, + }, + postId + ); } } } diff --git a/src/routes/post.route.ts b/src/routes/post.route.ts index 7996d8d..6381974 100644 --- a/src/routes/post.route.ts +++ b/src/routes/post.route.ts @@ -1,5 +1,5 @@ import express from "express"; -import { postCtrl, authCtrl, fileInfoCtrl } from "../controllers"; +import { postCtrl, authCtrl } from "../controllers"; const router = express.Router(); @@ -8,7 +8,6 @@ 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) -- GitLab