-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathincident_server.service.dart
40 lines (33 loc) · 1.07 KB
/
incident_server.service.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:safe/models/incident/incident.model.dart';
class IncidentServer {
final FirebaseFirestore _db;
static String path = "incidents";
IncidentServer(this._db);
// -> READ
Stream<List<Incident>> readFromUserId({required String userId}) {
return _db
.collection(path)
.where("user_id", isEqualTo: userId)
.snapshots()
.map(
(snapshot) => snapshot.docs
.map(
(doc) => Incident.fromJson(doc.data()),
)
.toList(),
);
}
Stream<Incident> readFromId({required String id}) {
return _db.collection(path).doc(id).snapshots().map(
(doc) => Incident.fromJson(doc.data()!),
);
}
// -> UPSERT
Future<void> upsert(Incident incident, {bool shouldMerge = true}) {
var options = SetOptions(merge: shouldMerge);
return _db.collection(path).doc(incident.id).set(incident.toMap(), options);
}
// -> DELETE
Future<void> delete(String id) => _db.collection(path).doc(id).delete();
}