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
parent
2aeb66de33
commit
444441861b
|
|
@ -146,10 +146,21 @@ func (h *Handlers) ModelUpdate(w http.ResponseWriter, r *http.Request) {
|
||||||
func (h *Handlers) ModelDelete(w http.ResponseWriter, r *http.Request) {
|
func (h *Handlers) ModelDelete(w http.ResponseWriter, r *http.Request) {
|
||||||
idStr := r.PathValue("id")
|
idStr := r.PathValue("id")
|
||||||
id, _ := strconv.ParseInt(idStr, 10, 64)
|
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)
|
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 {
|
func saveUploadedFile(r *http.Request, field, dir string) string {
|
||||||
file, header, err := r.FormFile(field)
|
file, header, err := r.FormFile(field)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue