createMoment method
Future<void>
createMoment({
- required PeamanMoment moment,
- dynamic onSuccess()?,
- dynamic onError(
- dynamic
Implementation
Future<void> createMoment({
required final PeamanMoment moment,
final Function(PeamanMoment)? onSuccess,
final Function(dynamic)? onError,
}) async {
try {
final _currentMillis = DateTime.now().millisecondsSinceEpoch;
late PeamanMoment _moment;
final _ownerMomentsRef = PeamanReferenceHelper.momentsCol
.where('owner_id', isEqualTo: moment.ownerId)
.limit(1);
final _ownerMomentsSnap = await _ownerMomentsRef.get();
if (_ownerMomentsSnap.docs.isNotEmpty) {
final _ownerMomentSnap = _ownerMomentsSnap.docs.first;
if (_ownerMomentSnap.exists) {
final _pictures = moment.pictures.map((e) {
final _randomDocument =
FirebaseFirestore.instance.collection('random').doc();
final _momentPicture = e.toJson();
_momentPicture['id'] = e.id ?? _randomDocument.id;
return _momentPicture;
}).toList();
await _ownerMomentSnap.reference.update({
...moment.toJson(),
'pictures': FieldValue.arrayUnion(_pictures),
});
final _ownerMomentData = _ownerMomentSnap.data();
_moment = PeamanMoment.fromJson(_ownerMomentData);
}
} else {
final _momentRef = PeamanReferenceHelper.momentsCol.doc(moment.id);
final _pictures = moment.pictures.map((e) {
final _randomDocument =
FirebaseFirestore.instance.collection('random').doc();
final _momentPicture = e.copyWith(id: e.id ?? _randomDocument.id);
return _momentPicture;
}).toList();
_moment = moment.copyWith(
id: _momentRef.id,
pictures: _pictures,
createdAt: moment.createdAt ?? _currentMillis,
updatedAt: moment.updatedAt ?? _currentMillis,
);
await _momentRef.set(_moment.toJson());
}
print('Success: Creating moment ${_moment.id}');
onSuccess?.call(_moment);
} catch (e) {
print(e);
print('Error!!!: Creating moment');
onError?.call(e);
}
}