mirror of
https://github.com/linux-do/new-api.git
synced 2025-09-19 17:06:38 +08:00
修复不能删除注销用户的问题
This commit is contained in:
parent
14592f9758
commit
e8bcf60f0a
@ -647,7 +647,7 @@ func ManageUser(c *gin.Context) {
|
|||||||
Username: req.Username,
|
Username: req.Username,
|
||||||
}
|
}
|
||||||
// Fill attributes
|
// Fill attributes
|
||||||
model.DB.Where(&user).First(&user)
|
model.DB.Unscoped().Where(&user).First(&user)
|
||||||
if user.Id == 0 {
|
if user.Id == 0 {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"success": false,
|
"success": false,
|
||||||
@ -683,7 +683,7 @@ func ManageUser(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := user.Delete(); err != nil {
|
if err := user.HardDelete(); err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": err.Error(),
|
"message": err.Error(),
|
||||||
|
@ -37,7 +37,15 @@ type User struct {
|
|||||||
// CheckUserExistOrDeleted check if user exist or deleted, if not exist, return false, nil, if deleted or exist, return true, nil
|
// CheckUserExistOrDeleted check if user exist or deleted, if not exist, return false, nil, if deleted or exist, return true, nil
|
||||||
func CheckUserExistOrDeleted(username string, email string) (bool, error) {
|
func CheckUserExistOrDeleted(username string, email string) (bool, error) {
|
||||||
var user User
|
var user User
|
||||||
err := DB.Unscoped().First(&user, "username = ? or email = ?", username, email).Error
|
|
||||||
|
// err := DB.Unscoped().First(&user, "username = ? or email = ?", username, email).Error
|
||||||
|
// check email if empty
|
||||||
|
var err error
|
||||||
|
if email == "" {
|
||||||
|
err = DB.Unscoped().First(&user, "username = ?", username).Error
|
||||||
|
} else {
|
||||||
|
err = DB.Unscoped().First(&user, "username = ? or email = ?", username, email).Error
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
// not exist, return false, nil
|
// not exist, return false, nil
|
||||||
@ -208,6 +216,14 @@ func (user *User) Delete() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (user *User) HardDelete() error {
|
||||||
|
if user.Id == 0 {
|
||||||
|
return errors.New("id 为空!")
|
||||||
|
}
|
||||||
|
err := DB.Unscoped().Delete(user).Error
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// ValidateAndFill check password & user status
|
// ValidateAndFill check password & user status
|
||||||
func (user *User) ValidateAndFill() (err error) {
|
func (user *User) ValidateAndFill() (err error) {
|
||||||
// When querying with struct, GORM will only query with non-zero fields,
|
// When querying with struct, GORM will only query with non-zero fields,
|
||||||
|
Loading…
Reference in New Issue
Block a user