import '../models/bookshelf.dart'; import 'database_service.dart'; class BookshelfRepository { final DatabaseService _databaseService = DatabaseService.instance; /// 获取所有书架 Future> getAllBookshelves() async { try { final bookshelvesBox = _databaseService.getBookshelvesBox(); return bookshelvesBox.values.toList(); } catch (e) { print('❌ 获取所有书架失败: $e'); rethrow; } } /// 根据ID获取书架 Future getBookshelfById(String id) async { try { final bookshelvesBox = _databaseService.getBookshelvesBox(); return bookshelvesBox.get(id); } catch (e) { print('❌ 根据ID获取书架失败: $e'); rethrow; } } /// 添加新书架 Future addBookshelf(Bookshelf bookshelf) async { try { final bookshelvesBox = _databaseService.getBookshelvesBox(); await bookshelvesBox.put(bookshelf.id, bookshelf); print('✅ 书架添加成功: ${bookshelf.name}'); } catch (e) { print('❌ 添加书架失败: $e'); rethrow; } } /// 更新书架信息 Future updateBookshelf(Bookshelf bookshelf) async { try { final bookshelvesBox = _databaseService.getBookshelvesBox(); await bookshelvesBox.put(bookshelf.id, bookshelf); print('✅ 书架更新成功: ${bookshelf.name}'); } catch (e) { print('❌ 更新书架失败: $e'); rethrow; } } /// 删除书架 Future deleteBookshelf(String id) async { try { final bookshelvesBox = _databaseService.getBookshelvesBox(); await bookshelvesBox.delete(id); print('✅ 书架删除成功: $id'); } catch (e) { print('❌ 删除书架失败: $e'); rethrow; } } }