@@ -18,9 +25,12 @@ export default function Header() {
diff --git a/frontend/src/types/index.tsx b/frontend/src/types/index.tsx
index 0047911c423c1a94765554f56ccfb1fff89da0fb..c95f450aac5c3cc19909341171e009762aa85693 100644
--- a/frontend/src/types/index.tsx
+++ b/frontend/src/types/index.tsx
@@ -43,6 +43,9 @@ export interface MainimgType {
_id: string;
theme: string;
city: string;
- url: string;
title: string;
+ pic: {
+ originalfilename: string;
+ newfilename: string;
+ };
}
diff --git a/src/app.ts b/src/app.ts
index b1c55bfb03c74b4ae75407eedd5e7b54320249bb..365b1171ea87773befaf5527f511fe0a8cbf4ac9 100644
--- a/src/app.ts
+++ b/src/app.ts
@@ -12,6 +12,7 @@ app.use(cookieParser());
app.use("/api", router);
app.use("/images", express.static(path.join(__dirname, "..", "/uploads")));
+app.use("/images", express.static(path.join(__dirname, "..", "/adminpics")));
app.use((err: any, req: Request, res: Response, next: NextFunction) => {
console.log("익스프레스 에러: ", err);
diff --git a/src/controllers/mainimg.controller.ts b/src/controllers/mainimg.controller.ts
index edb21d3efd8e04a3f5a90a3333d76d13a27add5f..83836e0041f93c6b8302f73409d8df3d380e2a4e 100644
--- a/src/controllers/mainimg.controller.ts
+++ b/src/controllers/mainimg.controller.ts
@@ -4,35 +4,53 @@ import { TypedRequestAuth } from "./auth.controller";
import { asyncWrap } from "../helpers";
import { mainimgDb } from "../db";
import { TypedRequest } from "../types";
+import { ObjectId } from "mongoose";
+import formidable from "formidable";
-export const createMainimg = asyncWrap(async (reqExp, res, next) => {
- const req = reqExp as TypedRequestAuth<{ userId: string }>;
+export const createMainimg = asyncWrap(async (reqExp, res) => {
+ const req = reqExp as TypedRequestAuth<{ userId: ObjectId }>;
+ const { userId } = req.auth;
- const { theme, city, url, title } = req.body as {
- theme: string;
- city: string;
- url: string;
- title: string;
- };
-
- console.log("body", req.body);
-
- if (!isLength(url ?? "", { min: 1 })) {
- return res.status(422).send("이미지 url을 입력해주세요");
- }
+ const form = formidable({
+ uploadDir: "adminpics",
+ keepExtensions: true,
+ multiples: false,
+ });
- if (!isLength(title ?? "", { min: 1 })) {
- return res.status(422).send("이미지 제목을 입력해주세요");
- }
+ form.parse(req, (err, fields, files) => {
+ if (!Array.isArray(files.mainimg)) {
+ //파일 좁히기 중
+ if (
+ !(
+ Array.isArray(fields.city) ||
+ Array.isArray(fields.title) ||
+ Array.isArray(fields.theme)
+ )
+ ) {
+ const city = fields.city;
+ const title = fields.title;
+ const theme = fields.theme;
- const newMainimg = await mainimgDb.createMainimg({
- theme,
- city,
- url,
- title,
+ // if (!isLength(title ?? "", { min: 1 })) {
+ // return res.status(422).send("이미지 제목을 입력해주세요");
+ // }
+ console.log(files);
+ const originalfilename = files.mainimg?.originalFilename;
+ const newfilename = files.mainimg.newFilename;
+ if (!(originalfilename === null || newfilename === undefined)) {
+ mainimgDb.createMainimg(
+ { city, title, theme },
+ {
+ originalfilename,
+ newfilename,
+ }
+ );
+ }
+ }
+ }
});
- return res.json(newMainimg);
+ res.json();
});
export const getMainimg = asyncWrap(async (req, res) => {
@@ -40,7 +58,6 @@ export const getMainimg = asyncWrap(async (req, res) => {
return res.json(mainimgs);
});
-
export const deleteMainimg = asyncWrap(async (req, res) => {
const { imgId } = req.params;
console.log(imgId);
@@ -48,6 +65,3 @@ export const deleteMainimg = asyncWrap(async (req, res) => {
return res.json(deleteCount);
});
-
-
-
diff --git a/src/controllers/user.controller.ts b/src/controllers/user.controller.ts
index 070b6e20b0a0866d02f4a397de78ebc57da1939d..cc157cbf59fe58b9f74fed4653dd43a04a44d56a 100644
--- a/src/controllers/user.controller.ts
+++ b/src/controllers/user.controller.ts
@@ -1,7 +1,7 @@
import { userDb } from "../db";
import { asyncWrap } from "../helpers/asyncWrap";
import { Request } from "express";
-import formidable, { Fields, Files } from "formidable";
+import formidable from "formidable";
import { ObjectId } from "mongoose";
import fs from "fs";
diff --git a/src/db/mainimg.db.ts b/src/db/mainimg.db.ts
index 924b3d088b0477cfb1046c81cbbe8a43be81fae2..6da5569fe765376231edcf9ed78308aad5a4a3e3 100644
--- a/src/db/mainimg.db.ts
+++ b/src/db/mainimg.db.ts
@@ -1,19 +1,27 @@
-import { Mainimg, MainimgType } from "../models";
+import { ObjectId } from "mongoose";
+import { Avatar, IAvatar, Mainimg, MainimgType } from "../models";
+
+export const createMainimg = async (mainimg: MainimgType, pic: IAvatar) => {
+ const newPic = await Avatar.create({
+ originalfilename: pic.originalfilename,
+ newfilename: pic.newfilename,
+ pictureauth: pic.picturepath,
+ });
-export const createMainimg = async (mainimg: MainimgType) => {
const newMainimg = await Mainimg.create({
theme: mainimg.theme,
city: mainimg.city,
- url: mainimg.url,
+ pic: newPic._id,
title: mainimg.title,
});
return newMainimg;
};
export const getMainimg = async () => {
- const users = await Mainimg.find({});
- return users;
- };
+ const img = await Mainimg.find({}).populate("pic");
+
+ return img;
+};
export const deleteOneMainimg = async (_id: string) => {
const res = await Mainimg.deleteOne({ _id: _id });
diff --git a/src/db/user.db.ts b/src/db/user.db.ts
index f05c955a0f1b6896db40cbf010a62d4c5cc4a810..e4c1c55907d7b970bbaded03d1c5bb7928deac9b 100644
--- a/src/db/user.db.ts
+++ b/src/db/user.db.ts
@@ -1,7 +1,7 @@
import bcrypt from "bcryptjs";
import { ObjectId } from "mongoose";
import { IUser, Role, Post, User, Avatar } from "../models";
-import fs from "fs";
+import fs from "fs/promises";
export const createUser = async (user: IUser) => {
// 비밀번호 암호화
@@ -106,9 +106,7 @@ export const deleteUser = async (userId: ObjectId) => {
const user = await User.findById(userId);
if (!(user?.avatar === undefined)) {
const ref = await Avatar.findById(user.avatar._id);
- fs.unlink("../travel/uploads/" + ref?.newfilename, (err) => {
- console.log(err);
- });
+ await fs.unlink("../travel/uploads/" + ref?.newfilename);
await Avatar.deleteOne({ _id: user.avatar._id });
await User.deleteOne({ _id: userId });
}
diff --git a/src/models/mainimg.model.ts b/src/models/mainimg.model.ts
index 70b1e4d3d05622ac76fa75d3c6c2f0f4a6dbd1eb..da24530922fab52e6e5205ce6b20463a5790013b 100644
--- a/src/models/mainimg.model.ts
+++ b/src/models/mainimg.model.ts
@@ -1,28 +1,24 @@
-import {model, Schema } from "mongoose";
+import { model, Schema, Types } from "mongoose";
export interface MainimgType {
-
- theme: string;
- city: string;
- url: string;
- title: string;
+ theme: string;
+ city: string;
+ title: string;
+ pic?: Types.ObjectId;
}
const MainimgSchema = new Schema({
- theme: {
- type: String,
- },
- city: {
- type: String,
- },
- url : {
- type: String,
- },
- title: {
- type: String,
- required: true,
- },
-
+ theme: {
+ type: String,
+ },
+ city: {
+ type: String,
+ },
+ title: {
+ type: String,
+ required: true,
+ },
+ pic: { type: Schema.Types.ObjectId, ref: "Avatar" },
});
export default model("Mainimg", MainimgSchema);