Ejemplo n.º 1
0
        public async Task EnqueueAsync(ICommand command)
        {
            var connection = await _sqlConnectionFactory.GetOpenConnectionAsync();

            string insertQuery = @"INSERT INTO [users].[InternalCommands] (Id, EnqueueDate, Type, Data)
                                   VALUES
                                   (@Id, @EnqueueDate, @Type, @Data)";

            await connection.ExecuteAsync(insertQuery, new
            {
                command.Id,
                EnqueueDate = DateTimeOffset.UtcNow,
                Type        = command.GetType().FullName,
                Data        = JsonConvert.SerializeObject(command, new JsonSerializerSettings
                {
                    ContractResolver = new AllPropertiesContractResolver()
                })
            });
        }
Ejemplo n.º 2
0
        public async Task <Unit> Handle(ProcessInboxCommand command, CancellationToken cancellationToken)
        {
            var connection = await _sqlConnectionFactory.GetOpenConnectionAsync();

            string sql = $@"SELECT [InboxMessage].[Id] AS [{nameof(InboxMessageDto.Id)}], 
                            [InboxMessage].[Type] AS [{nameof(InboxMessageDto.Type)}],
                            [InboxMessage].[Data] AS [{nameof(InboxMessageDto.Data)}] 
                            FROM [users].[InboxMessages] AS [InboxMessage] 
                            WHERE [InboxMessage].[ProcessedDate] IS NULL 
                            ORDER BY [InboxMessage].[OccurredOn]";

            var messages = await connection.QueryAsync <InboxMessageDto>(sql);

            const string sqlUpdateProcessedDate = @"UPDATE [users].[InboxMessages] 
                                                  SET [ProcessedDate] = @Date 
                                                  WHERE [Id] = @Id";

            foreach (var message in messages)
            {
                var messageAssembly = AppDomain.CurrentDomain.GetAssemblies()
                                      .SingleOrDefault(assembly => message.Type.Contains(assembly.GetName().Name));

                if (messageAssembly == null)
                {
                    continue;
                }

                var type    = messageAssembly.GetType(message.Type);
                var request = JsonConvert.DeserializeObject(message.Data, type);

                try
                {
                    await _mediator.Publish((INotification)request, cancellationToken);
                }
                catch (Exception e)
                {
                    _logger.Error(e, "Error in ProcessInboxCommandHandler");
                    throw;
                }

                await connection.ExecuteAsync(sqlUpdateProcessedDate, new
                {
                    Date = DateTimeOffset.UtcNow,
                    Id   = message.Id
                });
            }

            return(Unit.Value);
        }
        public async Task <AuthenticationResult> Handle(AuthenticateCommand request, CancellationToken cancellationToken)
        {
            var connection = await _sqlConnectionFactory.GetOpenConnectionAsync();

            const string sql = "SELECT " +
                               "[User].[Id], " +
                               "[User].[Login], " +
                               "[User].[Name], " +
                               "[User].[Email], " +
                               "[User].[IsActive], " +
                               "[User].[Password] " +
                               "FROM [users].[v_Users] AS [User] " +
                               "WHERE [User].[Login] = @Login";

            var user = await connection.QuerySingleOrDefaultAsync <UserDto>(
                sql,
                new
            {
                request.Login,
            });

            if (user == null)
            {
                return(new AuthenticationResult("Incorrect login or password"));
            }

            if (!user.IsActive)
            {
                return(new AuthenticationResult("User is not active"));
            }

            if (!PasswordManager.VerifyHashedPassword(user.Password, request.Password))
            {
                return(new AuthenticationResult("Incorrect login or password"));
            }

            user.Claims = new List <Claim>();
            user.Claims.Add(new Claim(CustomClaimTypes.Name, user.Name));
            user.Claims.Add(new Claim(CustomClaimTypes.Email, user.Email));

            return(new AuthenticationResult(user));
        }
Ejemplo n.º 4
0
        public async Task SendEmailAsync(EmailMessage message)
        {
            var sqlConnection = await _sqlConnectionFactory.GetOpenConnectionAsync();

            await sqlConnection.ExecuteScalarAsync(
                "INSERT INTO [app].[Emails] ([Id], [From], [To], [Subject], [Content], [Date]) " +
                "VALUES (@Id, @From, @To, @Subject, @Content, @Date) ",
                new
            {
                Id   = Guid.NewGuid(),
                From = _configuration.FromEmail,
                message.To,
                message.Subject,
                message.Content,
                Date = DateTime.UtcNow
            });

            _logger.Information(
                "Email sent. From: {From}, To: {To}, Subject: {Subject}, Content: {Content}.",
                _configuration.FromEmail,
                message.To,
                message.Subject,
                message.Content);
        }
Ejemplo n.º 5
0
        public async Task <List <EmailDto> > Handle(GetAllEmailsQuery query, CancellationToken cancellationToken)
        {
            var connection = await _sqlConnectionFactory.GetOpenConnectionAsync();

            return((await connection.QueryAsync <EmailDto>(