Base de dados atualizada

This commit is contained in:
Carlos Correia
2026-03-24 10:43:49 +00:00
parent 1794208143
commit b70ac32327
5 changed files with 865 additions and 204 deletions

View File

@@ -8,7 +8,7 @@ class SupabaseService {
static Future<void> initialize() async {
try {
print('DEBUG: Inicializando Supabase...');
await Supabase.initialize(
url: AppConstants.supabaseUrl,
anonKey: AppConstants.supabaseAnonKey,
@@ -110,4 +110,118 @@ class SupabaseService {
// Listen to auth state changes
static Stream<AuthState> get authStateChanges =>
_supabase.auth.onAuthStateChange;
// Save a new run
static Future<void> saveRun({
required double distance,
required double pace,
required int duration,
}) async {
try {
final userId = currentUser?.id;
if (userId == null) {
throw Exception('Usuário não autenticado');
}
// Insert new run
await _supabase.from('runs').insert({
'id_user': userId,
'distance': distance,
'pace': pace,
'created_at': DateTime.now().toIso8601String(),
});
// Update user stats
await _updateUserStats(userId, distance, pace);
} catch (e) {
throw Exception('Erro ao salvar corrida: $e');
}
}
// Update user statistics
static Future<void> _updateUserStats(
String userId,
double newDistance,
double newPace,
) async {
try {
// Get current user stats
final currentStats = await _supabase
.from('user_stats')
.select('best_pace, max_distance')
.eq('id_user', userId)
.maybeSingle();
if (currentStats == null) {
// Create new user stats record
await _supabase.from('user_stats').insert({
'id_user': userId,
'best_pace': newPace,
'max_distance': newDistance,
'created_at': DateTime.now().toIso8601String(),
});
} else {
// Update if new records are better
final updates = <String, dynamic>{};
if (currentStats['max_distance'] == null ||
newDistance > currentStats['max_distance']) {
updates['max_distance'] = newDistance;
}
if (currentStats['best_pace'] == null ||
newPace < currentStats['best_pace']) {
updates['best_pace'] = newPace;
}
if (updates.isNotEmpty) {
await _supabase
.from('user_stats')
.update(updates)
.eq('id_user', userId);
}
}
} catch (e) {
throw Exception('Erro ao atualizar estatísticas: $e');
}
}
// Get user statistics
static Future<Map<String, dynamic>?> getUserStats() async {
try {
final userId = currentUser?.id;
if (userId == null) {
throw Exception('Usuário não autenticado');
}
return await _supabase
.from('user_stats')
.select('*')
.eq('id_user', userId)
.maybeSingle();
} catch (e) {
throw Exception('Erro ao buscar estatísticas: $e');
}
}
// Get user runs history
static Future<List<Map<String, dynamic>>> getUserRuns({
int limit = 50,
}) async {
try {
final userId = currentUser?.id;
if (userId == null) {
throw Exception('Usuário não autenticado');
}
return await _supabase
.from('runs')
.select('*')
.eq('id_user', userId)
.order('created_at', ascending: false)
.limit(limit);
} catch (e) {
throw Exception('Erro ao buscar histórico de corridas: $e');
}
}
}