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
92 lines
3 KiB
Dart
92 lines
3 KiB
Dart
import 'package:drift/drift.dart';
|
||
import 'package:drift_postgres/drift_postgres.dart';
|
||
import '../database.dart';
|
||
import '../tables/achievements.dart';
|
||
|
||
part 'achievement_dao.g.dart';
|
||
|
||
@DriftAccessor(tables: [UserAchievements])
|
||
class AchievementDao extends DatabaseAccessor<AppDatabase>
|
||
with _$AchievementDaoMixin {
|
||
AchievementDao(super.db);
|
||
|
||
// ==================== UserAchievements ====================
|
||
|
||
/// Получить все достижения пользователя
|
||
Future<List<UserAchievement>> getUserAchievements(String userId) {
|
||
return (select(userAchievements)
|
||
..where((ua) => ua.userId.equals(userId))
|
||
..orderBy([(ua) => OrderingTerm.desc(ua.unlockedAt)]))
|
||
.get();
|
||
}
|
||
|
||
/// Проверить, есть ли у пользователя достижение
|
||
Future<bool> hasAchievement(String userId, String achievementId) async {
|
||
final result =
|
||
await (select(userAchievements)..where(
|
||
(ua) =>
|
||
ua.userId.equals(userId) &
|
||
ua.achievementId.equals(achievementId),
|
||
))
|
||
.getSingleOrNull();
|
||
return result != null;
|
||
}
|
||
|
||
/// Получить достижение пользователя
|
||
Future<UserAchievement?> getUserAchievement(
|
||
String userId,
|
||
String achievementId,
|
||
) {
|
||
return (select(userAchievements)..where(
|
||
(ua) =>
|
||
ua.userId.equals(userId) & ua.achievementId.equals(achievementId),
|
||
))
|
||
.getSingleOrNull();
|
||
}
|
||
|
||
/// Создать достижение пользователя
|
||
Future<String> unlockAchievement(
|
||
UserAchievementsCompanion achievement,
|
||
) async {
|
||
final inserted = await into(userAchievements).insertReturning(achievement);
|
||
return inserted.id;
|
||
}
|
||
|
||
/// Обновить прогресс достижения
|
||
Future<bool> updateAchievementProgress(
|
||
String userId,
|
||
String achievementId,
|
||
double progress,
|
||
) async {
|
||
final count =
|
||
await (update(userAchievements)..where(
|
||
(ua) =>
|
||
ua.userId.equals(userId) &
|
||
ua.achievementId.equals(achievementId),
|
||
))
|
||
.write(
|
||
UserAchievementsCompanion(
|
||
progress: Value(progress),
|
||
updatedAt: Value(PgDateTime(DateTime.now())),
|
||
),
|
||
);
|
||
return count > 0;
|
||
}
|
||
|
||
/// Получить прогресс по всем достижениям пользователя
|
||
Future<Map<String, double>> getAchievementProgress(String userId) async {
|
||
final achievements = await getUserAchievements(userId);
|
||
return Map.fromEntries(
|
||
achievements.map((a) => MapEntry(a.achievementId, a.progress)),
|
||
);
|
||
}
|
||
|
||
/// Удалить достижение пользователя (для сброса)
|
||
Future<void> removeAchievement(String userId, String achievementId) {
|
||
return (delete(userAchievements)..where(
|
||
(ua) =>
|
||
ua.userId.equals(userId) & ua.achievementId.equals(achievementId),
|
||
))
|
||
.go();
|
||
}
|
||
}
|