Exemplo n.º 1
0
        public async Task <IActionResult> CreateBehaviorAsync([FromBody] CreateBehaviorDto request, CancellationToken cancellationToken = default)
        {
            try
            {
                await _behaviorAppService.DispatchBehavior(request, cancellationToken);

                return(Created("order/confirmation", null)); // 201
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }
Exemplo n.º 2
0
        public async Task CreateBehavior_Should_Succeed()
        {
            // Arrange
            string ip        = "127.0.0.1";
            string pageName  = "home";
            string userAgent = "safari";
            var    dto       = new CreateBehaviorDto {
                IP = ip, PageName = pageName, UserAgent = userAgent
            };

            // Act
            var       service = _behaviorAppService.Object;
            var       task    = Task.Run(() => service.CreateBehaviorAsync(dto));
            Exception ex      = await Record.ExceptionAsync(async() => await task);

            // Assert
            Assert.Null(ex);
        }
Exemplo n.º 3
0
        public async Task CreateBehaviorAsync(CreateBehaviorDto dto, CancellationToken cancellationToken = default)
        {
            try
            {
                var command  = dto.Assemble();
                var behavior = new Behavior(command);

                // create behavior in MSSQL
                await _behaviorRepository("MSSQL").CreateBehavior(behavior, cancellationToken);

                // create behavior in Couchbase
                await _behaviorRepository("Couch").CreateBehavior(behavior, cancellationToken);
            }
            catch (Exception ex)
            {
                throw new CreateBehaviorException(
                          $"An error occurred when trying to create behavior for IP {dto.IP} and page name {dto.PageName}. See inner exception for details.",
                          ex);
            }
        }
Exemplo n.º 4
0
        public async Task DispatchBehavior(CreateBehaviorDto dto, CancellationToken cancellationToken = default)
        {
            try
            {
                var command  = dto.Assemble();
                var behavior = new Behavior(command);

                // dispatch domain event
                var @event = new BehaviorCreatedEvent(behavior.Id,
                                                      behavior.IP,
                                                      behavior.PageName,
                                                      behavior.UserAgent,
                                                      behavior.PageParameters);
                await _messageService.PublishAsync("behavior_created", @event, cancellationToken);
            }
            catch (Exception ex)
            {
                throw new CreateBehaviorException(
                          $"An error occurred when trying to create behavior for IP {dto.IP} and page name {dto.PageName}. See inner exception for details.",
                          ex);
            }
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            // start DI container
            InitializeDIContainer();

            // create a provider containing all services which exists whithin IServiceCollection instance.i
            var serviceProvider = services.BuildServiceProvider();

            using (IMessageService messageService = new MessageService(serviceProvider.GetService <RabbitMQConfiguration>()))
            {
                // consume the queue from RabbitMQ
                Task.Run(() => messageService.ConsumeAsync(queueName, (s, e) =>
                {
                    try
                    {
                        var content = Encoding.UTF8.GetString(e.Body);
                        var result  = JsonConvert.DeserializeObject <BehaviorCreatedEvent>(content);

                        // persists the new behavior inside every database with the domain application service
                        IBehaviorAppService behaviorAppService = serviceProvider.GetService <IBehaviorAppService>();
                        var createBehaviorDto = new CreateBehaviorDto
                        {
                            IP             = result.IP,
                            PageName       = result.PageName,
                            UserAgent      = result.UserAgent,
                            PageParameters = result.PageParameters
                        };

                        Task.Run(() =>
                        {
                            try
                            {
                                behaviorAppService.CreateBehaviorAsync(createBehaviorDto);

                                Console.WriteLine($"New behavior created for IP: {result.IP}.");
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine($"Error when creating behavior: {ex.Message}");
                            }
                        });
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Error when pulling from queue: {ex.Message}");
                    }
                }));

                Console.WriteLine("Waiting for created behaviors..");

                Console.CancelKeyPress += (o, e) =>
                {
                    Console.WriteLine("Leaving..");

                    _waitHandle.Set();
                    e.Cancel = true;
                };

                _waitHandle.WaitOne();
            }
        }