48 lines
1.3 KiB
Dart
48 lines
1.3 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:playmaker/service/auth_service.dart';
|
|
|
|
class TeamController {
|
|
final AuthService _authService = AuthService();
|
|
final CollectionReference _teamsRef = FirebaseFirestore.instance.collection('teams');
|
|
|
|
|
|
Stream<QuerySnapshot> get teamsStream {
|
|
final uid = _authService.currentUid;
|
|
return _teamsRef
|
|
.where('userId', isEqualTo: uid)
|
|
.orderBy('createdAt', descending: true)
|
|
.snapshots();
|
|
}
|
|
|
|
// --- CRIAR EQUIPA ---
|
|
Future<void> createTeam(String name, String season, String imageUrl) async {
|
|
final uid = _authService.currentUid;
|
|
|
|
if (uid != null) {
|
|
try {
|
|
await _teamsRef.add({
|
|
'name': name,
|
|
'season': season,
|
|
'imageUrl': imageUrl,
|
|
'userId': uid,
|
|
'createdAt': FieldValue.serverTimestamp(),
|
|
});
|
|
} catch (e) {
|
|
print("Erro ao criar equipa: $e");
|
|
}
|
|
} else {
|
|
print("Erro: Utilizador não autenticado.");
|
|
}
|
|
}
|
|
Future<void> deleteTeam(String docId) async {
|
|
try {
|
|
await _teamsRef.doc(docId).delete();
|
|
} 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;
|
|
}
|
|
}
|
|
} |