eigentliche Handwerksapp
This commit is contained in:
50
services/auftrag_repository.dart
Normal file
50
services/auftrag_repository.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
|
||||
import '../models/auftrag.dart';
|
||||
|
||||
class AuftragRepository {
|
||||
AuftragRepository({FirebaseFirestore? firestore, FirebaseAuth? auth})
|
||||
: _db = firestore ?? FirebaseFirestore.instance,
|
||||
_auth = auth ?? FirebaseAuth.instance;
|
||||
|
||||
final FirebaseFirestore _db;
|
||||
final FirebaseAuth _auth;
|
||||
|
||||
CollectionReference<Map<String, dynamic>> get _col {
|
||||
final uid = _auth.currentUser?.uid;
|
||||
if (uid == null) {
|
||||
throw StateError('Nicht angemeldet');
|
||||
}
|
||||
return _db.collection('users').doc(uid).collection('auftraege');
|
||||
}
|
||||
|
||||
Stream<List<Auftrag>> watchAuftraege() {
|
||||
return _col.orderBy('createdAt', descending: true).snapshots().map(
|
||||
(snap) => snap.docs.map(Auftrag.fromDoc).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Future<Auftrag?> get(String id) async {
|
||||
final doc = await _col.doc(id).get();
|
||||
if (!doc.exists) return null;
|
||||
return Auftrag.fromDoc(doc);
|
||||
}
|
||||
|
||||
/// Neue Dokument-ID ohne Schreibzugriff (für Storage-Pfade vor erstem Speichern).
|
||||
String neueId() => _col.doc().id;
|
||||
|
||||
Future<void> speichern({
|
||||
required String id,
|
||||
required Auftrag daten,
|
||||
bool isNeu = false,
|
||||
}) async {
|
||||
final payload = daten.toMap();
|
||||
if (isNeu) {
|
||||
payload['createdAt'] = FieldValue.serverTimestamp();
|
||||
}
|
||||
await _col.doc(id).set(payload, SetOptions(merge: true));
|
||||
}
|
||||
|
||||
Future<void> loeschen(String id) => _col.doc(id).delete();
|
||||
}
|
||||
Reference in New Issue
Block a user