Example #1
0
        public async Task DeleteDislikeAsync(string space, string itemId, string userId)
        {
            if (string.IsNullOrEmpty(itemId))
            {
                throw new ArgumentNullException("itemId");
            }

            if (string.IsNullOrEmpty(userId))
            {
                throw new ArgumentNullException("userId");
            }

            // Writing to all required key families
            const SignalType signal   = SignalType.Dislike;
            DateTime         dateTime = DateTime.UtcNow;
            var tasks = new List <Task>();


            // 1. Item and user relations

            // Building composite row key to isolated different spaces of statistics
            string itemRowKey = BuildGlobalId(space, itemId);
            string userRowKey = BuildGlobalId(space, userId);

            // Item cumulative counts
            tasks.Add(_itemCountsRepository.DecDislikesAsync(itemRowKey));

            // User cumulative counts
            tasks.Add(_userCountsRepository.DecDislikesAsync(userRowKey));

            // User dislikes
            tasks.Add(Task.Run(() =>
            {
                // cancel dislike
                var deleteTasks   = new List <Task>();
                var deleteOptions = new UserSignalsInsertDeleteOptions {
                    ItemId = itemId, Signal = signal, UserId = userRowKey, DateTime = dateTime
                };

                deleteTasks.Add(_userSignalsUnorderedRepository.DeleteAsync(deleteOptions));
                deleteTasks.Add(_userSignalsRepository.DeleteAsync(deleteOptions));

                return(Task.WhenAll(deleteTasks));
            }));

            // Users who disliked an item
            tasks.Add(_itemSignalsRepository.DeleteAsync(new ItemSignalsInsertDeleteOptions {
                ItemId = itemRowKey, UserId = userId, Signal = signal, DateTime = dateTime
            }));


            // 2. Affinity groups
            IEnumerable <string> affinityGroups = BuildAffinityGroups(dateTime);

            foreach (string affinityGroup in affinityGroups)
            {
                // Building composite row keys to isolated different spaces of statistics
                string groupId = BuildGlobalId(space, affinityGroup);

                // Affinity group most liked
                tasks.Add(Task.Run(async() =>
                {
                    // getting current version of most signaled items
                    AffinityGroupMostSignaledVersionEntity currentVersion = await _affinityGroupMostSignaledVersionRepository.GetAsync(groupId, signal);

                    // getting item counts
                    IEnumerable <AffinityGroupItemCountsEntity> itemCounts = await _affinityGroupItemCountsRepository.GetSequenceAsync(groupId, signal, 1000);

                    // building new version of most liked
                    Dictionary <string, AffinityGroupItemCountsEntity> itemCountsDict = itemCounts.ToDictionary(i => i.ItemId);
                    var insertOptions = new List <AffinityGroupMostSignaledInsertOptions>();
                    foreach (var i in itemCountsDict)
                    {
                        var insertItem = new AffinityGroupMostSignaledInsertOptions {
                            ItemId = i.Key, Count = i.Value.Count
                        };
                        if (insertItem.ItemId == itemId)
                        {
                            insertItem.Count = insertItem.Count - 1;
                        }

                        insertOptions.Add(insertItem);
                    }

                    await _affinityGroupMostSignaledRepository.AddAsync(groupId, signal, currentVersion.Version + 1, insertOptions);

                    // decrementing counter of itemId
                    await _affinityGroupItemCountsRepository.DecAsync(groupId, signal, itemId);

                    // incrementing version
                    await _affinityGroupMostSignaledVersionRepository.UpdateAsync(groupId, signal, currentVersion.Version + 1);
                }));

                // decrementing affinity group cumulative counts
                tasks.Add(_affinityGroupCountsRepository.DecAsync(groupId, signal));
            }

            await Task.WhenAll(tasks);
        }