mnemo_cards/mnemo_cards_backend/lib/database/daos/test_dao.dart
Dmitry 550b56276e
Some checks are pending
Backend CI / test (push) Waiting to run
Backend CI / build (push) Blocked by required conditions
Mobile App CI / test (push) Waiting to run
Mobile App CI / build-android (push) Blocked by required conditions
Mobile App CI / build-ios (push) Blocked by required conditions
Web App CI / test (push) Waiting to run
Web App CI / build (push) Blocked by required conditions
Deploy Mnemo Cards / Deploy Backend (push) Waiting to run
Deploy Mnemo Cards / Deploy Web App (push) Blocked by required conditions
Deploy Mnemo Cards / Final Verification (push) Blocked by required conditions
Deploy Telegram Bot / Deploy Telegram Bot (push) Waiting to run
postgress fix
2025-12-13 23:55:50 +03:00

114 lines
3.6 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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(String 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(String 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<String> createTest(TestsCompanion test) async {
final inserted = await into(tests).insertReturning(test);
return inserted.id;
}
/// Обновить тест
Future<bool> updateTest(Test test) {
return update(tests).replace(test);
}
/// Удалить тест (soft delete)
Future<void> softDeleteTest(String testId) {
return (update(tests)..where((t) => t.id.equals(testId)))
.write(TestsCompanion(
isDeleted: const Value(true),
updatedAt: Value(DateTime.now()),
));
}
/// Связать тест с паком
Future<void> linkTestToPack(String testId, String packId) async {
await into(testPackRelations).insert(
TestPackRelationsCompanion.insert(
testId: testId,
packId: packId,
),
mode: InsertMode.insertOrIgnore,
);
}
// ==================== TestQuestions ====================
/// Получить вопросы теста
Future<List<TestQuestion>> getTestQuestions(String testId) {
return (select(testQuestions)
..where((tq) => tq.testId.equals(testId))
).get();
}
/// Создать вопрос теста
Future<String> createTestQuestion(TestQuestionsCompanion question) async {
final inserted = await into(testQuestions).insertReturning(question);
return inserted.id;
}
/// Обновить вопрос теста
Future<bool> updateTestQuestion(TestQuestion question) {
return update(testQuestions).replace(question);
}
/// Удалить вопрос теста
Future<void> deleteTestQuestion(String questionId) {
return (delete(testQuestions)..where((tq) => tq.id.equals(questionId))).go();
}
// ==================== TestStatistics ====================
/// Получить статистику теста пользователя
Future<TestStatistic?> getTestStatistics(String userId, String testId) {
return (select(testStatistics)
..where((ts) => ts.userId.equals(userId) & ts.testId.equals(testId))
).getSingleOrNull();
}
/// Создать статистику теста
Future<String> createTestStatistics(TestStatisticsCompanion statistics) async {
final inserted = await into(testStatistics).insertReturning(statistics);
return inserted.id;
}
/// Обновить статистику теста
Future<bool> updateTestStatistics(TestStatistic statistics) {
return update(testStatistics).replace(statistics);
}
}