2
votes

Hi new in golang and trying to make rest API

I want to updating entity passing only needed datas with GORM and golang echo framework. And I get this error : "strconv.ParseUint: parsing \"ea78944c-a2a9-4813-86e7-10b199d0f002\": invalid syntax"

I use echo.Bind() func to bind formdata (I use postman) with my Club struct.

PS : I use xid to external id and keep my int id to interne work.

Expected :

I'm gonna find Club in my database by id (get with url param) with GORM and set it up in a new club struct.

After that, I bind my club struct with my formdata.

And finally save it with Save() GORM func. => get error

Reality :

I'm gonna find Club in my database by id (write hardcoding string id) with GORM and set it up in a new club struct.

After that, I bind my club struct with my formdata.

And finally save it with Save() GORM func. => works

Conclusion :

I don't know how to pass url parameter AND formdata in PUT method using echo.Bind()

Here my Club struct :

// Club struct
type Club struct {
    Base
    Name    string  `json:"name;" gorm:"type:varchar(255);not null" validate:"required"`
    Slug    string  `json:"slug;" gorm:"type:varchar(255);unique;not null"`
    Website string  `json:"website;" gorm:"type:varchar(255)"`
    Lat     float32 `json:"lat;" gorm:"lat;" sql:"type:decimal(8,6);" validate:"required,numeric"`
    Lng     float32 `json:"lng;" gorm:"lng;" sql:"type:decimal(9,6);" validate:"required,numeric"`
    Logo    string  `json:"logo;" gorm:"type:varchar(100)" validate:"required"`
    Phone   string  `json:"phone;" gorm:"type:varchar(20)" validate:"required"`
}

My FindById func :

// GetClubByXID get club by xid
func GetClubByXID(c echo.Context, xid string) (*schemas.Club, error) {
    club := new(schemas.Club)
    if result := db.Where("xid = ?", xid).First(&club); result.Error != nil {
        return nil, result.Error
    }
    return club, nil
}

And here my updating func :

func UpdateClub(c echo.Context) error {
    xid := c.Param("id") // => doesn't work
        // xid := "ea78944c-a2a9-4813-86e7-10b199d0f002" // => work

    club, err := models.GetClubByXID(c, xid)
    if err != nil {
        return c.JSON(http.StatusNotFound, err)
    }

    if err := c.Bind(club); err != nil {
        return c.JSON(http.StatusInternalServerError, err)
    }

    db.Save(&club)
    return c.JSON(http.StatusOK, echo.Map{
        "club": &club,
    })
}

My updating route :

API.PUT("/updateClub/:id", handlers.UpdateClub) // => doesn't work
// API.PUT("/updateClub", handlers.UpdateClub) // => work

When I write with hardcoding my xid ea78944c-a2a9-4813-86e7-10b199d0f002 in my updating func it's work like a charm but I can't combine my url and my formdata with echo.Bind()

my err if I try http://localhost:1323/api/updateClub/ea78944c-a2a9-4813-86e7-10b199d0f002:

"strconv.ParseUint: parsing \"ea78944c-a2a9-4813-86e7-10b199d0f002\": invalid syntax"

Thanks for reading hope someone can help me :)

1

1 Answers

2
votes

Can you try to c.Param("xid") instead of c.Param("id") and change the routing parameter as API.PUT("/updateClub/:xid", handlers.UpdateClub)