mnemo_cards/mnemo_cards_backend/lib/packs/pack_manager.dart

130 lines
3.9 KiB
Dart
Raw Normal View History

2025-11-16 11:25:27 +00:00
import 'dart:async';
import 'dart:io';
import 'package:injectable/injectable.dart';
2025-12-13 13:27:05 +00:00
import 'package:mnemo_cards_backend/database/database.dart';
2025-12-20 18:26:15 +00:00
import 'package:mnemo_cards_common_backend/mnemo_cards_common_backend.dart'
hide VoiceModel;
2025-11-16 11:25:27 +00:00
import 'package:mnemo_cards_common/mnemo_cards_common.dart';
import 'pack_dto_converter.dart';
2025-12-13 13:27:05 +00:00
import 'card_pack_drift_extension.dart';
2026-01-09 17:21:18 +00:00
import '../repository/export.dart';
2025-11-16 11:25:27 +00:00
@lazySingleton
class PackManager {
2026-01-08 16:07:56 +00:00
final PackRepository _packRepository;
2025-11-16 11:25:27 +00:00
final PackDtoConverter packDtoConverter;
2026-01-09 17:21:18 +00:00
final AppDatabase _db;
2025-11-16 11:25:27 +00:00
2026-01-09 17:21:18 +00:00
PackManager(this._packRepository, this.packDtoConverter, this._db);
2025-11-16 11:25:27 +00:00
Future<List<CardPackPreviewDto>> listPacksPreviews(
UserModel? userModel,
Map<String, String>? params,
) async {
2026-01-08 16:07:56 +00:00
// Get packs from Repository
final packModels = await _packRepository.getAllPacks(
2025-12-13 13:27:05 +00:00
enabledOnly: true,
orderByField: 'order',
);
2026-01-08 16:07:56 +00:00
// Convert to Drift models for toPreviewDto (temporary, until we refactor DTO conversion)
2026-01-09 17:21:18 +00:00
// TODO: Refactor to use PackRepository and remove direct DAO access
2026-01-08 16:07:56 +00:00
final packs = await Future.wait(
packModels.map((packModel) async {
final pack = await _db.packDao.getPackById(packModel.id!);
if (pack == null) {
throw StateError('Pack not found: ${packModel.id}');
}
return pack;
}),
);
2025-12-20 18:26:15 +00:00
return (await Future.wait(
packs.map((pack) async {
2025-12-13 13:27:05 +00:00
final dto = await pack.toPreviewDto(userModel);
if (!pack.enabled) {
2025-11-16 11:25:27 +00:00
return dto.copyWith(subtitle: 'DISABLED ${dto.subtitle}');
}
return dto;
2025-12-20 18:26:15 +00:00
}),
)).toList();
2025-11-16 11:25:27 +00:00
}
2026-01-08 16:07:56 +00:00
Future<CardPackModel?> getPack(String id) async {
return await _packRepository.getPackById(id);
2025-11-16 11:25:27 +00:00
}
2026-01-08 16:07:56 +00:00
Future<List<GameCardModel>> getCards(String packId) async {
return await _packRepository.getPackCards(packId);
2025-11-16 11:25:27 +00:00
}
2026-01-08 16:07:56 +00:00
Future<GameCardModel?> getCard(String id) async {
return await _packRepository.getCardById(id);
2025-11-16 11:25:27 +00:00
}
2025-12-13 20:55:50 +00:00
Future<VoiceModel?> getVoice(String id) async {
2026-01-09 17:21:18 +00:00
return await _packRepository.getVoiceById(id);
2025-11-16 11:25:27 +00:00
}
2025-12-13 20:55:50 +00:00
Future<List<VoiceModel>> getVoices(String cardId) async {
2026-01-09 17:21:18 +00:00
return await _packRepository.getCardVoices(cardId);
2025-11-16 11:25:27 +00:00
}
2025-12-13 20:55:50 +00:00
Future<CardPackDto> getPackDto(String id, UserModel? userModel) async {
2026-01-08 16:07:56 +00:00
final packModel = await getPack(id);
if (packModel == null) {
throw StateError('Pack not found');
}
// Get pack from DAO for DTO conversion (temporary, until we refactor)
2026-01-09 17:21:18 +00:00
// TODO: Refactor to use PackRepository and remove direct DAO access
2026-01-08 16:07:56 +00:00
final pack = await _db.packDao.getPackById(id);
2025-12-13 13:27:05 +00:00
if (pack == null) {
throw StateError('Pack not found');
2025-11-16 11:25:27 +00:00
}
2026-01-09 17:21:18 +00:00
// TODO: Refactor to use PackRepository and remove direct DAO access
final cardsDrift = await _db.packDao.getPackCards(id);
2025-12-13 13:27:05 +00:00
final voices = <VoiceModel>[];
2026-01-09 17:21:18 +00:00
for (final card in cardsDrift) {
2025-12-13 13:27:05 +00:00
voices.addAll(await getVoices(card.id));
2025-11-16 11:25:27 +00:00
}
2026-01-09 17:21:18 +00:00
return await pack.toDto(cardsDrift, voices, userModel);
2025-11-16 11:25:27 +00:00
}
2025-12-13 13:27:05 +00:00
}
2025-11-16 11:25:27 +00:00
2025-12-13 13:27:05 +00:00
// Static utility functions for PackManager
class PackManagerUtils {
2025-11-16 11:25:27 +00:00
static Directory get assetsDirectory {
String mainPath = Platform.resolvedExecutable;
if ((Platform.isMacOS || Platform.isLinux) &&
!Platform.script.toString().contains('StudioProjects')) {
mainPath = mainPath.substring(0, mainPath.lastIndexOf("/"));
var dir = Directory("$mainPath/../data");
if (dir.existsSync()) {
return dir;
}
dir = Directory("$mainPath/data");
if (dir.existsSync()) {
return dir;
}
throw Exception('No asset dir! $mainPath');
}
if (Platform.isMacOS) {
mainPath = mainPath.substring(0, mainPath.lastIndexOf("/"));
return Directory(
2025-12-20 18:26:15 +00:00
'/Users/dmitry/StudioProjects/mnemo_cards/mnemo_cards_backend/data',
);
2025-11-16 11:25:27 +00:00
} else if (Platform.isWindows) {
mainPath = mainPath.substring(0, mainPath.lastIndexOf("\\"));
return Directory("$mainPath/data/flutter_assets/data");
} else {
return Directory('');
}
}
2025-12-20 18:26:15 +00:00
}