mnemo_cards/mnemo_cards_backend/lib/packs/pack_manager.dart
Dmitry 336bafc600
Some checks are pending
Backend CI / test (push) Waiting to run
Backend CI / build (push) Blocked by required conditions
Web App CI / test (push) Waiting to run
Web App CI / build (push) Blocked by required conditions
Deploy Admin Panel / Deploy Admin Panel (push) Waiting to run
Deploy Admin Panel / Admin Panel Verification (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
tasks and stuff
2026-01-09 20:21:18 +03:00

129 lines
3.9 KiB
Dart

import 'dart:async';
import 'dart:io';
import 'package:injectable/injectable.dart';
import 'package:mnemo_cards_backend/database/database.dart';
import 'package:mnemo_cards_common_backend/mnemo_cards_common_backend.dart'
hide VoiceModel;
import 'package:mnemo_cards_common/mnemo_cards_common.dart';
import 'pack_dto_converter.dart';
import 'card_pack_drift_extension.dart';
import '../repository/export.dart';
@lazySingleton
class PackManager {
final PackRepository _packRepository;
final PackDtoConverter packDtoConverter;
final AppDatabase _db;
PackManager(this._packRepository, this.packDtoConverter, this._db);
Future<List<CardPackPreviewDto>> listPacksPreviews(
UserModel? userModel,
Map<String, String>? params,
) async {
// Get packs from Repository
final packModels = await _packRepository.getAllPacks(
enabledOnly: true,
orderByField: 'order',
);
// Convert to Drift models for toPreviewDto (temporary, until we refactor DTO conversion)
// TODO: Refactor to use PackRepository and remove direct DAO access
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;
}),
);
return (await Future.wait(
packs.map((pack) async {
final dto = await pack.toPreviewDto(userModel);
if (!pack.enabled) {
return dto.copyWith(subtitle: 'DISABLED ${dto.subtitle}');
}
return dto;
}),
)).toList();
}
Future<CardPackModel?> getPack(String id) async {
return await _packRepository.getPackById(id);
}
Future<List<GameCardModel>> getCards(String packId) async {
return await _packRepository.getPackCards(packId);
}
Future<GameCardModel?> getCard(String id) async {
return await _packRepository.getCardById(id);
}
Future<VoiceModel?> getVoice(String id) async {
return await _packRepository.getVoiceById(id);
}
Future<List<VoiceModel>> getVoices(String cardId) async {
return await _packRepository.getCardVoices(cardId);
}
Future<CardPackDto> getPackDto(String id, UserModel? userModel) async {
final packModel = await getPack(id);
if (packModel == null) {
throw StateError('Pack not found');
}
// Get pack from DAO for DTO conversion (temporary, until we refactor)
// TODO: Refactor to use PackRepository and remove direct DAO access
final pack = await _db.packDao.getPackById(id);
if (pack == null) {
throw StateError('Pack not found');
}
// TODO: Refactor to use PackRepository and remove direct DAO access
final cardsDrift = await _db.packDao.getPackCards(id);
final voices = <VoiceModel>[];
for (final card in cardsDrift) {
voices.addAll(await getVoices(card.id));
}
return await pack.toDto(cardsDrift, voices, userModel);
}
}
// Static utility functions for PackManager
class PackManagerUtils {
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(
'/Users/dmitry/StudioProjects/mnemo_cards/mnemo_cards_backend/data',
);
} else if (Platform.isWindows) {
mainPath = mainPath.substring(0, mainPath.lastIndexOf("\\"));
return Directory("$mainPath/data/flutter_assets/data");
} else {
return Directory('');
}
}
}