Exemplo n.º 1
0
        public async Task <IActionResult> Checkout(CheckoutViewModel model)
        {
            var session = HttpContext.Session.Get <List <ShoppingCartViewModel> >(CommonConstants.CartSession);

            if (ModelState.IsValid)
            {
                if (session != null)
                {
                    var details = new List <BillDetailViewModel>();
                    foreach (var item in session)
                    {
                        details.Add(new BillDetailViewModel()
                        {
                            Price     = item.Price,
                            ColorId   = item.Color.Id,
                            SizeId    = item.Size.Id,
                            Quantity  = item.Quantity,
                            ProductId = item.Product.Id
                        });
                    }

                    var billViewModel = new BillViewModel()
                    {
                        CustomerMobile  = model.CustomerMobile,
                        BillStatus      = BillStatus.New,
                        CustomerAddress = model.CustomerAddress,
                        CustomerName    = model.CustomerName,
                        CustomerMessage = model.CustomerMessage,
                        BillDetails     = details,
                        Status          = Status.Active,
                        ShipperId       = 1
                    };

                    if (User.Identity.IsAuthenticated == true)
                    {
                        billViewModel.CustomerId = Guid.Parse(User.GetSpecificClaim("UserId"));
                    }

                    var notificationId = Guid.NewGuid().ToString();


                    var announcement = new AnnouncementViewModel()
                    {
                        Title       = "New bill",
                        DateCreated = DateTime.Now,
                        UserId      = Guid.Parse(User.GetSpecificClaim("UserId")),
                        Content     = $"New bill has been created",
                        Id          = notificationId
                    };

                    var users = await _userService.AnnouncementUsers("BILL");

                    var announUsers = new List <AnnouncementUserViewModel>();

                    foreach (var item in users)
                    {
                        announUsers.Add(new AnnouncementUserViewModel()
                        {
                            AnnouncementId = notificationId,
                            HasRead        = false,
                            UserId         = (Guid)item.Id
                        });
                    }

                    announcement.AnnouncementUsers = announUsers;

                    _billService.Create(billViewModel, announcement);

                    try
                    {
                        _billService.Save();

                        //var content = await _viewRenderService.RenderToStringAsync("Cart/_BillMail", billViewModel);
                        ////Send mail
                        //await _emailSender.SendEmailAsync(_configuration["MailSettings:AdminMail"], "New bill from ShopMart", content);

                        ViewData["Success"] = true;

                        await _hubContext.Clients.All.SendAsync("ReceiveMessage", announcement);
                    }
                    catch (Exception ex)
                    {
                        ViewData["Success"] = false;
                        ModelState.AddModelError("", ex.Message);
                    }
                }
            }

            model.Carts = session;
            return(View(model));
        }