Exemplo n.º 1
0
        private async Task DoWorkAsync()
        {
            _logger.LogInformation($"{nameof(FileProcessingHostedService)} Background Service is working.");

            try
            {
                using (var scope = _serviceScopeFactory.CreateScope())
                {
                    IDomainEventService  _domainEventService    = scope.ServiceProvider.GetService <IDomainEventService>();
                    IFileRepository      fileRepository         = scope.ServiceProvider.GetService <IFileRepository>();
                    List <string>        supportFiles           = _configuration.FileTypes;
                    List <string>        supportedCategoryTypes = _configuration.SupportedCategoryTypes;
                    List <BlueprintFile> blueprintFiles         = fileRepository.GetFilesForBackgroudProcessing(supportFiles, supportedCategoryTypes, _configuration.FileProcessingHostedServiceTotalFilesProcessedPerTimes).ToList();

                    try
                    {
                        foreach (var blueprintFile in blueprintFiles)
                        {
                            await _domainEventService.Publish(new PreGenerateResizedImagesEvent(blueprintFile.Id));
                        }

                        fileRepository.UpdateRangeForBackgroudProcessing(blueprintFiles, (int)BackgroudProcessingStatus.Processed);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, $"Call to {nameof(PreGenerateResizedImagesEvent)} failed.");
                        fileRepository.UpdateRangeForBackgroudProcessing(blueprintFiles, (int)BackgroudProcessingStatus.Failed);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Call to {nameof(FileProcessingHostedService)} -- {nameof(DoWorkAsync)} failed.");
            }
        }
Exemplo n.º 2
0
 private async Task DispatchEvents(DomainEvent[] events)
 {
     foreach (var @event in events)
     {
         @event.IsPublished = true;
         await _domainEventService.Publish(@event);
     }
 }
Exemplo n.º 3
0
            public async Task <PostDto> Handle(GetPostByIdQuery request, CancellationToken cancellationToken)
            {
                var post = await _context.Posts.Include(p => p.PostTags).ThenInclude(p => p.Tag)
                           .Include(p => p.Comments)
                           .SingleOrDefaultAsync(p => p.Id == request.Id);

                await _domainEventService.Publish(new PostViewed(request.Id));

                return(_mapper.Map <PostDto>(post));
            }
Exemplo n.º 4
0
        public async Task <RegisterResponseDto> Handle(RegisterUserCommand request, CancellationToken cancellationToken)
        {
            var(Result, UserName, Email, Id) = await _identityService.RegisterUserAsync(request.Email, request.UserName, request.Password);

            if (Result.Succeeded)
            {
                await _domainEventService.Publish(new UserRegisteredEvent(Id, request.UserName, request.Email));
            }

            return(new RegisterResponseDto {
                UserName = UserName, Email = Email, Errors = new List <string>(Result.Errors)
            });
        }
Exemplo n.º 5
0
        public async Task <ChangeEmailDto> Handle(ChangeEmailCommand request, CancellationToken cancellationToken)
        {
            var result = await _identityService.ChangeEmailAsync(_currentUserService.UserId, request.Email, request.NewEmail, request.Token);

            if (result.Succeeded)
            {
                await _domainEventService.Publish(new UserEmailChangedEvent(request.Email, request.NewEmail));
            }

            return(result.Succeeded ? new ChangeEmailDto {
                IsChanged = true
            } : new ChangeEmailDto {
                IsChanged = false, Errors = result.Errors
            });
        }
Exemplo n.º 6
0
        public async Task <Unit> Handle(RemoveUserFromProjectCommand request, CancellationToken cancellationToken)
        {
            var projectAssignment = await _context.ProjectAssignments.SingleOrDefaultAsync(a =>
                                                                                           a.UserId == request.UserId && a.ProjectId == request.ProjectId, cancellationToken);

            if (projectAssignment == null)
            {
                throw new NotFoundException(
                          $"{nameof(ProjectAssignment)} with user({request.UserId} in project({request.ProjectId}) does not exists.");
            }

            _context.ProjectAssignments.Remove(projectAssignment);
            await _context.SaveChangesAsync(cancellationToken);

            await _domainEventService.Publish(new ProjectAssignmentDeletedEvent(request.UserId, request.ProjectId));

            return(Unit.Value);
        }
        private async Task DispatchEvents()
        {
            var entitiesWithEvents = ChangeTracker.Entries <IHasDomainEvent>()
                                     .Select(e => e.Entity)
                                     .Where(e => e.DomainEvents.Any())
                                     .ToArray();

            foreach (var entity in entitiesWithEvents)
            {
                var events = entity.DomainEvents.ToArray();
                entity.DomainEvents.Clear();
                foreach (var domainEvent in events)
                {
                    domainEvent.IsPublished = true;
                    await _domainEventService.Publish(domainEvent).ConfigureAwait(false);
                }
            }
        }
Exemplo n.º 8
0
        public async Task Process(TRequest request, TResponse response, CancellationToken cancellationToken)
        {
            while (true)
            {
                var domainEventEntity = _dbContext.ChangeTracker.Entries <IHasDomainEvent>()
                                        .Select(x => x.Entity.DomainEvents)
                                        .SelectMany(x => x)
                                        .Where(domainEvent => !domainEvent.IsPublished)
                                        .FirstOrDefault();
                if (domainEventEntity == null)
                {
                    break;
                }

                domainEventEntity.IsPublished = true;
                await _domainEventService.Publish(domainEventEntity);

                _logger.LogInformation("Published event: {Name}", domainEventEntity.GetType().Name);
            }
        }
        public async Task <Unit> Handle(AddXpToUserCommand request, CancellationToken cancellationToken)
        {
            var user = await _context.XPUsers
                       .FirstOrDefaultAsync(u => u.UserId == request.UserId, cancellationToken);

            if (user == null)
            {
                user = new User
                {
                    GuildId = request.GuildId,
                    UserId  = request.UserId,
                    Lvl     = 0,
                    XP      = 0
                };

                _context.XPUsers.Add(user);
            }

            var oldXP = user.XP;

            user.XP += await _calculator.XpEarned(request.Message, request.GuildId);

            await _context.SaveChangesAsync(cancellationToken);

            await _provider.Publish(new XpAddedToUserEvent
            {
                MessageDetails = new MessageDetails
                {
                    Message         = request.Message,
                    ChannelId       = request.ChannelId,
                    GuildId         = request.GuildId,
                    UserId          = request.UserId,
                    UserAvatarUrl   = request.UserAvatarUrl,
                    UserDisplayName = request.UserDisplayName,
                },
                OldXp = oldXP,
                NewXp = user.XP
            });

            return(new Unit());
        }
Exemplo n.º 10
0
        private async Task DispatchEvents()
        {
            var domainEvents = ChangeTracker
                               .Entries <IHasDomainEvent>()
                               .Select(x => x.Entity.DomainEvents)
                               .SelectMany(x => x)
                               .OrderBy(x => x.DateOccurred)
                               .Where(domainEvent => !domainEvent.IsPublished).ToList();

            if (!domainEvents.Any())
            {
                return;
            }

            foreach (var domainEvent in domainEvents)
            {
                domainEvent.IsPublished = true;

                await _domainEventService.Publish(domainEvent);
            }
        }
Exemplo n.º 11
0
        public async Task Handle(DomainEventNotification <XpAddedToUserEvent> notification, CancellationToken cancellationToken)
        {
            var user = await _context.XPUsers.FirstOrDefaultAsync(u => u.UserId == notification.DomainEvent.MessageDetails.UserId, cancellationToken);

            var oldLevel = user.Lvl;

            while (user.XP > _calculator.TotalXpRequired(user.Lvl))
            {
                user.Lvl++;
            }

            if (oldLevel < user.Lvl)
            {
                await _context.SaveChangesAsync(cancellationToken);

                await _provider.Publish(new UserLevelUpEvent
                {
                    MessageDetails = notification.DomainEvent.MessageDetails,
                    OldLvl         = oldLevel,
                    NewLvl         = user.Lvl
                });
            }
        }