diff --git a/frontend/src/auth/profile.tsx b/frontend/src/auth/profile.tsx index 9e6e54ed09e636728e81fc34ae4e6209cfbf0257..0383e305fd138589df057e425530758014d39ac4 100644 --- a/frontend/src/auth/profile.tsx +++ b/frontend/src/auth/profile.tsx @@ -36,10 +36,15 @@ export default function Profile() { } }; + const onNickChange = (e: React.ChangeEvent) => { + const nickname = e.target.value; + setNickname(nickname); + }; + const userChange = async () => { const profile = await handleProfile(); setEmail(profile.email); - setPicturename(profile.newfilename); + setPicturename(profile.avatar.newfilename); }; const handleClick = async ( e: React.MouseEvent @@ -48,6 +53,7 @@ export default function Profile() { if (!(file === undefined || file === null)) { formdata.append("picture", file); } + formdata.append("nickname", nickname); await profileApi.picture(formdata); }; @@ -80,7 +86,7 @@ export default function Profile() { /> ) : ( @@ -104,6 +110,7 @@ export default function Profile() { diff --git a/frontend/src/types/index.tsx b/frontend/src/types/index.tsx index cebf5401ced8ed9cdcf96244a5ba53f3dc3e6335..3a913d7a6946b62d7f1f567284299c015405e169 100644 --- a/frontend/src/types/index.tsx +++ b/frontend/src/types/index.tsx @@ -31,8 +31,10 @@ export interface SignupUser { export interface Profile { _id: string; email: string; - originalfilename: string; - newfilename: string; - picturepath: string; - nickname: string; + avatar: { + originalfilename: string; + newfilename: string; + picturepath: string; + nickname: string; + }; } diff --git a/src/controllers/user.controller.ts b/src/controllers/user.controller.ts index 6997ef26f64c7e6ea7f37c356c75d0e2a1010086..7ed7d68b94876166c7c17092dfbef79f9f5e7de5 100644 --- a/src/controllers/user.controller.ts +++ b/src/controllers/user.controller.ts @@ -34,7 +34,7 @@ export const postPicture = asyncWrap(async (reqExp, res) => { const { userId } = req.auth; const form = formidable({ - uploadDir: "./src/upload", + uploadDir: "uploads", keepExtensions: true, multiples: false, }); @@ -42,11 +42,34 @@ export const postPicture = asyncWrap(async (reqExp, res) => { form.parse(req, (err, fields, files) => { if (!Array.isArray(files.picture)) { //파일 좁히기 중 + if (Array.isArray(fields.nickname)) { + console.log(fields.nickname); + const nickname = fields.nickname.join(); - const originalfilename = files.picture.originalFilename; - const newfilename = files.picture.newFilename; - const picturepath = files.picture.filepath; - userDb.postPicture(userId, originalfilename, newfilename, picturepath); + const originalfilename = files.picture.originalFilename; + const newfilename = files.picture.newFilename; + const picturepath = files.picture.filepath; + userDb.postPicture( + userId, + originalfilename, + newfilename, + picturepath, + nickname + ); + } else { + const nickname = fields.nickname; + + const originalfilename = files.picture.originalFilename; + const newfilename = files.picture.newFilename; + const picturepath = files.picture.filepath; + userDb.postPicture( + userId, + originalfilename, + newfilename, + picturepath, + nickname + ); + } } }); res.json(); diff --git a/src/db/user.db.ts b/src/db/user.db.ts index 56996632f87d897feede94e73d2a5f0c6c94a5c6..1eab6987f041e6c8a6d09cf1d4194b36a2d0ee49 100644 --- a/src/db/user.db.ts +++ b/src/db/user.db.ts @@ -1,10 +1,11 @@ import bcrypt from "bcryptjs"; import { ObjectId } from "mongoose"; -import { IUser, Role, Post, User } from "../models"; +import { IUser, Role, Post, User, Avatar } from "../models"; export const createUser = async (user: IUser) => { // 비밀번호 암호화 const hash = await bcrypt.hash(user.password, 10); + const newAvatar = await Avatar.create({}); // 사용자 역할 추가: 기본값은 "user" let userRole = null; if (user.role) { @@ -17,6 +18,7 @@ export const createUser = async (user: IUser) => { password: hash, role: userRole, isNew: true, + avatar: newAvatar, }); const retUser = await newUser.save(); return retUser; @@ -39,8 +41,9 @@ export const findUserByPostId = async (postId: string) => { const post = await Post.findOne({ _id: postId }).populate("user"); return post?.user; }; + export const getProfile = async (userId: string) => { - const profile = await User.findById(userId); + const profile = await User.findById(userId).populate("avatar"); return profile; //이름 수정 }; @@ -67,25 +70,20 @@ export const isValidUserId = async (userId: string) => { } }; -export const postPicture = ( +export const postPicture = async ( userId: ObjectId, originalfilename: string | null, newfilename: string, - picturepath: string + picturepath: string, + nickname: string ) => { - User.findByIdAndUpdate( - userId, - { + const profile = await User.findById(userId); + if (!(profile?.avatar === undefined)) { + await Avatar.findByIdAndUpdate(profile.avatar._id, { originalfilename: originalfilename, newfilename: newfilename, picturepath: picturepath, - }, - function (err: any, docs: any) { - if (err) { - console.log(err); - } else { - console.log("Updated User : ", docs); - } - } - ); + nickname: nickname, + }); + } }; diff --git a/src/models/fileinfo.model.ts b/src/models/fileinfo.model.ts new file mode 100644 index 0000000000000000000000000000000000000000..b6a071f79e66a80d9437bfd786534c97b1a16c5f --- /dev/null +++ b/src/models/fileinfo.model.ts @@ -0,0 +1,17 @@ +import { model, Schema } from "mongoose"; + +export interface IAvatar { + originalfilename?: string; + newfilename?: string; + picturepath?: string; + nickname?: string; +} + +const Avatarschema = new Schema({ + originalfilename: { type: String, unique: true }, + newfilename: { type: String }, + nickname: { type: String }, + picturepath: { type: String }, +}); + +export default model("Avatar", Avatarschema); diff --git a/src/models/index.ts b/src/models/index.ts index d0554aab37fd8273f348d78c134ba3812db4ee31..a5735f977ae40777721d887284cda3b78c3e7400 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -1,3 +1,4 @@ export { default as User, IUser } from "./user.model"; export { default as Post, PostType } from "./post.model"; export { default as Role } from "./role.model"; +export { default as Avatar, IAvatar } from "./fileinfo.model"; diff --git a/src/models/user.model.ts b/src/models/user.model.ts index 9d558da4d7962f7b5c1b52b9d1c2b4cf620bbc08..95181dad37f07891ad05dbb6611bc70713693e06 100644 --- a/src/models/user.model.ts +++ b/src/models/user.model.ts @@ -5,10 +5,7 @@ export interface IUser { name?: string; password: string; role?: Types.ObjectId; - originalfilename?: string; - newfilename?: string; - picturepath?: string; - nickname?: string; + avatar?: Types.ObjectId; } const validateEmail = (email: string) => { @@ -25,10 +22,7 @@ const schema = new Schema( validate: [validateEmail, "이메일을 입력해주세요"], }, name: { type: String }, - originalfilename: { type: String }, - newfilename: { type: String }, - nickname: { type: String }, - picturepath: { type: String }, + avatar: { type: Schema.Types.ObjectId, ref: "Avatar" }, password: { type: String, required: true, select: false }, role: { type: Schema.Types.ObjectId, ref: "Role" }, },