付智勇

删除用户

... ... @@ -103,4 +103,17 @@ userController.prototype.updateUserByUserId = async(ctx, next) =>{
}
}
userController.prototype.delUserByUserId = async(ctx,next) => {
const userId = ctx.params.userId;
if(!userId){
return status.paramError('userId');
}
try {
let deluser = userService.delUserByUserId(userId);
return deluser
} catch (error) {
throw new Error(error)
}
}
module.exports = new userController();
\ No newline at end of file
... ...
... ... @@ -41,6 +41,16 @@ router.put('/updateUserByUserId/:userId',async(ctx, next)=>{
}
})
router.delete('/delUserByUserId/:userId',async(ctx, next)=>{
try{
let delData = await userContoller.delUserByUserId(ctx, next);
status.successTemp(ctx,200,delData);
}catch(e){
console.log(e)
status.catchError(ctx,400,e.message);
}
})
... ...
... ... @@ -82,5 +82,23 @@ userService.prototype.updateUserByUserId = async(userId,data)=>{
}
}
userService.prototype.delUserByUserId = async(userId) => {
let t = await sequelize.transaction({ autocommit: true })
try {
let User = await userModel.find({where:{id:userId}},{transaction: t});
if(!User){
t.rollback()
return {msg:'用户不存在'}
}
let delUser = await userModel.destroy({where:{id:userId}},{transaction: t});
await t.commit();
return delUser
} catch (error) {
t.rollback();
throw new Error(error);
}
}
module.exports = new userService();
\ No newline at end of file
... ...
... ... @@ -31,7 +31,7 @@ module.exports = {
successTemp: (ctx,code,data)=>{
ctx.response.status= 200;
ctx.response.body= {
code:data.code ? data.code:200,
code:!data.code ? 200:data.code,
returnData:{
data:data
}
... ...