diff --git a/api/coverage.out b/api/coverage.out index 47fdb77..a6ea2e9 100644 --- a/api/coverage.out +++ b/api/coverage.out @@ -1,19 +1,19 @@ mode: set -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: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:110.16,115.3 4 0 -mymodule/auth.go:118.2,120.16 3 1 +mymodule/auth.go:118.2,120.16 3 0 mymodule/auth.go:120.16,125.3 4 0 -mymodule/auth.go:127.2,128.89 2 1 +mymodule/auth.go:127.2,128.89 2 0 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 @@ -302,41 +302,41 @@ mymodule/main.go:1149.5,1149.103 1 1 mymodule/main.go:1153.58,1154.90 1 1 mymodule/main.go:1154.90,1156.6 1 1 mymodule/main.go:1157.5,1157.15 1 1 -mymodule/main.go:1162.70,1163.35 1 0 -mymodule/main.go:1163.35,1166.3 2 0 -mymodule/main.go:1169.2,1171.16 3 0 -mymodule/main.go:1171.16,1174.3 2 0 -mymodule/main.go:1177.2,1186.16 4 0 -mymodule/main.go:1186.16,1190.3 3 0 -mymodule/main.go:1193.2,1193.18 1 0 -mymodule/main.go:1193.18,1196.3 2 0 -mymodule/main.go:1199.2,1206.16 3 0 -mymodule/main.go:1206.16,1210.3 3 0 -mymodule/main.go:1213.2,1214.23 2 0 -mymodule/main.go:1214.23,1217.3 2 0 -mymodule/main.go:1219.2,1219.47 1 0 -mymodule/main.go:1223.68,1224.35 1 0 -mymodule/main.go:1224.35,1227.3 2 0 -mymodule/main.go:1230.2,1232.16 3 0 -mymodule/main.go:1232.16,1235.3 2 0 -mymodule/main.go:1238.2,1247.16 4 0 -mymodule/main.go:1247.16,1251.3 3 0 -mymodule/main.go:1254.2,1263.16 4 0 -mymodule/main.go:1263.16,1267.3 3 0 -mymodule/main.go:1270.2,1277.16 3 0 -mymodule/main.go:1277.16,1281.3 3 0 -mymodule/main.go:1284.2,1285.23 2 0 -mymodule/main.go:1285.23,1288.3 2 0 -mymodule/main.go:1291.2,1291.24 1 0 -mymodule/main.go:1291.24,1299.17 3 0 -mymodule/main.go:1299.17,1303.4 3 0 -mymodule/main.go:1306.2,1306.45 1 0 -mymodule/main.go:1310.74,1311.35 1 0 -mymodule/main.go:1311.35,1314.3 2 0 -mymodule/main.go:1317.2,1319.16 3 0 -mymodule/main.go:1319.16,1322.3 2 0 -mymodule/main.go:1325.2,1332.16 3 0 -mymodule/main.go:1332.16,1336.3 3 0 -mymodule/main.go:1339.2,1340.23 2 0 -mymodule/main.go:1340.23,1343.3 2 0 -mymodule/main.go:1345.2,1345.51 1 0 +mymodule/main.go:1161.70,1162.35 1 1 +mymodule/main.go:1162.35,1165.3 2 1 +mymodule/main.go:1167.2,1168.16 2 1 +mymodule/main.go:1168.16,1171.3 2 1 +mymodule/main.go:1173.2,1175.16 3 1 +mymodule/main.go:1175.16,1178.3 2 1 +mymodule/main.go:1180.2,1180.18 1 1 +mymodule/main.go:1180.18,1183.3 2 1 +mymodule/main.go:1185.2,1186.16 2 1 +mymodule/main.go:1186.16,1189.3 2 1 +mymodule/main.go:1191.2,1191.65 1 1 +mymodule/main.go:1191.65,1194.3 2 1 +mymodule/main.go:1196.2,1196.48 1 1 +mymodule/main.go:1200.68,1201.35 1 0 +mymodule/main.go:1201.35,1204.3 2 0 +mymodule/main.go:1207.2,1209.16 3 0 +mymodule/main.go:1209.16,1212.3 2 0 +mymodule/main.go:1215.2,1224.16 4 0 +mymodule/main.go:1224.16,1228.3 3 0 +mymodule/main.go:1231.2,1240.16 4 0 +mymodule/main.go:1240.16,1244.3 3 0 +mymodule/main.go:1247.2,1254.16 3 0 +mymodule/main.go:1254.16,1258.3 3 0 +mymodule/main.go:1261.2,1262.23 2 0 +mymodule/main.go:1262.23,1265.3 2 0 +mymodule/main.go:1268.2,1268.24 1 0 +mymodule/main.go:1268.24,1276.17 3 0 +mymodule/main.go:1276.17,1280.4 3 0 +mymodule/main.go:1283.2,1283.45 1 0 +mymodule/main.go:1287.74,1288.35 1 0 +mymodule/main.go:1288.35,1291.3 2 0 +mymodule/main.go:1294.2,1296.16 3 0 +mymodule/main.go:1296.16,1299.3 2 0 +mymodule/main.go:1302.2,1309.16 3 0 +mymodule/main.go:1309.16,1313.3 3 0 +mymodule/main.go:1316.2,1317.23 2 0 +mymodule/main.go:1317.23,1320.3 2 0 +mymodule/main.go:1322.2,1322.51 1 0 diff --git a/api/main.go b/api/main.go index 80f7c4b..c34bec8 100644 --- a/api/main.go +++ b/api/main.go @@ -1157,7 +1157,6 @@ func ValidateSubscriberData(subscriber Subscriber) error { return nil } - // DeleteAuthor deletes an existing author from the database func (app *App) DeleteAuthor(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodDelete { @@ -1197,7 +1196,6 @@ func (app *App) DeleteAuthor(w http.ResponseWriter, r *http.Request) { 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 { @@ -1205,84 +1203,51 @@ func (app *App) DeleteBook(w http.ResponseWriter, r *http.Request) { return } - // Extract the book ID from the URL path - vars := mux.Vars(r) - bookID, err := strconv.Atoi(vars["id"]) + bookID, err := strconv.Atoi(mux.Vars(r)["id"]) if err != nil { http.Error(w, "Invalid book ID", http.StatusBadRequest) return } - // Query to get the author ID of the book - authorIDQuery := ` - SELECT author_id - FROM books - WHERE id = ? - ` - - // Execute the query var authorID int - err = app.DB.QueryRow(authorIDQuery, bookID).Scan(&authorID) + err = app.DB.QueryRow(`SELECT author_id FROM books WHERE id = ?`, bookID).Scan(&authorID) if err != nil { app.Logger.Printf("Failed to retrieve author ID: %v", err) - http.Error(w, fmt.Sprintf("Failed to retrieve author ID: %v", err), http.StatusInternalServerError) + http.Error(w, "Failed to retrieve author ID", http.StatusInternalServerError) return } - // Query to check if the author has any other books - otherBooksQuery := ` - SELECT COUNT(*) - FROM books - WHERE author_id = ? AND id != ? - ` - - // Execute the query var numOtherBooks int - err = app.DB.QueryRow(otherBooksQuery, authorID, bookID).Scan(&numOtherBooks) + err = app.DB.QueryRow(`SELECT COUNT(*) FROM books WHERE author_id = ? AND id != ?`, authorID, bookID).Scan(&numOtherBooks) if err != nil { app.Logger.Printf("Failed to check for other books: %v", err) - http.Error(w, fmt.Sprintf("Failed to check for other books: %v", err), http.StatusInternalServerError) + http.Error(w, "Failed to check for other books", http.StatusInternalServerError) return } - // Query to delete the book - deleteBookQuery := ` - DELETE FROM books - WHERE id = ? - ` - - // Execute the query to delete the book - result, err := app.DB.Exec(deleteBookQuery, bookID) + result, err := app.DB.Exec(`DELETE FROM books WHERE id = ?`, bookID) if err != nil { app.Logger.Printf("Failed to delete book: %v", err) - http.Error(w, fmt.Sprintf("Failed to delete book: %v", err), http.StatusInternalServerError) + http.Error(w, "Failed to delete book", http.StatusInternalServerError) return } - // Check if any row was actually deleted rowsAffected, _ := result.RowsAffected() if rowsAffected == 0 { http.Error(w, "Book not found", http.StatusNotFound) return } - // If the author has no other books, delete the author as well if numOtherBooks == 0 { - deleteAuthorQuery := ` - DELETE FROM authors - WHERE id = ? - ` - - // Execute the query to delete the author - _, err = app.DB.Exec(deleteAuthorQuery, authorID) + _, 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 } } - fmt.Fprintf(w, "Book deleted successfully") + fmt.Fprintln(w, "Book deleted successfully") } // DeleteSubscriber deletes an existing subscriber from the database diff --git a/api/main_test.go b/api/main_test.go index d6a7787..670a200 100644 --- a/api/main_test.go +++ b/api/main_test.go @@ -3439,3 +3439,8 @@ func TestUpdateSubscriber_FailedToRetrieveAffectedRows(t *testing.T) { err = mock.ExpectationsWereMet() assert.NoError(t, err) } + + + + +