Exemple #1
0
        public async Task <CommandResponse <Order> > ExecuteAsync(CreateOrderCommand command, CommandResponse <Order> previousResult)
        {
            ShoppingCart.Model.ShoppingCart cart = (await _dispatcher.DispatchAsync(new GetCartQuery {
                AuthenticatedUserId = command.AuthenticatedUserId
            })).Result;
            if (cart.Items.Count == 0)
            {
                return(CommandResponse <Order> .WithError("Shopping cart must not be empty to checkout"));
            }
            Order order = new Order
            {
                Id           = Guid.NewGuid(),
                CreatedAtUtc = DateTime.UtcNow,
                OrderItems   = cart.Items.Select(x => new OrderItem
                {
                    Name      = x.Product.Name,
                    Price     = x.Product.Price,
                    ProductId = x.Product.Id,
                    Quantity  = x.Quantity
                }).ToArray(),
                PaymentMade = false,
                PercentageDiscountApplied = 10.0,
                UserId = command.AuthenticatedUserId
            };

            order.Total = order.OrderItems.Sum(i => i.Price * i.Quantity) * (100 - order.PercentageDiscountApplied) / 100.0;
            await _repository.CreateAsync(order);

            return(CommandResponse <Order> .Ok(order));
        }
Exemple #2
0
 /// <summary>
 /// Try to locate a matching regular account (whether member or admin)
 /// </summary>
 /// <param name="input"></param>
 /// <returns></returns>
 public CommandResponse TryAuthenticate(LoginInput input)
 {
     //var candidate = _memberRepository.FindByEmail(input.UserName);
     //if (candidate == null || !_passwordService.Validate(input.Password, candidate.Password))
     //    return CommandResponse.Fail();
     if (input.UserName.EqualsAny(input.Password))
     {
         return(CommandResponse.Ok());
     }
     return(CommandResponse.Fail());
 }
Exemple #3
0
        public async Task <JsonResult> TrySignIn(LoginInput input)
        {
            var response = _service.TryAuthenticate(input);

            if (response.Success)
            {
                var redirectUrl = input.ReturnUrl.IsNullOrWhitespace() ? "/" : input.ReturnUrl;
                await HttpContext.AuthenticateUser(input.UserName, "guest", input.RememberMe);

                return(Json(CommandResponse.Ok().AddRedirectUrl(redirectUrl)));
            }

            return(Json(CommandResponse.Fail().AddMessage(response.Message)));
        }
Exemple #4
0
        public async Task ExecuteGenericCommandWithCommandResponseWithNoResultGeneratesOkResponse()
        {
            Mock <ICommandDispatcher> dispatcher = new Mock <ICommandDispatcher>();

            dispatcher.Setup(x => x.DispatchAsync(It.IsAny <SimpleCommandCommandResponse>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new CommandResult <CommandResponse>(CommandResponse.Ok(), false));
            TestSubjectController controller = new TestSubjectController(dispatcher.Object);

            IActionResult result = await controller.ExecuteCommandProxy <SimpleCommandCommandResponse>();

            OkResult castResult = (OkResult)result;

            Assert.Equal(200, castResult.StatusCode);
        }
Exemple #5
0
 public IActionResult Scalar([FromBody] Command command)
 {
     return(this.Intercept(() =>
     {
         try
         {
             var results = dbConnection.ExecuteScalars <long>(command.Commands);
             return Ok(CommandResponse.Ok(results));
         }
         catch (Exception err)
         {
             return Ok(CommandResponse.Failed(err.Message));
         }
     }));
 }
Exemple #6
0
 public IActionResult Command([FromBody] Command command)
 {
     return(this.Intercept(() =>
     {
         try
         {
             dbConnection.ExecuteCommands(command.Commands);
             return Ok(CommandResponse.Ok());
         }
         catch (Exception err)
         {
             return Ok(CommandResponse.Failed(err.Message));
         }
     }));
 }
        public async Task <CommandResponse <Model.ShoppingCart> > ExecuteAsync(GetCartQuery command, CommandResponse <Model.ShoppingCart> previousResult)
        {
            _logger.LogInformation("Getting basket for user {0}", command.AuthenticatedUserId);
            try
            {
                Model.ShoppingCart cart = await _repository.GetActualOrDefaultAsync(command.AuthenticatedUserId);

                _logger.LogInformation("Retrieved cart for user {0} with {1} items", command.AuthenticatedUserId, cart.Items.Count);
                return(CommandResponse <Model.ShoppingCart> .Ok(cart));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Unable to get basket for user {0}", command.AuthenticatedUserId);
                return(CommandResponse <Model.ShoppingCart> .WithError("Unable to get basket"));
            }
        }
Exemple #8
0
        public async Task <CommandResponse <Order> > ExecuteAsync(CreateOrderCommand command, CommandResponse <Order> previousResult)
        {
            _logger.LogInformation("Creating order for user {0} from basket", command.AuthenticatedUserId);
            try
            {
                ShoppingCart.Model.ShoppingCart cart = (await _dispatcher.DispatchAsync(new GetCartQuery {
                    AuthenticatedUserId = command.AuthenticatedUserId
                })).Result;
                if (cart.Items.Count == 0)
                {
                    return(new CommandResponse <Order> {
                        ErrorMessage = "Shopping cart must not be empty to checkout", IsSuccess = false
                    });
                }
                Order order = new Order
                {
                    Id           = Guid.NewGuid(),
                    CreatedAtUtc = DateTime.UtcNow,
                    OrderItems   = cart.Items.Select(x => new OrderItem
                    {
                        Name      = x.Product.Name,
                        Price     = x.Product.Price,
                        ProductId = x.Product.Id,
                        Quantity  = x.Quantity
                    }).ToArray(),
                    PaymentMade = false,
                    PercentageDiscountApplied = 10.0,
                    UserId = command.AuthenticatedUserId
                };
                order.Total = order.OrderItems.Sum(i => i.Price * i.Quantity) * (100 - order.PercentageDiscountApplied) / 100.0;
                await _repository.CreateAsync(order);

                _logger.LogInformation("Created order for user {0} from basket", command.AuthenticatedUserId);
                return(CommandResponse <Order> .Ok(order));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Unable to create order for user {0}", command.AuthenticatedUserId);
                throw new CheckoutException($"Unable to create order for user {command.AuthenticatedUserId}");
            }
        }
Exemple #9
0
        public async Task <CommandResponse <Model.ShoppingCart> > ExecuteAsync(GetCartQuery command, CommandResponse <Model.ShoppingCart> previousResult)
        {
            Model.ShoppingCart cart = await _repository.GetActualOrDefaultAsync(command.AuthenticatedUserId);

            return(CommandResponse <Model.ShoppingCart> .Ok(cart));
        }
Exemple #10
0
 public async Task <CommandResponse <StoreProduct> > ExecuteAsync(GetStoreProductQuery command, CommandResponse <StoreProduct> previousResult)
 {
     return(CommandResponse <StoreProduct> .Ok(await _repository.GetAsync(command.ProductId)));
 }
Exemple #11
0
 public CommandResponse ResetPassword(string email)
 {
     return(CommandResponse.Ok());
 }