Skip to content

Commit

Permalink
Rewrite DeleteAuthor Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Giurgiu committed Oct 2, 2024
1 parent b956529 commit 0659ff7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 42 deletions.
26 changes: 13 additions & 13 deletions api/coverage.out
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
mode: set
mymodule/auth.go:39.59,43.2 3 0
mymodule/auth.go:46.58,51.2 4 0
mymodule/auth.go:67.68,72.16 3 0
mymodule/auth.go:72.16,77.3 4 0
mymodule/auth.go:79.2,84.69 4 0
mymodule/auth.go:84.69,89.3 4 0
mymodule/auth.go:92.2,95.40 4 0
mymodule/auth.go:95.40,100.3 4 0
mymodule/auth.go:102.2,102.25 1 0
mymodule/auth.go:102.25,106.3 3 0
mymodule/auth.go:109.2,110.16 2 0
mymodule/auth.go:39.59,43.2 3 1
mymodule/auth.go:46.58,51.2 4 1
mymodule/auth.go:67.68,72.16 3 1
mymodule/auth.go:72.16,77.3 4 1
mymodule/auth.go:79.2,84.69 4 1
mymodule/auth.go:84.69,89.3 4 1
mymodule/auth.go:92.2,95.40 4 1
mymodule/auth.go:95.40,100.3 4 1
mymodule/auth.go:102.2,102.25 1 1
mymodule/auth.go:102.25,106.3 3 1
mymodule/auth.go:109.2,110.16 2 1
mymodule/auth.go:110.16,115.3 4 0
mymodule/auth.go:118.2,120.16 3 0
mymodule/auth.go:118.2,120.16 3 1
mymodule/auth.go:120.16,125.3 4 0
mymodule/auth.go:127.2,128.89 2 0
mymodule/auth.go:127.2,128.89 2 1
mymodule/auth.go:131.67,136.16 3 0
mymodule/auth.go:136.16,141.3 4 0
mymodule/auth.go:143.2,148.69 4 0
Expand Down
37 changes: 8 additions & 29 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1165,60 +1165,39 @@ func (app *App) DeleteAuthor(w http.ResponseWriter, r *http.Request) {
return
}

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

// Query to check if the author has books
booksQuery := `
SELECT COUNT(*)
FROM books
WHERE author_id = ?
`

// Execute the query
var numBooks int
err = app.DB.QueryRow(booksQuery, authorID).Scan(&numBooks)
err = app.DB.QueryRow(`SELECT COUNT(*) FROM books WHERE author_id = ?`, authorID).Scan(&numBooks)
if err != nil {
app.Logger.Printf("Failed to check for books: %v", err)
http.Error(w, fmt.Sprintf("Failed to check for books: %v", err), http.StatusInternalServerError)
http.Error(w, "Failed to check for books", http.StatusInternalServerError)
return
}

// If author has books, respond with a bad request
if numBooks > 0 {
http.Error(w, "Author has associated books, delete books first", http.StatusBadRequest)
return
}

// Query to delete the author
deleteQuery := `
DELETE FROM authors
WHERE id = ?
`

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

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

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


// DeleteBook deletes an existing book from the database
func (app *App) DeleteBook(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
Expand Down

0 comments on commit 0659ff7

Please sign in to comment.