Show error when model deletion fails

ModelDelete now checks the DB error (e.g. FK constraint from devices still
using the model) and renders the model list with an alert instead of
silently redirecting with no feedback.
master
Joca 2026-06-09 18:56:40 -03:00
parent 2aeb66de33
commit 444441861b
Signed by: jocadbz
GPG Key ID: B1836DCE2F50BDF7
1 changed files with 12 additions and 1 deletions

View File

@ -146,10 +146,21 @@ func (h *Handlers) ModelUpdate(w http.ResponseWriter, r *http.Request) {
func (h *Handlers) ModelDelete(w http.ResponseWriter, r *http.Request) {
idStr := r.PathValue("id")
id, _ := strconv.ParseInt(idStr, 10, 64)
h.Store.ModelDelete(id)
if err := h.Store.ModelDelete(id); err != nil {
h.renderModelListError(w, "Cannot delete model: it is in use by existing devices.")
return
}
http.Redirect(w, r, "/models", http.StatusSeeOther)
}
func (h *Handlers) renderModelListError(w http.ResponseWriter, msg string) {
list, _ := h.Store.ModelGetAll()
if list == nil {
list = []models.DeviceModel{}
}
h.render(w, "model_list.html", ModelListData{Models: list, Error: msg})
}
func saveUploadedFile(r *http.Request, field, dir string) string {
file, header, err := r.FormFile(field)
if err != nil {