feat(ebooks): add ISBN normalization and graceful library requirement handling

- Increase ISBN column from VARCHAR(13) to VARCHAR(17) to support ISBN-13 with hyphens
- Add normalize_isbn() database function to automatically remove hyphens and spaces
- Create trigger to auto-normalize ISBNs on INSERT/UPDATE operations
- Update all Ebook and MediaItem queries to use ISBN normalization
- Add GetEbookLibraryID query to check for existing ebook libraries
- Add graceful error handling when no ebook library exists
- Return helpful error message: 'no ebook library found. Please create an ebook library first'
- Create comprehensive tests for ISBN normalization and library selection
- Add Bruno test files for various ISBN formats and error scenarios
- Update documentation with ISBN normalization details
This commit is contained in:
2026-01-29 10:52:14 -05:00
parent 6ed69005b5
commit 66f1eb11a0
10 changed files with 709 additions and 24 deletions
+29 -1
View File
@@ -73,13 +73,25 @@ CREATE TABLE library_visibility (
UNIQUE(user_id, library_id)
);
-- ISBN normalization function
-- Removes hyphens and spaces from ISBN to standardize format
CREATE OR REPLACE FUNCTION normalize_isbn(isbn TEXT) RETURNS TEXT AS $$
BEGIN
IF isbn IS NULL THEN
RETURN NULL;
END IF;
-- Remove hyphens and spaces, return only digits and X (for ISBN-10)
RETURN regexp_replace(isbn, '[-\s]', '', 'g');
END;
$$ LANGUAGE plpgsql IMMUTABLE;
-- Create media_items table (replaces ebooks table for broader media support)
CREATE TABLE media_items (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
library_id UUID NOT NULL REFERENCES libraries(id) ON DELETE CASCADE,
title VARCHAR(255) NOT NULL,
author VARCHAR(255),
isbn VARCHAR(13), -- Still relevant for ebooks
isbn VARCHAR(17), -- Supports ISBN-13 with hyphens (up to 17 chars)
description TEXT,
file_path VARCHAR(500) NOT NULL,
file_size BIGINT,
@@ -195,6 +207,22 @@ WHERE lt.name = 'ebooks';
-- Note: user_ebook_folders table is replaced by library_folders table
-- Libraries now handle folder management instead of individual users
-- ISBN normalization trigger
-- Automatically normalizes ISBN on insert and update
CREATE OR REPLACE FUNCTION normalize_media_item_isbn() RETURNS TRIGGER AS $$
BEGIN
IF NEW.isbn IS NOT NULL THEN
NEW.isbn := normalize_isbn(NEW.isbn);
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER trigger_normalize_media_item_isbn
BEFORE INSERT OR UPDATE ON media_items
FOR EACH ROW
EXECUTE FUNCTION normalize_media_item_isbn();
-- Create indexes for better query performance
CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_users_username ON users(username);