chore(errors): create helper function for method not allowed Instead of writing same code multiple times for making method not allowed responses be in json, moved the logic to a helper function.
Brijesh ops@brijesh.dev
Thu, 04 Jul 2024 22:13:56 +0530
3 files changed,
35 insertions(+),
110 deletions(-)
M
internal/logs.go
→
internal/logs.go
@@ -7,14 +7,12 @@ "fmt"
"net/http" "time" "watchman/schema" + "watchman/utils" ) func BatchInsertLogs(w http.ResponseWriter, r *http.Request, db *sql.DB) { - if r.Method != http.MethodPost { - w.WriteHeader(http.StatusMethodNotAllowed) - fmt.Fprintf(w, "Method %s not allowed", r.Method) - return - } + utils.HandleMethodNotAllowed(w, r, http.MethodPost) + var logs []schema.Log decoder := json.NewDecoder(r.Body) err := decoder.Decode(&logs)@@ -52,11 +50,8 @@ }
} func GetLogs(w http.ResponseWriter, r *http.Request, db *sql.DB) { - if r.Method != http.MethodGet { - w.WriteHeader(http.StatusMethodNotAllowed) - fmt.Fprintf(w, "Method %s not allowed", r.Method) - return - } + utils.HandleMethodNotAllowed(w, r, http.MethodGet) + projectID := r.URL.Query().Get("project_id") userID := r.URL.Query().Get("user_id") startTime := r.URL.Query().Get("start_time")@@ -122,11 +117,8 @@ }
// DeleteLogs function similar to above GetLogs that takes in the same parameters and deletes the logs from the database func DeleteLogs(w http.ResponseWriter, r *http.Request, db *sql.DB) { - if r.Method != http.MethodDelete { - w.WriteHeader(http.StatusMethodNotAllowed) - fmt.Fprintf(w, "Method %s not allowed", r.Method) - return - } + utils.HandleMethodNotAllowed(w, r, http.MethodDelete) + projectID := r.URL.Query().Get("project_id") userID := r.URL.Query().Get("user_id") startTime := r.URL.Query().Get("start_time")
M
internal/projects.go
→
internal/projects.go
@@ -9,18 +9,8 @@ "watchman/schema"
"watchman/utils" ) -// CREATE: INSERT INTO Projects (ID, Name) VALUES (?, ?) -// READ: SELECT * FROM Projects -// READ WITH ID: SELECT * FROM Projects WHERE ID = ? -// UPDATE: UPDATE Projects SET Name = ? WHERE ID = ? -// DELETE: DELETE FROM Projects WHERE ID = ? - func CreateProject(w http.ResponseWriter, r *http.Request, db *sql.DB) { - if r.Method != http.MethodPost { - w.WriteHeader(http.StatusMethodNotAllowed) - fmt.Fprintf(w, "Method %s not allowed", r.Method) - return - } + utils.HandleMethodNotAllowed(w, r, http.MethodPost) var project schema.Project decoder := json.NewDecoder(r.Body)@@ -63,76 +53,9 @@ http.Error(w, err.Error(), http.StatusInternalServerError)
} } -// func ListProjects(w http.ResponseWriter, r *http.Request, db *sql.DB) { -// if r.Method != http.MethodGet { -// w.WriteHeader(http.StatusMethodNotAllowed) -// fmt.Fprintf(w, "Method %s not allowed", r.Method) -// return -// } -// -// projectID := r.URL.Query().Get("id") -// -// if projectID == "" { -// rows, err := db.Query("SELECT * FROM Projects") -// if err != nil { -// w.WriteHeader(http.StatusInternalServerError) -// fmt.Fprintf(w, "Error querying database: %v", err) -// return -// } -// defer rows.Close() -// var projects []schema.Project -// for rows.Next() { -// var project schema.Project -// err := rows.Scan(&project.ID, &project.Name) -// if err != nil { -// w.WriteHeader(http.StatusInternalServerError) -// fmt.Fprintf(w, "Error scanning row: %v", err) -// return -// } -// projects = append(projects, project) -// } -// response := schema.Response_Type{ -// Status: "OK", -// Message: "Projects retrieved successfully", -// RequestID: r.Context().Value(schema.RequestIDKey{}).(string), -// Data: projects, -// } -// w.Header().Set("Content-Type", "application/json") -// err = json.NewEncoder(w).Encode(response) -// if err != nil { -// http.Error(w, err.Error(), http.StatusInternalServerError) -// } -// } else { -// row := db.QueryRow("SELECT * FROM Projects WHERE ID = ?", projectID) -// var projectByID schema.Project -// err := row.Scan(&projectByID.ID, &projectByID.Name) -// if err != nil { -// w.WriteHeader(http.StatusInternalServerError) -// fmt.Fprintf(w, "Error querying database: %v", err) -// return -// } -// response := schema.Response_Type{ -// Status: "OK", -// Message: "Project retrieved successfully", -// RequestID: r.Context().Value(schema.RequestIDKey{}).(string), -// Data: projectByID, -// } -// w.Header().Set("Content-Type", "application/json") -// err = json.NewEncoder(w).Encode(response) -// if err != nil { -// http.Error(w, err.Error(), http.StatusInternalServerError) -// } -// } -// } - -// internal.GetProjectByID(w, r, db_connection, projectID) +func GetProjectByID(w http.ResponseWriter, r *http.Request, db *sql.DB, projectID string) { + utils.HandleMethodNotAllowed(w, r, http.MethodGet) -func GetProjectByID(w http.ResponseWriter, r *http.Request, db *sql.DB, projectID string) { - if r.Method != http.MethodGet { - w.WriteHeader(http.StatusMethodNotAllowed) - fmt.Fprintf(w, "Method %s not allowed", r.Method) - return - } row := db.QueryRow("SELECT * FROM Projects WHERE ID = ?", projectID) var projectByID schema.Project err := row.Scan(&projectByID.ID, &projectByID.Name)@@ -155,11 +78,8 @@ }
} func ListAllProjects(w http.ResponseWriter, r *http.Request, db *sql.DB) { - if r.Method != http.MethodGet { - w.WriteHeader(http.StatusMethodNotAllowed) - fmt.Fprintf(w, "Method %s not allowed", r.Method) - return - } + utils.HandleMethodNotAllowed(w, r, http.MethodGet) + rows, err := db.Query("SELECT * FROM Projects") if err != nil { w.WriteHeader(http.StatusInternalServerError)@@ -192,11 +112,7 @@ }
} func UpdateProjectByID(w http.ResponseWriter, r *http.Request, db *sql.DB, projectID string) { - if r.Method != http.MethodPut { - w.WriteHeader(http.StatusMethodNotAllowed) - fmt.Fprintf(w, "Method %s not allowed", r.Method) - return - } + utils.HandleMethodNotAllowed(w, r, http.MethodPut) var project schema.Project decoder := json.NewDecoder(r.Body)@@ -236,11 +152,7 @@ }
} func DeleteProjectByID(w http.ResponseWriter, r *http.Request, db *sql.DB, projectID string) { - if r.Method != http.MethodDelete { - w.WriteHeader(http.StatusMethodNotAllowed) - fmt.Fprintf(w, "Method %s not allowed", r.Method) - return - } + utils.HandleMethodNotAllowed(w, r, http.MethodDelete) stmt, err := db.Prepare("DELETE FROM Projects WHERE ID = ?") if err != nil {
A
utils/errors.go
@@ -0,0 +1,21 @@
+package utils + +import ( + "encoding/json" + "net/http" + "watchman/schema" +) + +func HandleMethodNotAllowed(w http.ResponseWriter, r *http.Request, method string) { + if r.Method != method { + response := schema.Response_Type{ + Status: "ERROR", + Message: "Method " + method + " not allowed", + RequestID: r.Context().Value(schema.RequestIDKey{}).(string), + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusMethodNotAllowed) + json.NewEncoder(w).Encode(response) + } +}