Sessions: make loadpoint changeable (#19016)
This commit is contained in:
parent
dadd51040e
commit
28cfb4c6d1
3 changed files with 51 additions and 9 deletions
|
|
@ -117,20 +117,28 @@ func updateSessionHandler(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
id := mux.Vars(r)["id"]
|
||||
|
||||
var session struct {
|
||||
Vehicle string
|
||||
var data map[string]interface{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
jsonError(w, http.StatusBadRequest, errors.New("invalid JSON"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&session); err != nil {
|
||||
jsonError(w, http.StatusBadRequest, err)
|
||||
updates := map[string]interface{}{}
|
||||
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"))
|
||||
return
|
||||
}
|
||||
|
||||
// https://github.com/evcc-io/evcc/issues/13738#issuecomment-2094070362
|
||||
if txn := db.Instance.Table("sessions").Where("id = ?", id).Select("vehicle").Updates(&session); txn.Error != nil {
|
||||
if txn := db.Instance.Table("sessions").Where("id = ?", id).Updates(updates); txn.Error != nil {
|
||||
jsonError(w, http.StatusBadRequest, txn.Error)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue