mnemo_cards/mnemo_cards_backend/lib/tests/test_manager.dart

237 lines
7 KiB
Dart
Raw Normal View History

2025-11-16 11:25:27 +00:00
import 'dart:convert';
import 'package:injectable/injectable.dart';
2025-12-13 13:27:05 +00:00
import 'package:mnemo_cards_backend/database/database.dart';
2025-11-16 11:25:27 +00:00
import 'package:mnemo_cards_common_backend/mnemo_cards_common_backend.dart';
import 'package:mnemo_cards_backend/tests/generators/models/creation_test_data.dart';
import 'package:mnemo_cards_common/mnemo_cards_common.dart';
2025-12-13 13:27:05 +00:00
import 'package:drift/drift.dart' as drift;
2025-11-16 11:25:27 +00:00
import 'generators/pack_test_generator.dart';
@lazySingleton
class TestManager {
2025-12-13 13:27:05 +00:00
final AppDatabase _db;
2025-11-16 11:25:27 +00:00
2025-12-13 14:48:00 +00:00
TestManager(this._db);
2025-11-16 11:25:27 +00:00
Future<TestStatisticsDto?> _testStatisticsDto(
2025-12-13 20:55:50 +00:00
String userId, String testId) async {
2025-12-13 13:27:05 +00:00
final statistics = await _db.testDao.getTestStatistics(userId, testId);
if (statistics == null) return null;
// Convert TestStatistic to TestStatisticsDto
2025-12-13 14:48:00 +00:00
// results is stored as Map<String, dynamic> with 'attempts' key
final results = statistics.results ?? <String, dynamic>{};
final attempts = (results['attempts'] as List<dynamic>?) ?? [];
// Get words from the last attempt, or empty list if no attempts
final lastAttempt = attempts.isNotEmpty
? attempts.last as Map<String, dynamic>?
: null;
final wordsJson = (lastAttempt?['words'] as List<dynamic>?) ?? [];
// Convert words JSON to WordStatisticsDto
final words = wordsJson
.map((w) => WordStatisticsDto.fromJson(w as Map<String, dynamic>))
.toList();
2025-12-13 13:27:05 +00:00
return TestStatisticsDto(
2025-12-13 14:48:00 +00:00
testId: statistics.testId,
words: AllWordsStatisticsDto(words: words),
attempts: attempts.length,
sessionToken: lastAttempt?['sessionToken'] as String?,
2025-12-13 13:27:05 +00:00
);
2025-11-16 11:25:27 +00:00
}
Future<TestDto?> fetchTest(String id, UserModel user) async {
2025-12-13 20:55:50 +00:00
final testId = id;
2025-12-13 13:27:05 +00:00
final test = await _db.testDao.getTestById(testId);
if (test == null) return null;
final questions = await _db.testDao.getTestQuestions(testId);
final statistics = await _testStatisticsDto(user.id!, testId);
// Convert Test to TestDto
final questionsList = questions.map((q) {
// Parse question body based on type
final body = json.decode(q.body) as Map<String, dynamic>;
return AbstractTestQuestion.fromJson({
'questionType': q.questionType,
...body,
});
}).toList();
return TestDto(
id: testId.toString(),
name: test.name,
color: test.color,
cover: test.cover,
version: test.version ?? '1.0',
time: test.time,
timeSubtitle: test.timeSubtitle,
questions: questionsList,
statistics: statistics,
2025-11-16 11:25:27 +00:00
);
}
Future<List<TestDto>> availableTests(UserModel userModel) async {
2025-12-13 13:27:05 +00:00
final tests = await _db.testDao.getAllTests();
final testDtos = <TestDto>[];
for (final test in tests) {
final questions = await _db.testDao.getTestQuestions(test.id);
final statistics = await _testStatisticsDto(userModel.id!, test.id);
final questionsList = questions.map((q) {
final body = json.decode(q.body) as Map<String, dynamic>;
return AbstractTestQuestion.fromJson({
'questionType': q.questionType,
...body,
});
}).toList();
testDtos.add(TestDto(
id: test.id.toString(),
name: test.name,
color: test.color,
cover: test.cover,
version: test.version ?? '1.0',
time: test.time,
timeSubtitle: test.timeSubtitle,
questions: questionsList,
statistics: statistics,
));
}
return testDtos;
2025-11-16 11:25:27 +00:00
}
Future<List<TestDto>> fetchPackTests(
UserModel user,
CardPackModel model,
) async {
2025-12-13 13:27:05 +00:00
final packId = model.id;
if (packId == null) return [];
final tests = await _db.testDao.getTestsByPackId(packId);
final testDtos = <TestDto>[];
for (final test in tests) {
final questions = await _db.testDao.getTestQuestions(test.id);
final statistics = await _testStatisticsDto(user.id!, test.id);
final questionsList = questions.map((q) {
final body = json.decode(q.body) as Map<String, dynamic>;
return AbstractTestQuestion.fromJson({
'questionType': q.questionType,
...body,
});
}).toList();
testDtos.add(TestDto(
id: test.id.toString(),
name: test.name,
color: test.color,
cover: test.cover,
version: test.version ?? '1.0',
time: test.time,
timeSubtitle: test.timeSubtitle,
questions: questionsList,
statistics: statistics,
));
2025-11-16 11:25:27 +00:00
}
2025-12-13 13:27:05 +00:00
return testDtos;
2025-11-16 11:25:27 +00:00
}
Future<void> refreshCustomCreationTestsData() async {
2025-12-13 13:27:05 +00:00
// Load custom creation test data from database
// For now, keep it simple
2025-11-16 11:25:27 +00:00
}
Future<void> updateGeneratedTests(CardPackModel model) async {
2025-12-13 13:27:05 +00:00
final packId = model.id;
if (packId == null) return;
// Get pack data from database
final pack = await _db.packDao.getPackById(packId);
if (pack == null) return;
// Get all cards for the pack
final cards = await _db.packDao.getPackCards(packId);
if (cards.isEmpty) return;
2025-12-16 23:26:49 +00:00
// Delete old generated tests for this pack
final existingTests = await _db.testDao.getTestsByPackId(packId);
final oldGeneratedTests = existingTests
.where((test) => test.version == 'generated')
.toList();
for (final oldTest in oldGeneratedTests) {
await _db.testDao.softDeleteTest(oldTest.id);
}
2025-12-13 13:27:05 +00:00
// Create creation test data
final testDataItems = cards.map((card) {
return TestDataItem(
id: card.id.toString(),
original: card.original,
translation: card.translation,
image: card.image,
2025-12-13 14:48:00 +00:00
audio: card.original, // Use original as audio
2025-12-13 13:27:05 +00:00
);
}).toList();
final creationTestData = CreationTestData(
items: testDataItems,
2025-12-13 14:48:00 +00:00
title: pack.title,
2025-12-13 13:27:05 +00:00
color: pack.color,
packId: packId.toString(),
2025-11-16 11:25:27 +00:00
);
2025-12-13 13:27:05 +00:00
// Generate test using PackTestGenerator
final generator = PackTestGenerator(creationTestData);
final testDto = await generator.generate(
name: '${pack.title} Test',
ratios: {
TestQuestionType.simple: 0.7,
TestQuestionType.input_buttons: 0.3,
},
multiply: 1.0,
);
// Save test to database
await addTest(testDto);
// Link test to pack
2025-12-13 20:55:50 +00:00
await _db.testDao.linkTestToPack(testDto.id!, packId);
2025-11-16 11:25:27 +00:00
}
Future<void> addTest(TestDto testDto) async {
2025-12-13 13:27:05 +00:00
await _db.transaction(() async {
// Create test
final testCompanion = TestsCompanion.insert(
name: testDto.name,
color: drift.Value(testDto.color),
cover: drift.Value(testDto.cover),
version: drift.Value(testDto.version),
time: drift.Value(testDto.time),
timeSubtitle: drift.Value(testDto.timeSubtitle),
);
final testId = await _db.testDao.createTest(testCompanion);
// Create questions
for (final question in testDto.questions) {
final questionCompanion = TestQuestionsCompanion.insert(
testId: testId,
questionType: question.questionType.name,
body: json.encode(question.toJson()),
2025-11-16 11:25:27 +00:00
);
2025-12-13 13:27:05 +00:00
await _db.testDao.createTestQuestion(questionCompanion);
2025-11-16 11:25:27 +00:00
}
});
}
2025-12-13 13:27:05 +00:00
}