More management tools
Some checks failed
CI / Test (1.22) (push) Failing after -30m28s
CI / Lint (push) Failing after -30m32s
CI / Build (push) Failing after -30m31s
CI / Test (1.23) (push) Failing after -30m31s

This commit is contained in:
2026-03-04 22:30:40 +02:00
parent 4a716bb82d
commit 4b44340c58
25 changed files with 3094 additions and 230 deletions

View File

@@ -536,25 +536,41 @@ func handleQueryUpdate(w http.ResponseWriter, r *http.Request, db *bun.DB, req Q
return
}
// Convert data map to model
dataJSON, err := json.Marshal(req.Data)
if err != nil {
http.Error(w, "Invalid data", http.StatusBadRequest)
if req.Data == nil || len(req.Data) == 0 {
http.Error(w, "No update data provided", http.StatusBadRequest)
return
}
if err := json.Unmarshal(dataJSON, model); err != nil {
http.Error(w, "Invalid data format", http.StatusBadRequest)
updateQuery := db.NewUpdate().Model(model).Where("id = ?", req.ID)
updatedColumns := 0
for column, value := range req.Data {
// Protect immutable/audit columns from accidental overwrite.
if column == "id" || column == "created_at" {
continue
}
updateQuery = updateQuery.Set("? = ?", bun.Ident(column), value)
updatedColumns++
}
if updatedColumns == 0 {
http.Error(w, "No mutable fields to update", http.StatusBadRequest)
return
}
// Update in database
_, err = db.NewUpdate().Model(model).Where("id = ?", req.ID).Exec(r.Context())
// Update only the provided fields.
_, err := updateQuery.Exec(r.Context())
if err != nil {
http.Error(w, fmt.Sprintf("Update failed: %v", err), http.StatusInternalServerError)
return
}
// Return the latest database row after update.
if err := db.NewSelect().Model(model).Where("id = ?", req.ID).Scan(r.Context()); err != nil {
http.Error(w, fmt.Sprintf("Update succeeded but reload failed: %v", err), http.StatusInternalServerError)
return
}
writeJSON(w, http.StatusOK, model)
}