I'm trying to build a PATCH method for my REST API. What I can't understand is how can I take the name from a JSON raw field to use it as a parameter in the UPDATE query so the query can know which field to update because, unlike PUT, PATCH needs to update only one field and not the whole row in the DB. I'm using SQLite3
EDIT: Code:
func PatchServer(c echo.Context) error {
patchedServer := new(structs.Server)
requestID := c.Param("id")
if err := c.Bind(patchedServer); err != nil {
return err
}
if reflect.TypeOf(patchedServer.Name).Kind() != reflect.String || reflect.TypeOf(patchedServer.Components).Kind() != reflect.Int {
panic("Insert a string or an int")
} else {
sql := "UPDATE servers SET server_name = CASE WHEN ? IS NOT NULL THEN ? END WHERE id = ?"
stmt, err := db.Get().Prepare(sql)
if err != nil {
panic(err)
}
_, err2 := stmt.Exec(patchedServer.Name, patchedServer.Name, requestID)
if err2 != nil {
panic(err2)
}
fmt.Println(patchedServer.ID, patchedServer.Name, patchedServer.Components)
fmt.Println("Requested id: ", requestID)
return c.JSON(http.StatusOK, "Patched!")
}