Example #1
0
        public async Task <string> CreateAsync(OrderCreateServiceModel model)
        {
            if (!IsEntityStateValid(model))
            {
                return(null);
            }

            var user = await this.Context.Users.SingleOrDefaultAsync(u => u.UserName == model.UserName);

            if (user == null ||
                !await this.Context.Products.AnyAsync(b => b.Id == model.ProductId))
            {
                return(null);
            }

            var order = new Order
            {
                ProductId     = model.ProductId,
                UserId        = user.Id,
                CreatedOn     = model.CreatedOn,
                PaymentStatus = PaymentStatus.Pending
            };

            await this.Context.Orders.AddAsync(order);

            await this.Context.SaveChangesAsync();

            return(order.Id);
        }
Example #2
0
        public async Task <IActionResult> OrderProduct(string productId, string paymentMethod)
        {
            if (productId == null)
            {
                return(this.RedirectToAction("Index", "Home"));
            }

            var serviceModel = new OrderCreateServiceModel
            {
                ProductId = productId,
                CreatedOn = DateTime.UtcNow,
                UserName  = this.User.Identity.Name
            };

            string orderId = await this.ordersService.CreateAsync(serviceModel);

            if (orderId == null)
            {
                return(this.RedirectToAction("Index", "Home"));
            }

            if (paymentMethod == DirectPaymentMethodName)
            {
                return(this.RedirectToAction("Pay", "DirectPayments", new { id = orderId }));
            }

            if (paymentMethod == CardPaymentMethodName)
            {
                return(this.RedirectToAction("Pay", "CardPayments", new { id = orderId }));
            }

            return(this.RedirectToAction("My"));
        }