Exemple #1
0
        public async Task <IActionResult> Order([FromQuery] string items)
        {
            var cartItems = JsonConvert.DeserializeObject <IEnumerable <CartItemModel> >(WebUtility.UrlDecode(items));

            var user = await _userManager.GetUserAsync(User);

            var order = new Order
            {
                Date       = DateTime.Now,
                User       = user,
                OrderItems = new List <OrderItem>(),
            };

            foreach (var cartItem in cartItems)
            {
                var good = _context.Good.First(good => good.Id == cartItem.Id);

                good.Amount -= cartItem.Amount;
                if (good.Amount > 0)
                {
                    _context.Update(good);
                }
                else
                {
                    _context.Remove(good);
                }

                order.OrderItems.Add(new OrderItem
                {
                    Description = good.Description,
                    Amount      = cartItem.Amount,
                    Price       = good.Price,
                });
            }

            _context.Add(order);

            await _context.SaveChangesAsync();

            TempData.AddNotificationMessage(new Notification {
                Type = NotificationType.Success, Message = "Order successfully created"
            });

            return(LocalRedirect("/"));
        }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl       = returnUrl ?? Url.Content("~/");
            ExternalLogins  = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            RolesSelectList = GetRolesSelectList();
            if (ModelState.IsValid)
            {
                var user = new IdentityUser {
                    UserName = Input.Email, Email = Input.Email
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    //code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    //var callbackUrl = Url.Page(
                    //    "/Account/ConfirmEmail",
                    //    pageHandler: null,
                    //    values: new { area = "Identity", userId = user.Id, code = code },
                    //    protocol: Request.Scheme);

                    //await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                    //    $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    //if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    //{
                    //    return RedirectToPage("RegisterConfirmation", new { email = Input.Email });
                    //}
                    //else
                    //{
                    //    await _signInManager.SignInAsync(user, isPersistent: false);
                    //    return LocalRedirect(returnUrl);
                    //}

                    if (HttpContext.User.IsSuperUser())
                    {
                        if (await AddUserRole(user, Input.Role))
                        {
                            TempData.AddNotificationMessage(new Notification
                            {
                                Message = "User successfully created",
                                Type    = NotificationType.Success,
                            });
                            return(LocalRedirect(returnUrl));
                        }

                        return(Page());
                    }

                    if (!await AddUserRole(user, Role.User))
                    {
                        return(Page());
                    }

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(LocalRedirect(returnUrl));
                }

                SetModelErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }