stats page pagina melhrar
This commit is contained in:
@@ -1,31 +1,37 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:playmaker/service/auth_service.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
|
||||
class TeamController {
|
||||
final AuthService _authService = AuthService();
|
||||
final CollectionReference _teamsRef = FirebaseFirestore.instance.collection('teams');
|
||||
// Acesso ao cliente do Supabase
|
||||
final SupabaseClient _supabase = Supabase.instance.client;
|
||||
|
||||
// --- STREAM DE EQUIPAS (LER) ---
|
||||
// Retorna uma Lista de Mapas em tempo real
|
||||
Stream<List<Map<String, dynamic>>> get teamsStream {
|
||||
final user = _supabase.auth.currentUser;
|
||||
|
||||
if (user == null) {
|
||||
return const Stream.empty();
|
||||
}
|
||||
|
||||
Stream<QuerySnapshot> get teamsStream {
|
||||
final uid = _authService.currentUid;
|
||||
return _teamsRef
|
||||
.where('userId', isEqualTo: uid)
|
||||
.orderBy('createdAt', descending: true)
|
||||
.snapshots();
|
||||
return _supabase
|
||||
.from('teams')
|
||||
.stream(primaryKey: ['id']) // É obrigatório definir a Primary Key para Streams
|
||||
.eq('user_id', user.id) // Filtra apenas as equipas do utilizador logado
|
||||
.order('created_at', ascending: false);
|
||||
}
|
||||
|
||||
// --- CRIAR EQUIPA ---
|
||||
Future<void> createTeam(String name, String season, String imageUrl) async {
|
||||
final uid = _authService.currentUid;
|
||||
final user = _supabase.auth.currentUser;
|
||||
|
||||
if (uid != null) {
|
||||
if (user != null) {
|
||||
try {
|
||||
await _teamsRef.add({
|
||||
await _supabase.from('teams').insert({
|
||||
'name': name,
|
||||
'season': season,
|
||||
'imageUrl': imageUrl,
|
||||
'userId': uid,
|
||||
'createdAt': FieldValue.serverTimestamp(),
|
||||
'image_url': imageUrl, // Garante que na tabela a coluna se chama 'image_url' (snake_case)
|
||||
'user_id': user.id, // Chave estrangeira para ligar ao utilizador
|
||||
// 'created_at': O Supabase preenche isto sozinho se tiver default: now()
|
||||
});
|
||||
} catch (e) {
|
||||
print("Erro ao criar equipa: $e");
|
||||
@@ -34,15 +40,32 @@ class TeamController {
|
||||
print("Erro: Utilizador não autenticado.");
|
||||
}
|
||||
}
|
||||
|
||||
// --- ELIMINAR EQUIPA ---
|
||||
Future<void> deleteTeam(String docId) async {
|
||||
try {
|
||||
await _teamsRef.doc(docId).delete();
|
||||
// Se configuraste "ON DELETE CASCADE" no Supabase, isto apaga também os jogadores
|
||||
await _supabase.from('teams').delete().eq('id', docId);
|
||||
} catch (e) {
|
||||
print("Erro ao eliminar: $e");
|
||||
}
|
||||
Future<int> getPlayerCount(String teamId) async {
|
||||
var snapshot = await _teamsRef.doc(teamId).collection('players').get();
|
||||
return snapshot.docs.length;
|
||||
}
|
||||
}
|
||||
|
||||
// --- CONTAR JOGADORES ---
|
||||
// No SQL não entramos dentro da equipa. Vamos à tabela 'members' e filtramos pelo team_id.
|
||||
// --- CONTAR JOGADORES (CORRIGIDO) ---
|
||||
Future<int> getPlayerCount(String teamId) async {
|
||||
try {
|
||||
// Correção: O Supabase agora retorna o 'int' diretamente, não um objeto response
|
||||
final int count = await _supabase
|
||||
.from('members')
|
||||
.count(CountOption.exact) // Pede o número exato
|
||||
.eq('team_id', teamId);
|
||||
|
||||
return count;
|
||||
} catch (e) {
|
||||
print("Erro ao contar jogadores: $e");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user