diff --git a/src/library/entry.cr b/src/library/entry.cr index 43fbb23..9e676c8 100644 --- a/src/library/entry.cr +++ b/src/library/entry.cr @@ -8,6 +8,9 @@ class Entry size : String, pages : Int32, id : String, encoded_path : String, encoded_title : String, mtime : Time, err_msg : String? + @[YAML::Field(ignore: true)] + @sort_title : String? + def initialize(@zip_path, @book) storage = Storage.default @encoded_path = URI.encode @zip_path @@ -66,6 +69,27 @@ class Entry end end + def sort_title + sort_title_cached = @sort_title + return sort_title_cached if sort_title_cached + sort_title = Storage.default.get_entry_sort_title id + if sort_title + @sort_title = sort_title + return sort_title + end + @sort_title = @title + @title + end + + def sort_title=(sort_title : String | Nil) + Storage.default.set_entry_sort_title id, sort_title + @sort_title = sort_title + end + + def sort_title_db + Storage.default.get_entry_sort_title id + end + def display_name @book.display_name @title end diff --git a/src/library/title.cr b/src/library/title.cr index 4886f3d..d2e34a2 100644 --- a/src/library/title.cr +++ b/src/library/title.cr @@ -10,6 +10,8 @@ class Title entry_cover_url_cache : Hash(String, String)? setter entry_cover_url_cache : Hash(String, String)? + @[YAML::Field(ignore: true)] + @sort_title : String? @[YAML::Field(ignore: true)] @entry_display_name_cache : Hash(String, String)? @[YAML::Field(ignore: true)] @@ -286,6 +288,27 @@ class Title ary.join " and " end + def sort_title + sort_title_cached = @sort_title + return sort_title_cached if sort_title_cached + sort_title = Storage.default.get_title_sort_title id + if sort_title + @sort_title = sort_title + return sort_title + end + @sort_title = @title + @title + end + + def sort_title=(sort_title : String | Nil) + Storage.default.set_title_sort_title id, sort_title + @sort_title = sort_title + end + + def sort_title_db + Storage.default.get_title_sort_title id + end + def tags Storage.default.get_title_tags @id end diff --git a/src/storage.cr b/src/storage.cr index 32f446a..edf71ff 100644 --- a/src/storage.cr +++ b/src/storage.cr @@ -342,6 +342,44 @@ class Storage end end + def get_title_sort_title(title_id : String) + sort_title = nil + MainFiber.run do + get_db do |db| + sort_title = db.query_one? "Select sort_title from titles where id = (?)", title_id, as: String | Nil + end + end + sort_title + end + + def set_title_sort_title(title_id : String, sort_title : String | Nil) + sort_title = nil if sort_title == "" + MainFiber.run do + get_db do |db| + db.exec "update titles set sort_title = (?) where id = (?)", sort_title, title_id + end + end + end + + def get_entry_sort_title(entry_id : String) + sort_title = nil + MainFiber.run do + get_db do |db| + sort_title = db.query_one? "Select sort_title from ids where id = (?)", entry_id, as: String | Nil + end + end + sort_title + end + + def set_entry_sort_title(entry_id : String, sort_title : String | Nil) + sort_title = nil if sort_title == "" + MainFiber.run do + get_db do |db| + db.exec "update ids set sort_title = (?) where id = (?)", sort_title, entry_id + end + end + end + def save_thumbnail(id : String, img : Image) MainFiber.run do get_db do |db|