Skip to content

Commit

Permalink
Rewrite DeleteSubscriber Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Giurgiu committed Oct 2, 2024
1 parent 1b89a27 commit 323c9b3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
20 changes: 5 additions & 15 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1257,34 +1257,24 @@ func (app *App) DeleteSubscriber(w http.ResponseWriter, r *http.Request) {
return
}

// Extract the subscriber ID from the URL path
vars := mux.Vars(r)
subscriberID, err := strconv.Atoi(vars["id"])
subscriberID, err := strconv.Atoi(mux.Vars(r)["id"])
if err != nil {
http.Error(w, "Invalid subscriber ID", http.StatusBadRequest)
return
}

// Query to delete the subscriber
deleteQuery := `
DELETE FROM subscribers
WHERE id = ?
`

// Execute the query to delete the subscriber
result, err := app.DB.Exec(deleteQuery, subscriberID)
result, err := app.DB.Exec(`DELETE FROM subscribers WHERE id = ?`, subscriberID)
if err != nil {
app.Logger.Printf("Failed to delete subscriber: %v", err)
http.Error(w, fmt.Sprintf("Failed to delete subscriber: %v", err), http.StatusInternalServerError)
http.Error(w, "Failed to delete subscriber", http.StatusInternalServerError)
return
}

// Check if any row was actually deleted
rowsAffected, _ := result.RowsAffected()
if rowsAffected == 0 {
http.Error(w, "Subscriber not found", http.StatusNotFound)
return
}

fmt.Fprintf(w, "Subscriber deleted successfully")
fmt.Fprintln(w, "Subscriber deleted successfully")
}

1 change: 1 addition & 0 deletions api/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3444,3 +3444,4 @@ func TestUpdateSubscriber_FailedToRetrieveAffectedRows(t *testing.T) {




0 comments on commit 323c9b3

Please sign in to comment.