示例#1
0
        public async Task <VkUserModel> CreateAsync(string userId, CancellationToken token = default)
        {
            var userInfo = await _vkClient.GetUserInfo(userId);

            if (userInfo.Response == null)
            {
                throw new ArgumentNullException(nameof(userId));
            }

            var user = new VkUserModel
            {
                Id       = userInfo.Response.First().Id,
                FullName = userInfo.Response.First()?.FirstName + " " + userInfo.Response.First()?.LastName,
                Avatar   = userInfo.Response.First()?.Photo50
            };

            if (user.Id > 0)
            {
                await using var scope = await _transactionScopeFactory.CreateAsync(token);

                await scope.GetRepository <VkUserModel>().CreateAsync(user, token);

                await scope.CommitAsync(token);
            }

            return(user);
        }
        public async Task <DataSourceResponseModel> ExecuteAsync(UserMessageQuery messageQuery,
                                                                 CancellationToken token = default)
        {
            var(page, pageSize)   = messageQuery;
            await using var scope = await _transactionScopeFactory.CreateAsync(token);

            var query = scope.Query <VkRepost>()
                        .Where(f => f.UserId == _authenticatedUserProvider.GetUser().Id).OrderByDescending(f => f.Id);

            var data = await query.Skip(page *pageSize).Take(pageSize).ToListAsync(token);

            var posts = await _vkClient.GetById(
                data.Select(f => new VkRepostViewModel(f.OwnerId, f.MessageId)));

            var texts = posts
                        .Response.Items
                        .Select(f => new
            {
                f.Text, f.Id,
                DateStatus = data.Where(a => a.OwnerId == f.OwnerId && a.MessageId == f.Id)
                             .Select(a => a.DateStatus)
            });

            return(new DataSourceResponseModel(texts, await query.CountAsync(token)));
        }
示例#3
0
        public async Task HandleAsync(SetMessageType obj, CancellationToken token = default)
        {
            await using var transaction = await _transactionScopeFactory.CreateAsync(token);

            var message = await transaction.Query <MessageVk>()
                          .Where(a => a.MessageId == obj.MessageId && a.OwnerId == obj.OwnerId)
                          .FirstOrDefaultAsync(token);

            if (message == null)
            {
                await transaction.GetRepository <MessageVk>().CreateAsync(new MessageVk
                {
                    MessageId         = obj.MessageId,
                    OwnerId           = obj.OwnerId,
                    MessageCategoryId = obj.MessageCategory
                }, token);
            }
            else
            {
                message.MessageCategoryId = obj.MessageCategory;
                await transaction.GetRepository <MessageVk>().UpdateAsync(message, token);
            }

            await transaction.CommitAsync(token);
        }
        public async Task <IActionResult> SaveAsync([FromBody] MessageCategory category)
        {
            await using var scope = await _transactionScopeFactory.CreateAsync();

            if (category.Id > 0)
            {
                await scope.GetRepository <MessageCategory>().UpdateAsync(category);
            }
            else
            {
                await scope.GetRepository <MessageCategory>().CreateAsync(category);
            }

            await scope.CommitAsync();

            return(Ok());
        }
示例#5
0
        public async Task SetAsync(int userId, IEnumerable <VkRepostViewModel> models)
        {
            await using var scope = await _transactionScopeFactory.CreateAsync();

            var obj = models.Select(f => new
            {
                f.Id,
                f.OwnerId,
                Key = f.Id + "_" + f.OwnerId
            }).ToArray();

            var keys = obj.Select(f => f.Key);

            var alreadyCreate = scope.Query <VkRepost>()
                                .Where(f => f.UserId == userId &&
                                       (f.Status == VkRepostStatus.New || f.Status == VkRepostStatus.Pending))
                                .Select(f => new
            {
                f.MessageId,
                f.OwnerId,
                key = f.MessageId.ToString() + "_" + f.OwnerId.ToString()
            })
                                .Where(f => keys.Contains(f.key))
                                .ToList();

            var toCreate = obj.Select(f => f.Key).Except(alreadyCreate.Select(f => f.key));

            foreach (var create in obj.Where(f => toCreate.Contains(f.Key)))
            {
                await scope.GetRepository <VkRepost>().CreateAsync(new VkRepost
                {
                    Status     = VkRepostStatus.New,
                    MessageId  = create.Id,
                    DateStatus = DateTime.Now,
                    OwnerId    = create.OwnerId,
                    UserId     = userId
                });
            }

            await scope.CommitAsync();
        }
示例#6
0
        public async Task HandleAsync([NotNull] RepostCreated obj, CancellationToken token = default)
        {
            if (obj.UserId == default)
            {
                return;
            }

            await using var scope = await _transactionScopeFactory.CreateAsync(token);

            var message = await scope.Query <VkRepost>()
                          .Where(q =>
                                 q.UserId == obj.UserId &&
                                 obj.OwnerId == q.OwnerId &&
                                 q.Status == VkRepostStatus.Pending || q.Status == VkRepostStatus.New &&
                                 q.MessageId == obj.Id
                                 )
                          .Select(a => new VkRepost {
                Id = a.Id, RowVersion = a.RowVersion
            })
                          .FirstOrDefaultAsync(token);

            if (message == null)
            {
                return;
            }

            message.Status     = VkRepostStatus.Complete;
            message.DateStatus = DateTime.Now;

            await scope.GetRepository <VkRepost>().UpdateProperties(message,
                                                                    repost => new { repost.Status, repost.DateStatus }, CancellationToken.None);

            await scope.CommitAsync(token);

            await Task.WhenAll(
                _messageBroker.PublishAsync(
                    new NotifyUserEvent
            {
                UserId    = obj.UserId,
                MessageId = message.MessageId,
                OwnerId   = message.OwnerId
            },
                    token),
                _messageBroker.SendAsync(new ExecuteNextRepost
            {
                UserId = obj.UserId
            }, token)
                );
        }
示例#7
0
        public async Task HandleAsync(ExecuteNextRepost obj, CancellationToken token = default)
        {
            try
            {
                await using var transaction = await _transactionScopeFactory.CreateAsync(token);

                var message = await transaction
                              .Query <VkRepost>()
                              .OrderBy(f => f.DateStatus)
                              .Where(f => f.Status == VkRepostStatus.New && f.UserId == obj.UserId)
                              .TagWith("Select next repost message")
                              .Select(x => new VkRepost
                {
                    Id         = x.Id,
                    RowVersion = x.RowVersion,
                    OwnerId    = x.OwnerId,
                    MessageId  = x.MessageId
                })
                              .FirstOrDefaultAsync(token);

                if (message == null)
                {
                    return;
                }

                message.Status     = VkRepostStatus.Pending;
                message.DateStatus = DateTime.Now;

                await transaction.GetRepository <VkRepost>()
                .UpdateProperties(message, repost => new { repost.Status, repost.DateStatus }, token);

                await transaction.CommitAsync(token) !;

                _jobService.Schedule <ISchedulerJobService>(
                    client => client.Repost(message.OwnerId, message.MessageId, obj.UserId),
                    TimeSpan.FromSeconds(10));
            }
            catch (Exception e)
            {
                await _messageBroker.SendAsync(new ExecuteNextRepost { UserId = obj.UserId }, token);

                _logger.LogError(e, $"Error during consuming {nameof(RepostUserEventHandler)}");
            }
        }