2025-12-13 13:27:05 +00:00
|
|
|
|
import 'package:drift/drift.dart';
|
2025-12-13 23:35:14 +00:00
|
|
|
|
import 'package:drift_postgres/drift_postgres.dart';
|
2025-12-13 13:27:05 +00:00
|
|
|
|
import '../database.dart';
|
|
|
|
|
|
import '../tables/tests.dart';
|
|
|
|
|
|
import '../tables/packs.dart';
|
|
|
|
|
|
|
|
|
|
|
|
part 'test_dao.g.dart';
|
|
|
|
|
|
|
|
|
|
|
|
@DriftAccessor(tables: [Tests, TestQuestions, TestPackRelations, TestStatistics])
|
|
|
|
|
|
class TestDao extends DatabaseAccessor<AppDatabase> with _$TestDaoMixin {
|
|
|
|
|
|
TestDao(super.db);
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== Tests ====================
|
|
|
|
|
|
|
2025-12-14 20:42:38 +00:00
|
|
|
|
/// Получить тест по ID (только активные)
|
2025-12-13 20:55:50 +00:00
|
|
|
|
Future<Test?> getTestById(String id) {
|
2025-12-14 20:42:38 +00:00
|
|
|
|
return (select(tests)
|
|
|
|
|
|
..where((t) => t.id.equals(id) & t.isDeleted.equals(false))
|
|
|
|
|
|
).getSingleOrNull();
|
2025-12-13 13:27:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-14 20:42:38 +00:00
|
|
|
|
/// Получить все тесты (только активные)
|
2025-12-13 13:27:05 +00:00
|
|
|
|
Future<List<Test>> getAllTests() {
|
|
|
|
|
|
return (select(tests)
|
|
|
|
|
|
..where((t) => t.isDeleted.equals(false))
|
|
|
|
|
|
).get();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-14 20:42:38 +00:00
|
|
|
|
/// Получить тесты пака (только активные)
|
2025-12-13 20:55:50 +00:00
|
|
|
|
Future<List<Test>> getTestsByPackId(String packId) async {
|
2025-12-13 13:27:05 +00:00
|
|
|
|
final query = select(tests).join([
|
|
|
|
|
|
innerJoin(
|
|
|
|
|
|
testPackRelations,
|
|
|
|
|
|
testPackRelations.testId.equalsExp(tests.id) &
|
|
|
|
|
|
testPackRelations.packId.equals(packId),
|
|
|
|
|
|
),
|
2025-12-14 20:42:38 +00:00
|
|
|
|
])
|
|
|
|
|
|
..where(tests.isDeleted.equals(false));
|
2025-12-13 13:27:05 +00:00
|
|
|
|
|
|
|
|
|
|
return query.map((row) => row.readTable(tests)).get();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Создать тест
|
2025-12-13 20:55:50 +00:00
|
|
|
|
Future<String> createTest(TestsCompanion test) async {
|
|
|
|
|
|
final inserted = await into(tests).insertReturning(test);
|
|
|
|
|
|
return inserted.id;
|
2025-12-13 13:27:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Обновить тест
|
|
|
|
|
|
Future<bool> updateTest(Test test) {
|
|
|
|
|
|
return update(tests).replace(test);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Удалить тест (soft delete)
|
2025-12-13 20:55:50 +00:00
|
|
|
|
Future<void> softDeleteTest(String testId) {
|
2025-12-13 13:27:05 +00:00
|
|
|
|
return (update(tests)..where((t) => t.id.equals(testId)))
|
|
|
|
|
|
.write(TestsCompanion(
|
|
|
|
|
|
isDeleted: const Value(true),
|
2025-12-13 23:35:14 +00:00
|
|
|
|
updatedAt: Value(PgDateTime(DateTime.now())),
|
2025-12-13 13:27:05 +00:00
|
|
|
|
));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Связать тест с паком
|
2025-12-13 20:55:50 +00:00
|
|
|
|
Future<void> linkTestToPack(String testId, String packId) async {
|
2025-12-13 13:27:05 +00:00
|
|
|
|
await into(testPackRelations).insert(
|
|
|
|
|
|
TestPackRelationsCompanion.insert(
|
|
|
|
|
|
testId: testId,
|
|
|
|
|
|
packId: packId,
|
|
|
|
|
|
),
|
|
|
|
|
|
mode: InsertMode.insertOrIgnore,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== TestQuestions ====================
|
|
|
|
|
|
|
2025-12-14 20:42:38 +00:00
|
|
|
|
/// Получить вопросы теста (только активные)
|
2025-12-13 20:55:50 +00:00
|
|
|
|
Future<List<TestQuestion>> getTestQuestions(String testId) {
|
2025-12-13 13:27:05 +00:00
|
|
|
|
return (select(testQuestions)
|
2025-12-14 20:42:38 +00:00
|
|
|
|
..where((tq) => tq.testId.equals(testId) & tq.isDeleted.equals(false))
|
2025-12-13 13:27:05 +00:00
|
|
|
|
).get();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Создать вопрос теста
|
2025-12-13 20:55:50 +00:00
|
|
|
|
Future<String> createTestQuestion(TestQuestionsCompanion question) async {
|
|
|
|
|
|
final inserted = await into(testQuestions).insertReturning(question);
|
|
|
|
|
|
return inserted.id;
|
2025-12-13 13:27:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Обновить вопрос теста
|
|
|
|
|
|
Future<bool> updateTestQuestion(TestQuestion question) {
|
|
|
|
|
|
return update(testQuestions).replace(question);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-14 20:42:38 +00:00
|
|
|
|
/// Удалить вопрос теста (soft delete)
|
|
|
|
|
|
Future<void> softDeleteTestQuestion(String questionId) {
|
|
|
|
|
|
return (update(testQuestions)..where((tq) => tq.id.equals(questionId)))
|
|
|
|
|
|
.write(TestQuestionsCompanion(
|
|
|
|
|
|
isDeleted: const Value(true),
|
|
|
|
|
|
deletedAt: Value(DateTime.now()),
|
|
|
|
|
|
updatedAt: Value(PgDateTime(DateTime.now())),
|
|
|
|
|
|
));
|
2025-12-13 13:27:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== TestStatistics ====================
|
|
|
|
|
|
|
|
|
|
|
|
/// Получить статистику теста пользователя
|
2025-12-13 20:55:50 +00:00
|
|
|
|
Future<TestStatistic?> getTestStatistics(String userId, String testId) {
|
2025-12-13 13:27:05 +00:00
|
|
|
|
return (select(testStatistics)
|
|
|
|
|
|
..where((ts) => ts.userId.equals(userId) & ts.testId.equals(testId))
|
|
|
|
|
|
).getSingleOrNull();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Создать статистику теста
|
2025-12-13 20:55:50 +00:00
|
|
|
|
Future<String> createTestStatistics(TestStatisticsCompanion statistics) async {
|
|
|
|
|
|
final inserted = await into(testStatistics).insertReturning(statistics);
|
|
|
|
|
|
return inserted.id;
|
2025-12-13 13:27:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Обновить статистику теста
|
|
|
|
|
|
Future<bool> updateTestStatistics(TestStatistic statistics) {
|
|
|
|
|
|
return update(testStatistics).replace(statistics);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|