簡體   English   中英

使用 echo 和 gorm 部分更新結構

[英]Partial update a structure with echo and gorm

我有一個用戶界面,用戶 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"`
}

我有一個 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, "")
}

我可以測試每個字段的 newInfoUser 是否為空,如果不是則更新,但這將是復制代碼,我想以一般方式進行。

想要的行為:

有一個用戶

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

並用正文調用更新

{
    "username": "test2"
}

將其綁定到用戶結構將創建

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

我希望用戶更新

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

我發現https://willnorris.com/2014/05/go-rest-apis-and-pointers/顯示omitempty標簽

(我意識到我做錯了,不能綁定 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, "")
}

但我想知道為什么它不適用於 createAt 和 updateAt 字段

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM