From 0edab87bf69951c15d1a444b2b3f3bcc15ee661a Mon Sep 17 00:00:00 2001 From: Amolith Date: Thu, 27 Mar 2025 12:59:55 -0600 Subject: [PATCH] refactor(ws): rewrite if-else chain as a switch statement --- ws/ws.go | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/ws/ws.go b/ws/ws.go index c024f071ecb184bc31ce44a9d76d5675ea2eae25..bcf8bdf715138b5188714214b6541ad21f259e04 100644 --- a/ws/ws.go +++ b/ws/ws.go @@ -75,13 +75,27 @@ func (h Handler) NewHandler(w http.ResponseWriter, r *http.Request) { params := r.URL.Query() action := bmStrict.Sanitize(params.Get("action")) if r.Method == http.MethodGet { - if action == "" { + switch action { + case "": data := struct{ Version string }{Version: *h.Version} tmpl := template.Must(template.ParseFS(fs, "static/new.html.tmpl", "static/head.html.tmpl", "static/header.html.tmpl", "static/footer.html.tmpl")) if err := tmpl.Execute(w, data); err != nil { fmt.Println(err) } - } else if action != "delete" { + case "delete": + submittedID := params.Get("id") + if submittedID == "" { + w.WriteHeader(http.StatusBadRequest) + _, err := w.Write([]byte("No URL provided")) + if err != nil { + fmt.Println(err) + } + return + } + + project.Untrack(h.DbConn, h.Mu, submittedID) + http.Redirect(w, r, "/", http.StatusSeeOther) + default: submittedURL := bmStrict.Sanitize(params.Get("url")) if submittedURL == "" { w.WriteHeader(http.StatusBadRequest) @@ -151,19 +165,6 @@ func (h Handler) NewHandler(w http.ResponseWriter, r *http.Request) { if err := tmpl.Execute(w, data); err != nil { fmt.Println(err) } - } else if action == "delete" { - submittedID := params.Get("id") - if submittedID == "" { - w.WriteHeader(http.StatusBadRequest) - _, err := w.Write([]byte("No URL provided")) - if err != nil { - fmt.Println(err) - } - return - } - - project.Untrack(h.DbConn, h.Mu, submittedID) - http.Redirect(w, r, "/", http.StatusSeeOther) } }