Commit 8fbd9c22 authored by Kim, MinGyu's avatar Kim, MinGyu
Browse files

프로필 ref 변경

parent 4415469c
...@@ -36,10 +36,15 @@ export default function Profile() { ...@@ -36,10 +36,15 @@ export default function Profile() {
} }
}; };
const onNickChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const nickname = e.target.value;
setNickname(nickname);
};
const userChange = async () => { const userChange = async () => {
const profile = await handleProfile(); const profile = await handleProfile();
setEmail(profile.email); setEmail(profile.email);
setPicturename(profile.newfilename); setPicturename(profile.avatar.newfilename);
}; };
const handleClick = async ( const handleClick = async (
e: React.MouseEvent<HTMLButtonElement, globalThis.MouseEvent> e: React.MouseEvent<HTMLButtonElement, globalThis.MouseEvent>
...@@ -48,6 +53,7 @@ export default function Profile() { ...@@ -48,6 +53,7 @@ export default function Profile() {
if (!(file === undefined || file === null)) { if (!(file === undefined || file === null)) {
formdata.append("picture", file); formdata.append("picture", file);
} }
formdata.append("nickname", nickname);
await profileApi.picture(formdata); await profileApi.picture(formdata);
}; };
...@@ -80,7 +86,7 @@ export default function Profile() { ...@@ -80,7 +86,7 @@ export default function Profile() {
/> />
) : ( ) : (
<img <img
src={"http://localhost:3000/" + picturename} src={"http://localhost:3000/images/" + picturename}
width={200} width={200}
height={200} height={200}
/> />
...@@ -104,6 +110,7 @@ export default function Profile() { ...@@ -104,6 +110,7 @@ export default function Profile() {
<input <input
placeholder="빈칸" placeholder="빈칸"
className="basis-full placeholder:text-black my-10 ml-5" className="basis-full placeholder:text-black my-10 ml-5"
onChange={onNickChange}
/> />
</div> </div>
</div> </div>
......
...@@ -31,8 +31,10 @@ export interface SignupUser { ...@@ -31,8 +31,10 @@ export interface SignupUser {
export interface Profile { export interface Profile {
_id: string; _id: string;
email: string; email: string;
avatar: {
originalfilename: string; originalfilename: string;
newfilename: string; newfilename: string;
picturepath: string; picturepath: string;
nickname: string; nickname: string;
};
} }
...@@ -34,7 +34,7 @@ export const postPicture = asyncWrap(async (reqExp, res) => { ...@@ -34,7 +34,7 @@ export const postPicture = asyncWrap(async (reqExp, res) => {
const { userId } = req.auth; const { userId } = req.auth;
const form = formidable({ const form = formidable({
uploadDir: "./src/upload", uploadDir: "uploads",
keepExtensions: true, keepExtensions: true,
multiples: false, multiples: false,
}); });
...@@ -42,11 +42,34 @@ export const postPicture = asyncWrap(async (reqExp, res) => { ...@@ -42,11 +42,34 @@ export const postPicture = asyncWrap(async (reqExp, res) => {
form.parse(req, (err, fields, files) => { form.parse(req, (err, fields, files) => {
if (!Array.isArray(files.picture)) { 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 originalfilename = files.picture.originalFilename;
const newfilename = files.picture.newFilename; const newfilename = files.picture.newFilename;
const picturepath = files.picture.filepath; const picturepath = files.picture.filepath;
userDb.postPicture(userId, originalfilename, newfilename, picturepath); 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(); res.json();
......
import bcrypt from "bcryptjs"; import bcrypt from "bcryptjs";
import { ObjectId } from "mongoose"; 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) => { export const createUser = async (user: IUser) => {
// 비밀번호 암호화 // 비밀번호 암호화
const hash = await bcrypt.hash(user.password, 10); const hash = await bcrypt.hash(user.password, 10);
const newAvatar = await Avatar.create({});
// 사용자 역할 추가: 기본값은 "user" // 사용자 역할 추가: 기본값은 "user"
let userRole = null; let userRole = null;
if (user.role) { if (user.role) {
...@@ -17,6 +18,7 @@ export const createUser = async (user: IUser) => { ...@@ -17,6 +18,7 @@ export const createUser = async (user: IUser) => {
password: hash, password: hash,
role: userRole, role: userRole,
isNew: true, isNew: true,
avatar: newAvatar,
}); });
const retUser = await newUser.save(); const retUser = await newUser.save();
return retUser; return retUser;
...@@ -39,8 +41,9 @@ export const findUserByPostId = async (postId: string) => { ...@@ -39,8 +41,9 @@ export const findUserByPostId = async (postId: string) => {
const post = await Post.findOne({ _id: postId }).populate("user"); const post = await Post.findOne({ _id: postId }).populate("user");
return post?.user; return post?.user;
}; };
export const getProfile = async (userId: string) => { export const getProfile = async (userId: string) => {
const profile = await User.findById(userId); const profile = await User.findById(userId).populate("avatar");
return profile; //이름 수정 return profile; //이름 수정
}; };
...@@ -67,25 +70,20 @@ export const isValidUserId = async (userId: string) => { ...@@ -67,25 +70,20 @@ export const isValidUserId = async (userId: string) => {
} }
}; };
export const postPicture = ( export const postPicture = async (
userId: ObjectId, userId: ObjectId,
originalfilename: string | null, originalfilename: string | null,
newfilename: string, newfilename: string,
picturepath: string picturepath: string,
nickname: string
) => { ) => {
User.findByIdAndUpdate( const profile = await User.findById(userId);
userId, if (!(profile?.avatar === undefined)) {
{ await Avatar.findByIdAndUpdate(profile.avatar._id, {
originalfilename: originalfilename, originalfilename: originalfilename,
newfilename: newfilename, newfilename: newfilename,
picturepath: picturepath, picturepath: picturepath,
}, nickname: nickname,
function (err: any, docs: any) { });
if (err) {
console.log(err);
} else {
console.log("Updated User : ", docs);
}
} }
);
}; };
import { model, Schema } from "mongoose";
export interface IAvatar {
originalfilename?: string;
newfilename?: string;
picturepath?: string;
nickname?: string;
}
const Avatarschema = new Schema<IAvatar>({
originalfilename: { type: String, unique: true },
newfilename: { type: String },
nickname: { type: String },
picturepath: { type: String },
});
export default model<IAvatar>("Avatar", Avatarschema);
export { default as User, IUser } from "./user.model"; export { default as User, IUser } from "./user.model";
export { default as Post, PostType } from "./post.model"; export { default as Post, PostType } from "./post.model";
export { default as Role } from "./role.model"; export { default as Role } from "./role.model";
export { default as Avatar, IAvatar } from "./fileinfo.model";
...@@ -5,10 +5,7 @@ export interface IUser { ...@@ -5,10 +5,7 @@ export interface IUser {
name?: string; name?: string;
password: string; password: string;
role?: Types.ObjectId; role?: Types.ObjectId;
originalfilename?: string; avatar?: Types.ObjectId;
newfilename?: string;
picturepath?: string;
nickname?: string;
} }
const validateEmail = (email: string) => { const validateEmail = (email: string) => {
...@@ -25,10 +22,7 @@ const schema = new Schema<IUser>( ...@@ -25,10 +22,7 @@ const schema = new Schema<IUser>(
validate: [validateEmail, "이메일을 입력해주세요"], validate: [validateEmail, "이메일을 입력해주세요"],
}, },
name: { type: String }, name: { type: String },
originalfilename: { type: String }, avatar: { type: Schema.Types.ObjectId, ref: "Avatar" },
newfilename: { type: String },
nickname: { type: String },
picturepath: { type: String },
password: { type: String, required: true, select: false }, password: { type: String, required: true, select: false },
role: { type: Schema.Types.ObjectId, ref: "Role" }, role: { type: Schema.Types.ObjectId, ref: "Role" },
}, },
......
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