我正试图通过电子邮件找到一条记录

时间:2016-04-10 17:06:07

标签: postgresql go go-gorm

我有以下代码。

处理程序

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(错误)显示record not found错误。

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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值的指针。