//DeleteCategoryFuncAPI will delete the category for the user func DeleteCategoryFuncAPI(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { categoryName := r.URL.Path[len("/delete-category/"):] categoryName = strings.Trim(categoryName, "/") var statusCode int var err error var message string var catErr bool token := r.Header["Token"][0] w.Header().Set("Content-Type", "application/json; charset=UTF-8") IsTokenValid, username := ValidateToken(token) //When the token is not valid show the default error JSON document if !IsTokenValid { status := types.Status{StatusCode: http.StatusInternalServerError, Message: message} w.WriteHeader(http.StatusInternalServerError) err = json.NewEncoder(w).Encode(status) if err != nil { panic(err) } return } log.Println("token is valid " + username + " is logged in") categoryName = strings.Trim(categoryName, " ") if categoryName != "" { catErr = true } err = db.DeleteCategoryByName(username, categoryName) if err != nil { catErr = true } else { catErr = false } if catErr { statusCode = http.StatusBadRequest message = "error deleting category" + categoryName } else { statusCode = http.StatusOK message = "deleted category " + categoryName } status := types.Status{StatusCode: statusCode, Message: message} w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusOK) err = json.NewEncoder(w).Encode(status) if err != nil { panic(err) } } }
//DeleteCategoryFunc will delete any category func DeleteCategoryFunc(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { categoryName := r.URL.Path[len("/del-category/"):] err := db.DeleteCategoryByName(categoryName) if err != nil { message = "error deleting category" } else { message = "Category " + categoryName + " deleted" } http.Redirect(w, r, "/", http.StatusFound) } }
//DeleteCategoryFunc will delete any category func DeleteCategoryFunc(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { http.Redirect(w, r, "/", http.StatusBadRequest) return } categoryName := r.URL.Path[len("/del-category/"):] username := sessions.GetCurrentUserName(r) err := db.DeleteCategoryByName(username, categoryName) if err != nil { message = "error deleting category" } else { message = "Category " + categoryName + " deleted" } http.Redirect(w, r, "/", http.StatusFound) }