Skip to content
This repository was archived by the owner on Feb 1, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (m *Book) Run(
}

func (m *Book) Test(

ctx context.Context,
// +defaultPath="/"
source *dagger.Directory,
Expand Down
19 changes: 19 additions & 0 deletions controllers/book_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,22 @@ func GetBookByID(c *gin.Context) {

c.JSON(http.StatusOK, book)
}

func DeleteBook(c *gin.Context) {
var book models.Book
id := c.Param("id")

// First check if the book exists
if err := database.DB.First(&book, id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Book not found", "details": err.Error()})
return
}

// Delete the book
if err := database.DB.Delete(&book).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete book", "details": err.Error()})
return
}

c.JSON(http.StatusOK, book)
}
1 change: 1 addition & 0 deletions routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ func RegisterRoutes(r *gin.Engine) {
api.POST("/books", controllers.CreateBook)
api.GET("/books", controllers.GetBooks)
api.GET("/books/:id", controllers.GetBookByID)
api.DELETE("/books/:id", controllers.DeleteBook)
}
}
29 changes: 29 additions & 0 deletions tests/book_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func setupTest(t *testing.T) *gin.Engine {
r.POST("/api/books", controllers.CreateBook)
r.GET("/api/books", controllers.GetBooks)
r.GET("/api/books/:id", controllers.GetBookByID)
r.DELETE("/api/books/:id", controllers.DeleteBook)
return r
}

Expand Down Expand Up @@ -103,3 +104,31 @@ func TestGetBookByID(t *testing.T) {
assert.Equal(t, firstBook.Title, fetched.Title)
assert.Equal(t, firstBook.Author, fetched.Author)
}

func TestDeleteBook(t *testing.T) {
router := setupTest(t)
seedBooks(t)

var firstBook models.Book
if err := database.DB.First(&firstBook).Error; err != nil {
t.Fatalf("Failed to fetch seeded book: %v", err)
}

req, _ := http.NewRequest("DELETE", "/api/books/"+strconv.Itoa(int(firstBook.ID)), nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)

assert.Equal(t, http.StatusOK, w.Code, "Expected 200 OK, got %d. Body: %s", w.Code, w.Body.String())

// Verify the book was deleted
var deletedBook models.Book
err := database.DB.First(&deletedBook, firstBook.ID).Error
assert.Error(t, err, "Book should have been deleted")

// Try to delete non-existent book
req, _ = http.NewRequest("DELETE", "/api/books/999999", nil)
w = httptest.NewRecorder()
router.ServeHTTP(w, req)

assert.Equal(t, http.StatusNotFound, w.Code, "Expected 404 Not Found for non-existent book")
}