chore: simplify unmarshaling (#25010)
This commit is contained in:
parent
4a275b4bb1
commit
d6e304f1b9
1 changed files with 8 additions and 12 deletions
|
|
@ -119,26 +119,22 @@ func updateSessionHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
id := mux.Vars(r)["id"]
|
||||
|
||||
var data map[string]any
|
||||
var data struct {
|
||||
Vehicle, Loadpoint *string
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
jsonError(w, http.StatusBadRequest, errors.New("invalid JSON"))
|
||||
jsonError(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
updates := map[string]any{}
|
||||
for _, field := range []string{"vehicle", "loadpoint"} {
|
||||
if val, ok := data[field]; ok {
|
||||
updates[field] = val
|
||||
}
|
||||
}
|
||||
|
||||
if len(updates) == 0 {
|
||||
jsonError(w, http.StatusBadRequest, errors.New("no valid fields to update"))
|
||||
if data.Vehicle == nil && data.Loadpoint == nil {
|
||||
jsonError(w, http.StatusBadRequest, errors.New("nothing to update"))
|
||||
return
|
||||
}
|
||||
|
||||
// https://github.com/evcc-io/evcc/issues/13738#issuecomment-2094070362
|
||||
if txn := db.Instance.Table("sessions").Where("id = ?", id).Updates(updates); txn.Error != nil {
|
||||
if txn := db.Instance.Table("sessions").Where("id = ?", id).Updates(data); txn.Error != nil {
|
||||
jsonError(w, http.StatusBadRequest, txn.Error)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue