refactor: user remove is ready for console

This commit is contained in:
RockYang
2023-06-20 07:08:37 +08:00
parent 65a01f4776
commit d244ad9983
5 changed files with 79 additions and 34 deletions

View File

@@ -65,15 +65,16 @@ func (h *UserHandler) Update(c *gin.Context) {
resp.ERROR(c, types.InvalidArgs)
return
}
var user = model.User{
Nickname: data.Nickname,
Calls: data.Calls,
Status: data.Status,
ChatRoles: utils.JsonEncode(data.ChatRoles),
ExpiredTime: utils.Str2stamp(data.ExpiredTime),
}
var user = model.User{}
user.Id = data.Id
res := h.db.Updates(&user)
// 此处需要用 map 更新,用结构体无法更新 0 值
res := h.db.Model(&user).Updates(map[string]interface{}{
"nickname": data.Nickname,
"calls": data.Calls,
"status": data.Status,
"chat_roles_json": utils.JsonEncode(data.ChatRoles),
"expired_time": utils.Str2stamp(data.ExpiredTime),
})
if res.Error != nil {
resp.ERROR(c, "更新数据库失败")
return
@@ -82,6 +83,41 @@ func (h *UserHandler) Update(c *gin.Context) {
resp.SUCCESS(c)
}
func (h *UserHandler) Remove(c *gin.Context) {
id := h.GetInt(c, "id", 0)
if id > 0 {
tx := h.db.Begin()
res := h.db.Where("id = ?", id).Delete(&model.User{})
if res.Error != nil {
resp.ERROR(c, "删除失败")
return
}
// 删除聊天记录
res = h.db.Where("user_id = ?", id).Delete(&model.ChatItem{})
if res.Error != nil {
tx.Rollback()
resp.ERROR(c, "删除失败")
return
}
// 删除聊天历史记录
res = h.db.Where("user_id = ?", id).Delete(&model.HistoryMessage{})
if res.Error != nil {
tx.Rollback()
resp.ERROR(c, "删除失败")
return
}
// 删除登录日志
res = h.db.Where("user_id = ?", id).Delete(&model.UserLoginLog{})
if res.Error != nil {
tx.Rollback()
resp.ERROR(c, "删除失败")
return
}
tx.Commit()
}
resp.SUCCESS(c)
}
func (h *UserHandler) InitUser(c *gin.Context) {
var users []model.User
h.db.Find(&users)