Beispiel #1
0
        public async Task LikeAsync()
        {
            if (Item.Value == null || IsLiked.Value)
            {
                return;
            }

            _isLiked.Value = true;
            Item.Value.LikeCount++;

            using (await _lock.LockAsync())
            {
                try
                {
                    var success = await _firestore.RunTransactionAsync(transaction =>
                    {
                        var document = _firestore.GetCollection(Models.Item.CollectionPath)
                                       .GetDocument(Item.Value.Id);

                        var item = transaction.GetDocument(document).ToObject <Item>();

                        if (item == null)
                        {
                            return(false);
                        }

                        item.LikeCount++;
                        transaction.UpdateData(document, item);

                        return(true);
                    }).ConfigureAwait(false);

                    if (success)
                    {
                        var like = new Like
                        {
                            Timestamp = DateTime.Now.Ticks
                        };

                        await _firestore.GetDocument($"{User.CollectionPath}/{_accountService.UserId.Value}/{Like.CollectionPath}/{Item.Value.Id}")
                        .SetDataAsync(like)
                        .ConfigureAwait(false);
                    }
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e);
                }
            }
        }
        public Task IncrementContributionCountAsync(int delta)
        {
            return(_firestore.RunTransactionAsync(transaction =>
            {
                var document = _firestore.GetCollection(User.CollectionPath)
                               .GetDocument(UserId.Value);

                var user = transaction.GetDocument(document).ToObject <User>();

                if (user != null)
                {
                    user.ContributionCount += delta;
                    transaction.UpdateData(document, user);
                }
            }));
        }