refactor(ws): rewrite if-else chain as a switch statement

Amolith created

Change summary

ws/ws.go | 31 ++++++++++++++++---------------
1 file changed, 16 insertions(+), 15 deletions(-)

Detailed changes

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)
 		}
 	}