112 lines
3.4 KiB
Dart
112 lines
3.4 KiB
Dart
|
|
import 'package:drift/drift.dart';
|
|||
|
|
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 ====================
|
|||
|
|
|
|||
|
|
/// Получить тест по ID
|
|||
|
|
Future<Test?> getTestById(int id) {
|
|||
|
|
return (select(tests)..where((t) => t.id.equals(id))).getSingleOrNull();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// Получить все тесты
|
|||
|
|
Future<List<Test>> getAllTests() {
|
|||
|
|
return (select(tests)
|
|||
|
|
..where((t) => t.isDeleted.equals(false))
|
|||
|
|
).get();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// Получить тесты пака
|
|||
|
|
Future<List<Test>> getTestsByPackId(int packId) async {
|
|||
|
|
final query = select(tests).join([
|
|||
|
|
innerJoin(
|
|||
|
|
testPackRelations,
|
|||
|
|
testPackRelations.testId.equalsExp(tests.id) &
|
|||
|
|
testPackRelations.packId.equals(packId),
|
|||
|
|
),
|
|||
|
|
]);
|
|||
|
|
|
|||
|
|
return query.map((row) => row.readTable(tests)).get();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// Создать тест
|
|||
|
|
Future<int> createTest(TestsCompanion test) {
|
|||
|
|
return into(tests).insert(test);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// Обновить тест
|
|||
|
|
Future<bool> updateTest(Test test) {
|
|||
|
|
return update(tests).replace(test);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// Удалить тест (soft delete)
|
|||
|
|
Future<void> softDeleteTest(int testId) {
|
|||
|
|
return (update(tests)..where((t) => t.id.equals(testId)))
|
|||
|
|
.write(TestsCompanion(
|
|||
|
|
isDeleted: const Value(true),
|
|||
|
|
updatedAt: Value(DateTime.now()),
|
|||
|
|
));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// Связать тест с паком
|
|||
|
|
Future<void> linkTestToPack(int testId, int packId) async {
|
|||
|
|
await into(testPackRelations).insert(
|
|||
|
|
TestPackRelationsCompanion.insert(
|
|||
|
|
testId: testId,
|
|||
|
|
packId: packId,
|
|||
|
|
),
|
|||
|
|
mode: InsertMode.insertOrIgnore,
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ==================== TestQuestions ====================
|
|||
|
|
|
|||
|
|
/// Получить вопросы теста
|
|||
|
|
Future<List<TestQuestion>> getTestQuestions(int testId) {
|
|||
|
|
return (select(testQuestions)
|
|||
|
|
..where((tq) => tq.testId.equals(testId))
|
|||
|
|
).get();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// Создать вопрос теста
|
|||
|
|
Future<int> createTestQuestion(TestQuestionsCompanion question) {
|
|||
|
|
return into(testQuestions).insert(question);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// Обновить вопрос теста
|
|||
|
|
Future<bool> updateTestQuestion(TestQuestion question) {
|
|||
|
|
return update(testQuestions).replace(question);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// Удалить вопрос теста
|
|||
|
|
Future<void> deleteTestQuestion(int questionId) {
|
|||
|
|
return (delete(testQuestions)..where((tq) => tq.id.equals(questionId))).go();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ==================== TestStatistics ====================
|
|||
|
|
|
|||
|
|
/// Получить статистику теста пользователя
|
|||
|
|
Future<TestStatistic?> getTestStatistics(int userId, int testId) {
|
|||
|
|
return (select(testStatistics)
|
|||
|
|
..where((ts) => ts.userId.equals(userId) & ts.testId.equals(testId))
|
|||
|
|
).getSingleOrNull();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// Создать статистику теста
|
|||
|
|
Future<int> createTestStatistics(TestStatisticsCompanion statistics) {
|
|||
|
|
return into(testStatistics).insert(statistics);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// Обновить статистику теста
|
|||
|
|
Future<bool> updateTestStatistics(TestStatistic statistics) {
|
|||
|
|
return update(testStatistics).replace(statistics);
|
|||
|
|
}
|
|||
|
|
}
|