简体   繁体   English

使用 echo 和 gorm 部分更新结构

[英]Partial update a structure with echo and gorm

I got a user interface, user model and user id structures我有一个用户界面,用户 model 和用户 ID 结构

type UserId struct {
    Id int64 `param:"id" json:"id"`
}

type User struct {
    Email     string    `json:"email"`
    Password  string    `json:"password"`
    Username  string    `json:"username"`
    CreatedAt time.Time `json:"created_at"`
    UpdatedAt time.Time `json:"updated_at"`
}

type User struct {
    gorm.Model
    ID       int64  `gorm:"primary_key"`
    Email    string `gorm:"type:varchar(128);unique;not null"`
    Username string `gorm:"type:varchar(64);unique;not null"`
    Password string `gorm:"type:varchar(64);not null"`
}

I'v got a function to update a user我有一个 function 来更新用户

func updateUser(c echo.Context) error {
    cc := c.(*myproject.ConfigContext)

    userId := new(interfaces.UserId)
    err := cc.Bind(userId)
    if err != nil {
        return err
    }

    newInfoUser := new(interfaces.User)
    err = cc.Bind(newInfoUser)
    if err != nil {
        return err
    }

    db, err := cc.ConnectDB()
    if err != nil {
        return err
    }
    err = db.AutoMigrate(&models.User{})
    if err != nil {
        return err
    }

    dbUser := new(models.User)
    r := db.First(dbUser, userId.Id)
    if r.Error != nil {
        return cc.NoContent(http.StatusNotFound)
    }

    // the partial update

    return cc.JSON(200, "")
}

I could test if newInfoUser is empty for each fields and update if it not but that will be replicate code and I would like to do it in a general way.我可以测试每个字段的 newInfoUser 是否为空,如果不是则更新,但这将是复制代码,我想以一般方式进行。

behavior wanted:想要的行为:

got a user有一个用户

{
    "username": "test", 
    "email": "test@gmail.com",
    "password": "password"
}

and call update with a body并用正文调用更新

{
    "username": "test2"
}

bind it to user structure will create将其绑定到用户结构将创建

{
    "username": "test2",
    "email": "",
    "password": ""
}

and I would like user to be updated in我希望用户更新

{
    "username": "test2",
    "email": "test@gmail.com",
    "password": "password"
}

I find https://willnorris.com/2014/05/go-rest-apis-and-pointers/ which show the omitempty tag我发现https://willnorris.com/2014/05/go-rest-apis-and-pointers/显示omitempty标签

(I realized I did a mistake, can't bind 2 times the same context) (我意识到我做错了,不能绑定 2 次相同的上下文)

type UserId struct {
    Id int64 `param:"id" json:"id"`
}

type User struct {
    Email     string    `json:"email,omitempty"`
    Password  string    `json:"password,omitempty"`
    Username  string    `json:"username,omitempty"`
    // CreatedAt time.Time `json:"created_at,omitempty"`
    // UpdatedAt time.Time `json:"updated_at,omitempty"`
}

type User struct {
    gorm.Model
    ID       int64  `gorm:"primary_key"`
    Email    string `gorm:"type:varchar(128);unique;not null"`
    Username string `gorm:"type:varchar(64);unique;not null"`
    Password string `gorm:"type:varchar(64);not null"`
}
func updateUser(c echo.Context) error {
    cc := c.(*myproject.ConfigContext)
    var err error

    userId := new(interfaces.UserId)
    userId.Id, err = strconv.ParseInt(cc.Param("id"), 10, 64)
    if err != nil {
        return err
    }

    newInfoUser := new(interfaces.User)
    err = cc.Bind(newInfoUser)
    if err != nil {
        return err
    }

    db, err := cc.ConnectDB()
    if err != nil {
        return err
    }
    err = db.AutoMigrate(&models.User{})
    if err != nil {
        return err
    }

    r := db.Model(&models.User{}).Where("id = ?", userId.Id).Updates(newInfoUser)
    if r.Error != nil {
        return cc.NoContent(http.StatusNotFound)
    }

    return cc.JSON(200, "")
}

but I wonder why it is not working with createAt and updateAt fields但我想知道为什么它不适用于 createAt 和 updateAt 字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM