diff --git a/cmd/server/tests/media_item_isbn_test.go b/cmd/server/tests/media_item_isbn_test.go index 499bbfb..69c754c 100644 --- a/cmd/server/tests/media_item_isbn_test.go +++ b/cmd/server/tests/media_item_isbn_test.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httptest" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -301,7 +302,7 @@ func TestMediaItemsPagination(t *testing.T) { for i := 1; i <= 5; i++ { payload := map[string]interface{}{ "title": fmt.Sprintf("Book %d", i), - "isbn": fmt.Sprintf("978012345678%d", i), + "isbn": fmt.Sprintf("978030640615%d", i), "library_id": libID, "file_path": "/test/path.epub", "file_size": 1024, @@ -319,6 +320,9 @@ func TestMediaItemsPagination(t *testing.T) { resp.Body.Close() } + // Small delay to allow database to commit before pagination queries + time.Sleep(100 * time.Millisecond) + t.Run("Valid pagination parameters", func(t *testing.T) { req, _ := http.NewRequest("GET", setup.Server.URL+"/api/media-items?library_id="+libID+"&limit=2&offset=0", nil) req.Header.Set("Authorization", "Bearer "+token) diff --git a/internal/handlers/media.go b/internal/handlers/media.go index 5cea9af..7110bc1 100644 --- a/internal/handlers/media.go +++ b/internal/handlers/media.go @@ -915,10 +915,16 @@ func (mh *MediaHandler) CreateMediaItem(c echo.Context) error { // Validate and normalize ISBN normalizedISBN, err := utils.NormalizeISBN(req.ISBN) - if err != nil { + if err != nil && req.ISBN != "" { return c.JSON(http.StatusUnprocessableEntity, map[string]string{"error": "invalid ISBN format"}) } + // Use normalized ISBN if valid, otherwise empty string + isbnValue := normalizedISBN + if err != nil { + isbnValue = "" + } + _, err = mh.db.GetLibrary(c.Request().Context(), pgtype.UUID{Bytes: req.LibraryID, Valid: true}) if err != nil { if err == pgx.ErrNoRows { @@ -942,7 +948,7 @@ func (mh *MediaHandler) CreateMediaItem(c echo.Context) error { LibraryID: pgtype.UUID{Bytes: req.LibraryID, Valid: true}, Title: req.Title, Author: pgtype.Text{String: req.Author, Valid: req.Author != ""}, - Isbn: pgtype.Text{String: normalizedISBN, Valid: req.ISBN != ""}, + Isbn: pgtype.Text{String: isbnValue, Valid: req.ISBN != ""}, Description: pgtype.Text{String: req.Description, Valid: req.Description != ""}, FilePath: req.FilePath, FileSize: pgtype.Int8{Int64: req.FileSize, Valid: req.FileSize > 0}, @@ -1006,15 +1012,21 @@ func (mh *MediaHandler) UpdateMediaItem(c echo.Context) error { // Validate and normalize ISBN normalizedISBN, err := utils.NormalizeISBN(req.ISBN) - if err != nil { + if err != nil && req.ISBN != "" { return c.JSON(http.StatusUnprocessableEntity, map[string]string{"error": "invalid ISBN format"}) } + // Use normalized ISBN if valid, otherwise empty string + isbnValue := normalizedISBN + if err != nil { + isbnValue = "" + } + item, err := mh.db.UpdateMediaItem(c.Request().Context(), database.UpdateMediaItemParams{ ID: pgtype.UUID{Bytes: mediaUUID, Valid: true}, Title: req.Title, Author: pgtype.Text{String: req.Author, Valid: req.Author != ""}, - Isbn: pgtype.Text{String: normalizedISBN, Valid: req.ISBN != ""}, + Isbn: pgtype.Text{String: isbnValue, Valid: req.ISBN != ""}, Description: pgtype.Text{String: req.Description, Valid: req.Description != ""}, CoverImagePath: pgtype.Text{String: req.CoverImagePath, Valid: req.CoverImagePath != ""}, Series: pgtype.Text{String: req.Series, Valid: req.Series != ""},