This commit is contained in:
DelLevin-Home
2026-06-18 18:52:01 +08:00
parent ba2612fef8
commit 8c7631b252
23 changed files with 906 additions and 599 deletions

View File

@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:sqflite/sqflite.dart';
import '../../models/data_models.dart';
import '../database_helper.dart';
@@ -6,8 +7,17 @@ import '../database_helper.dart';
class BookReviewDao {
final DatabaseHelper _dbHelper = DatabaseHelper.instance;
Future<T> _wrap<T>(String op, Future<T> Function() fn) async {
try {
return await fn();
} catch (e) {
debugPrint('[BookReviewDao] $op error: $e');
rethrow;
}
}
/// 获取书籍的所有书评
Future<List<BookReview>> getReviewsByBookId(String bookId) async {
Future<List<BookReview>> getReviewsByBookId(String bookId) => _wrap('getReviewsByBookId', () async {
final db = await _dbHelper.database;
final maps = await db.query(
'book_reviews',
@@ -16,24 +26,22 @@ class BookReviewDao {
orderBy: 'created_at DESC',
);
return maps.map((map) => BookReview.fromJson(map)).toList();
}
});
/// 根据ID获取书评
Future<BookReview?> getReviewById(String id) async {
Future<BookReview?> getReviewById(String id) => _wrap('getReviewById', () async {
final db = await _dbHelper.database;
final maps = await db.query(
'book_reviews',
where: 'id = ? AND is_deleted = 0',
whereArgs: [id],
);
if (maps.isNotEmpty) {
return BookReview.fromJson(maps.first);
}
if (maps.isNotEmpty) return BookReview.fromJson(maps.first);
return null;
}
});
/// 插入书评
Future<String> insertReview(BookReview review) async {
Future<String> insertReview(BookReview review) => _wrap('insertReview', () async {
final db = await _dbHelper.database;
await db.insert(
'book_reviews',
@@ -41,10 +49,10 @@ class BookReviewDao {
conflictAlgorithm: ConflictAlgorithm.replace,
);
return review.id;
}
});
/// 更新书评
Future<void> updateReview(BookReview review) async {
Future<void> updateReview(BookReview review) => _wrap('updateReview', () async {
final db = await _dbHelper.database;
await db.update(
'book_reviews',
@@ -52,10 +60,10 @@ class BookReviewDao {
where: 'id = ?',
whereArgs: [review.id],
);
}
});
/// 删除书评(软删除)
Future<void> deleteReview(String id) async {
Future<void> deleteReview(String id) => _wrap('deleteReview', () async {
final db = await _dbHelper.database;
await db.update(
'book_reviews',
@@ -63,30 +71,30 @@ class BookReviewDao {
where: 'id = ?',
whereArgs: [id],
);
}
});
/// 彻底删除书评
Future<void> permanentDeleteReview(String id) async {
Future<void> permanentDeleteReview(String id) => _wrap('permanentDeleteReview', () async {
final db = await _dbHelper.database;
await db.delete(
'book_reviews',
where: 'id = ?',
whereArgs: [id],
);
}
});
/// 获取书评数量
Future<int> getReviewCount(String bookId) async {
Future<int> getReviewCount(String bookId) => _wrap('getReviewCount', () async {
final db = await _dbHelper.database;
final result = await db.rawQuery(
'SELECT COUNT(*) as count FROM book_reviews WHERE book_id = ? AND is_deleted = 0',
[bookId],
);
return result.first['count'] as int? ?? 0;
}
});
/// 获取所有已删除的书评
Future<List<BookReview>> getDeletedReviews() async {
Future<List<BookReview>> getDeletedReviews() => _wrap('getDeletedReviews', () async {
final db = await _dbHelper.database;
final maps = await db.query(
'book_reviews',
@@ -94,10 +102,10 @@ class BookReviewDao {
orderBy: 'updated_at DESC',
);
return maps.map((map) => BookReview.fromJson(map)).toList();
}
});
/// 恢复已删除的书评
Future<void> restoreReview(String id) async {
Future<void> restoreReview(String id) => _wrap('restoreReview', () async {
final db = await _dbHelper.database;
await db.update(
'book_reviews',
@@ -105,5 +113,5 @@ class BookReviewDao {
where: 'id = ?',
whereArgs: [id],
);
}
});
}