0

我有以下代码。

处理程序

func (authHandler *AuthHandler) Login(c *gin.Context) {
    var user models.User

    c.Bind(&user)

    if &user == nil {
        c.BindJSON(&user)
    }

    userObject, err := authHandler.userRepo.FindBy(
        models.User{
            Email: user.Email,
        },
    )

    if err != nil {
        c.JSON(401, gin.H{"_message": "User not found."})
        return
    }

    passErr := bcrypt.CompareHashAndPassword([]byte(userObject.Password), []byte(user.Password))

    if passErr != nil {
        c.JSON(401, gin.H{"_message": "Password incorrect."})
        return
    }

    token, err := services.CreateToken(userObject)

    if err != nil {
        c.JSON(401, gin.H{"_message": "Password incorrect."})
        return
    }

    c.JSON(200, gin.H{"token": token, "user": gin.H{
        "id":         &userObject.ID,
        "first_name": &userObject.FirstName,
        "last_name":  &userObject.LastName,
        "email":      &userObject.Email,
        "avatar":     &userObject.Avatar,
        "location":   &userObject.Location,
        "bg_img":     &userObject.BgImg,
    }})
}

模型

// user.go
type User struct {
    ID        string `gorm:"primary_key:true"`
    FirstName string `form:"first_name" json:"first_name,omitempty"`
    LastName  string `form:"last_name" json:"last_name,omitempty"`
    Password  string `form:"password" json:"password" bindind:"required"`
    Email     string `gorm:"type:varchar(110);unique_index" form:"email" json:"email,omitempty" binding:"required"`
    Location  string `form:"location" json:"location,omitempty"`
    Avatar    string `form:"avatar" json:"avatar,omitempty"`
    BgImg     string `form:"bg_img" json:"bg_img,omitempty"`
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt time.Time
}

func (repo *UserRepo) FindBy(params User) (User, error) {

    var user User

    err := repo.db.First(&user, "email = ?", params.Email).Error

    fmt.Println(err)

    if err != nil {
        return user, err
    }

    return user, nil
}

我试过用几种不同的方式进行查找,但没有运气。我每次都会收到 401 响应,并且模型中的 fmt.Prinlnt(err) 显示record not found错误。

4

2 回答 2

0

所以事实证明我需要将该DeletedAt字段设置为空值。在底层,GORM 会自动检查一个DeletedAt值。即SELECT * FROM users WHERE email = 'someone@gmail.com' AND deleted_at IS NULL。但是,我的DeletedAt字段被自动设置为空白日期,这在技术上不是NULL.

我添加了一个结构...

type NullTime struct {
  time.Time
  Valid bool
}

然后更新了我的模型...

type User struct {
    ID        string `gorm:"primary_key:true"`
    FirstName string `form:"first_name" json:"first_name,omitempty"`
    LastName  string `form:"last_name" json:"last_name,omitempty"`
    Password  string `form:"password" json:"password" bindind:"required"`
    Email     string `gorm:"type:varchar(110);unique_index" form:"email" json:"email,omitempty" binding:"required"`
    Location  string `form:"location" json:"location,omitempty"`
    Avatar    string `form:"avatar" json:"avatar,omitempty"`
    BgImg     string `form:"bg_img" json:"bg_img,omitempty"`
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt NullTime
}
于 2016-04-11T12:48:37.170 回答
0

'gorm' 中的约定是将基本模型包含在您的结构中。

// Base Model definition from gomodel.go
type Model struct {
  ID        uint `gorm:"primary_key"`
  CreatedAt time.Time
  UpdatedAt time.Time
  DeletedAt *time.Time
}

// Your struct
type User struct {
  gorm.Model
  Name string
}

如您所见,'gorm' 需要一个指针来处理 NULL 值。

于 2016-04-18T18:22:26.007 回答