Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
students
travel
Commits
c016fafd
Commit
c016fafd
authored
Jul 26, 2022
by
Yoon, Daeki
😅
Browse files
서버 프로필 업데이트 로직 수정 변경
parent
add2d2b3
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/controllers/user.controller.ts
View file @
c016fafd
...
...
@@ -30,34 +30,36 @@ export const getProfile = asyncWrap(async (reqExp, res) => {
res
.
json
(
profile
);
});
export
const
postPictur
e
=
asyncWrap
(
async
(
reqExp
,
res
)
=>
{
export
const
updateProfil
e
=
asyncWrap
(
async
(
reqExp
,
res
)
=>
{
const
req
=
reqExp
as
TypedRequest
;
const
{
userId
}
=
req
.
auth
;
const
field
=
req
.
body
;
const
file
=
req
.
files
;
const
{
name
}
=
req
.
body
;
const
{
avatar
}:
{
avatar
:
formidable
.
File
}
=
req
.
files
;
if
(
!
Array
.
isArray
(
file
.
picture
))
{
//파일 좁히기 중
if
(
!
Array
.
isArray
(
field
.
nickname
))
{
const
nickname
=
field
.
nickname
;
if
(
!
(
file
.
picture
===
undefined
))
{
const
originalfilename
=
file
.
picture
.
originalFilename
;
const
newfilename
=
file
.
picture
.
newFilename
;
const
picturepath
=
file
.
picture
.
filepath
;
userDb
.
postPicture
(
userId
,
nickname
,
originalfilename
,
newfilename
,
picturepath
);
}
else
{
userDb
.
postPicture
(
userId
,
nickname
);
}
}
}
const
user
=
await
userDb
.
updateProfile
(
userId
,
name
,
avatar
);
// if (!Array.isArray(file.avatar)) {
// //파일 좁히기 중
// if (!Array.isArray(field.nickname)) {
// const nickname = field.nickname;
// if (!(file.avatar === undefined)) {
// const originalfilename = file.avatar.originalFilename;
// const newfilename = file.avatar.newFilename;
// const picturepath = file.avatar.filepath;
// userDb.updateProfile(
// userId,
// nickname,
// originalfilename,
// newfilename,
// picturepath
// );
// } else {
// userDb.updateProfile(userId, nickname);
// }
// }
// }
res
.
json
();
res
.
json
(
user
);
});
export
const
deleteUser
=
asyncWrap
(
async
(
reqExp
,
res
)
=>
{
...
...
src/db/user.db.ts
View file @
c016fafd
import
bcrypt
from
"
bcryptjs
"
;
import
{
ObjectId
}
from
"
mongoose
"
;
import
{
IUser
,
Role
,
Post
,
User
,
FileInfo
,
IRole
}
from
"
../models
"
;
import
{
HydratedDocument
,
ObjectId
}
from
"
mongoose
"
;
import
{
IUser
,
Role
,
Post
,
User
,
FileInfo
,
IRole
,
IFileInfo
}
from
"
../models
"
;
import
fs
from
"
fs/promises
"
;
import
formidable
from
"
formidable
"
;
export
const
createUser
=
async
(
user
:
IUser
)
=>
{
// 비밀번호 암호화
...
...
@@ -16,6 +17,7 @@ export const createUser = async (user: IUser) => {
}
const
newUser
=
new
User
({
email
:
user
.
email
,
name
:
user
.
name
,
password
:
hash
,
role
:
userRole
,
isNew
:
true
,
...
...
@@ -46,7 +48,7 @@ export const findUserByPostId = async (postId: string) => {
};
export
const
getProfile
=
async
(
userId
:
string
)
=>
{
const
profile
=
await
User
.
findById
(
userId
).
populate
(
"
fileInfo
"
);
const
profile
=
await
User
.
findById
(
userId
).
populate
(
"
avatar
"
);
return
profile
;
//이름 수정
};
...
...
@@ -73,48 +75,87 @@ export const isValidUserId = async (userId: string) => {
}
};
export
const
postPictur
e
=
async
(
export
const
updateProfil
e
=
async
(
userId
:
ObjectId
,
nickname
:
string
,
originalfilename
?:
string
|
null
,
newfilename
?:
string
,
picturepath
?:
string
name
:
string
,
avatar
:
formidable
.
File
)
=>
{
const
profile
=
await
User
.
findById
(
userId
);
if
(
!
(
profile
?.
fileInfo
===
undefined
))
{
if
(
originalfilename
===
null
)
{
await
FileInfo
.
findByIdAndUpdate
(
profile
.
fileInfo
.
_id
,
{
nickname
:
nickname
,
});
}
else
if
(
nickname
===
""
)
{
const
ref
=
FileInfo
.
findById
(
profile
.
fileInfo
.
_id
);
console
.
log
(
ref
);
await
FileInfo
.
findByIdAndUpdate
(
profile
.
fileInfo
.
_id
,
{
originalfilename
:
originalfilename
,
newfilename
:
newfilename
,
picturepath
:
picturepath
,
const
user
=
await
User
.
findById
(
userId
).
populate
<
{
avatar
:
IFileInfo
}
>
(
"
avatar
"
);
console
.
log
(
"
user in update profile
"
,
user
,
avatar
);
if
(
!
user
)
{
throw
new
Error
(
"
사용자가 존재하지 않습니다
"
);
}
if
(
avatar
)
{
if
(
!
user
.
avatar
)
{
// 사용자의 아바타가 존재하지 않으면 생성
const
file
=
await
FileInfo
.
create
({
originalfilename
:
avatar
.
originalFilename
,
newfilename
:
avatar
.
newFilename
,
picturepath
:
avatar
.
filepath
,
});
user
.
avatar
=
file
;
}
else
{
const
ref
=
await
FileInfo
.
findByIdAndUpdate
(
profile
.
fileInfo
.
_id
,
{
originalfilename
:
originalfilename
,
newfilename
:
newfilename
,
picturepath
:
picturepath
,
nickname
:
nickname
,
});
// 아바타에 같은 원래파일이름이 존재하는지 확인
// 같으면 파일 수정하지 않고 통과
if
(
avatar
.
originalFilename
&&
user
.
avatar
.
originalfilename
!==
avatar
.
originalFilename
)
{
// 같지 않으면 기존의 파일을 디스크에서 삭제한 후
try
{
await
fs
.
unlink
(
user
.
avatar
.
picturepath
);
}
catch
(
error
)
{
console
.
log
(
"
error
"
,
error
);
}
const
userAvatar
=
user
.
avatar
as
HydratedDocument
<
IFileInfo
>
;
// 기존 아바타 fileinfo의 파일이름과 경로 변경 설정
userAvatar
.
originalfilename
=
avatar
.
originalFilename
;
userAvatar
.
newfilename
=
avatar
.
newFilename
;
userAvatar
.
picturepath
=
avatar
.
filepath
;
await
userAvatar
.
save
();
}
}
}
user
.
name
=
name
;
await
user
.
save
();
console
.
log
(
"
user updated
"
,
user
);
return
user
;
// if (!(profile?.avatar === undefined)) {
// if (originalfilename === null) {
// await FileInfo.findByIdAndUpdate(profile.avatar._id, {
// nickname: nickname,
// });
// } else if (nickname === "") {
// const ref = FileInfo.findById(profile.avatar._id);
// console.log(ref);
// await FileInfo.findByIdAndUpdate(profile.avatar._id, {
// originalfilename: originalfilename,
// newfilename: newfilename,
// picturepath: picturepath,
// });
// } else {
// const ref = await FileInfo.findByIdAndUpdate(profile.avatar._id, {
// originalfilename: originalfilename,
// newfilename: newfilename,
// picturepath: picturepath,
// nickname: nickname,
// });
// }
// }
};
export
const
deleteUser
=
async
(
userId
:
string
)
=>
{
const
user
=
await
User
.
findById
(
userId
);
if
(
!
(
user
?.
fileInfo
===
undefined
))
{
const
ref
=
await
FileInfo
.
findById
(
user
.
fileInfo
.
_id
);
if
(
!
(
user
?.
avatar
===
undefined
))
{
const
ref
=
await
FileInfo
.
findById
(
user
.
avatar
.
_id
);
if
(
!
(
ref
?.
newfilename
===
undefined
))
{
await
fs
.
unlink
(
"
../travel/uploads/
"
+
ref
?.
newfilename
);
}
await
FileInfo
.
deleteOne
({
_id
:
user
.
fileInfo
.
_id
});
await
FileInfo
.
deleteOne
({
_id
:
user
.
avatar
.
_id
});
const
finish
=
await
User
.
deleteOne
({
_id
:
userId
});
return
finish
;
}
...
...
src/models/user.model.ts
View file @
c016fafd
...
...
@@ -5,7 +5,7 @@ export interface IUser {
name
?:
string
;
password
:
string
;
role
?:
Types
.
ObjectId
;
fileInfo
?:
Types
.
ObjectId
;
avatar
?:
Types
.
ObjectId
;
}
const
validateEmail
=
(
email
:
string
)
=>
{
...
...
@@ -22,7 +22,7 @@ const schema = new Schema<IUser>(
validate
:
[
validateEmail
,
"
이메일을 입력해주세요
"
],
},
name
:
{
type
:
String
},
fileInfo
:
{
type
:
Schema
.
Types
.
ObjectId
,
ref
:
"
FileInfo
"
},
avatar
:
{
type
:
Schema
.
Types
.
ObjectId
,
ref
:
"
FileInfo
"
},
password
:
{
type
:
String
,
required
:
true
,
select
:
false
},
role
:
{
type
:
Schema
.
Types
.
ObjectId
,
ref
:
"
Role
"
},
},
...
...
src/routes/profile.route.ts
View file @
c016fafd
...
...
@@ -6,7 +6,7 @@ const router = express.Router();
router
.
route
(
"
/
"
)
.
get
(
authCtrl
.
requireLogin
,
userCtrl
.
getProfile
)
.
post
(
authCtrl
.
requireLogin
,
fileInfoCtrl
.
uploadFile
,
userCtrl
.
postPictur
e
);
//중간에 req쪽에 fields와 files 넣는 미들웨어 추가
.
post
(
authCtrl
.
requireLogin
,
fileInfoCtrl
.
uploadFile
,
userCtrl
.
updateProfil
e
);
//중간에 req쪽에 fields와 files 넣는 미들웨어 추가
router
.
route
(
"
/delete
"
).
delete
(
authCtrl
.
requireLogin
,
userCtrl
.
deleteUser
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment