addComment method
Future<void>
addComment({
- required PeamanComment comment,
- dynamic onSuccess()?,
- dynamic onError(
- dynamic
Implementation
Future<void> addComment({
required final PeamanComment comment,
final Function(PeamanComment)? onSuccess,
final Function(dynamic)? onError,
}) async {
try {
if (comment.feedId == null) throw Exception("feedId can't be null");
if (comment.ownerId == null) throw Exception("feedId can't be null");
if (comment.parentId == null) throw Exception("parentId can't be null");
if (comment.parentOwnerId == null)
throw Exception("parentOwner can't be null");
final _currentMillis = DateTime.now().millisecondsSinceEpoch;
final _commentRef =
PeamanReferenceHelper.commentsCol(feedId: comment.feedId!)
.doc(comment.id);
final _commentedFeedRef =
PeamanReferenceHelper.commentedFeedsCol(uid: comment.ownerId!)
.doc(comment.feedId);
final _repliedFeedRef =
PeamanReferenceHelper.repliedFeedsCol(uid: comment.ownerId!)
.doc(comment.feedId);
final _feedCommenterRef =
PeamanReferenceHelper.feedCommentersCol(feedId: comment.feedId!)
.doc(comment.ownerId);
final _feedReplierRef =
PeamanReferenceHelper.feedRepliersCol(feedId: comment.feedId!)
.doc(comment.ownerId);
final _comment = comment.copyWith(
id: _commentRef.id,
createdAt: comment.createdAt ?? _currentMillis,
updatedAt: comment.updatedAt ?? _currentMillis,
);
final _commentedFeed = PeamanSavedFeed(
id: _comment.feedId,
createdAt: _currentMillis,
updatedAt: _currentMillis,
);
final _repliedFeed = PeamanSavedFeed(
id: _comment.feedId,
createdAt: _currentMillis,
updatedAt: _currentMillis,
);
final _feedCommenter = PeamanFeedCommenter(
uid: _comment.ownerId,
createdAt: _currentMillis,
updatedAt: _currentMillis,
);
final _feedReplier = PeamanFeedReplier(
uid: _comment.ownerId,
createdAt: _currentMillis,
updatedAt: _currentMillis,
);
final _futures = <Future>[];
final _future = _commentRef.set(_comment.toJson());
_futures.add(_future);
if (_comment.parent == PeamanCommentParent.feed) {
final _feedPropertiesFuture = updateFeedPropertiesCount(
feedId: _comment.feedId!,
commentsCount: 1,
);
final _userPropertyFuture = PUserProvider.updateUserPropertiesCount(
uid: _comment.parentOwnerId!,
commentsReceivedFromFeeds: 1,
);
final _commentedFeedFuture = _commentedFeedRef.set(
_commentedFeed.toJson(),
);
final _feedCommenterFuture = _feedCommenterRef.set(
_feedCommenter.toJson(),
);
_futures.add(_feedPropertiesFuture);
_futures.add(_userPropertyFuture);
_futures.add(_commentedFeedFuture);
_futures.add(_feedCommenterFuture);
print('Success: Adding comment to feed ${comment.feedId}');
} else {
final _commentPropertiesFuture = updateCommentPropertiesCount(
feedId: _comment.feedId!,
commentId: _comment.parentId!,
repliesCount: 1,
);
final _feedPropertiesFuture = updateFeedPropertiesCount(
feedId: _comment.feedId!,
repliesCount: 1,
);
final _userPropertyFuture = PUserProvider.updateUserPropertiesCount(
uid: _comment.parentOwnerId!,
repliesReceivedFromFeeds: 1,
);
final _repliedFeedFuture = _repliedFeedRef.set(
_repliedFeed.toJson(),
);
final _feedReplierFuture = _feedReplierRef.set(
_feedReplier.toJson(),
);
_futures.add(_feedPropertiesFuture);
_futures.add(_commentPropertiesFuture);
_futures.add(_userPropertyFuture);
_futures.add(_repliedFeedFuture);
_futures.add(_feedReplierFuture);
print('Success: Adding comment to comment ${_comment.parentId}');
}
await Future.wait(_futures);
onSuccess?.call(_comment);
} catch (e) {
print(e);
print('Error!!!: Adding comment');
onError?.call(e);
}
}