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
survey
Commits
3085c95e
Commit
3085c95e
authored
Jul 07, 2022
by
Yoon, Daeki
😅
Browse files
delete user 기능 추가
parent
960b511c
Changes
3
Show whitespace changes
Inline
Side-by-side
src/controllers/user.controller.ts
View file @
3085c95e
import
{
NextFunction
,
Request
,
Response
}
from
"
express
"
;
import
{
userDb
}
from
"
../db
"
;
import
{
asyncWrap
}
from
"
../helpers/asyncWrap
"
;
import
{
TypedRequestAuth
}
from
"
./auth.controller
"
;
export
const
getUsers
=
asyncWrap
(
async
(
req
,
res
)
=>
{
const
users
=
await
userDb
.
getUsers
();
return
res
.
json
(
users
);
...
...
@@ -10,5 +11,31 @@ export const createUser = asyncWrap(async (req, res) => {
const
user
=
req
.
body
;
console
.
log
(
"
user body
"
,
user
);
const
newUser
=
await
userDb
.
createUser
(
user
);
return
res
.
json
(
user
);
return
res
.
json
(
newUser
);
});
export
const
deleteUser
=
asyncWrap
(
async
(
req
,
res
)
=>
{
const
{
userId
}
=
req
.
params
;
console
.
log
(
"
user id:
"
,
userId
);
const
deletedUser
=
await
userDb
.
deleteUserById
(
userId
);
return
res
.
json
(
deletedUser
);
});
export
const
userById
=
async
(
reqExp
:
Request
,
res
:
Response
,
next
:
NextFunction
,
userId
:
string
)
=>
{
try
{
const
req
=
reqExp
as
TypedRequestAuth
<
{
userId
:
string
}
>
;
let
user
=
await
userDb
.
findUserById
(
userId
);
if
(
!
user
)
{
return
res
.
status
(
404
).
send
(
"
사용자를 찾을 수 없습니다
"
);
}
req
.
user
=
user
;
next
();
}
catch
(
error
:
any
)
{
return
res
.
status
(
500
).
send
(
error
.
message
||
"
사용자 찾기 중 오류
"
);
}
};
src/db/user.db.ts
View file @
3085c95e
import
bcrypt
from
"
bcryptjs
"
;
import
{
IUser
,
User
}
from
"
../models
"
;
export
const
createUser
=
async
(
user
:
IUser
)
=>
{
const
newUser
=
await
User
.
create
(
user
);
// 비밀번호 암호화
const
hash
=
await
bcrypt
.
hash
(
user
.
password
,
10
);
const
newUser
=
await
User
.
create
({
email
:
user
.
email
,
password
:
hash
});
return
newUser
;
};
export
const
deleteUserById
=
async
(
userId
:
string
)
=>
{
const
deletedUser
=
await
User
.
findByIdAndDelete
(
userId
);
return
deletedUser
;
};
export
const
findUserById
=
async
(
id
:
string
)
=>
{
const
user
=
await
User
.
findById
(
id
);
return
user
;
};
export
const
findUserByEmail
=
async
(
email
:
string
,
includePassword
:
boolean
=
false
...
...
src/routes/user.route.ts
View file @
3085c95e
...
...
@@ -8,4 +8,10 @@ router
.
get
(
authCtrl
.
requireLogin
,
userCtrl
.
getUsers
)
.
post
(
authCtrl
.
requireLogin
,
userCtrl
.
createUser
);
router
.
route
(
"
/:userId
"
)
.
delete
(
authCtrl
.
requireLogin
,
authCtrl
.
authenticate
,
userCtrl
.
deleteUser
);
router
.
param
(
"
userId
"
,
userCtrl
.
userById
);
export
default
router
;
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