public async Task <IActionResult> Index(IndexViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            ApplicationUser user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            if (model.User.Email != user.Email)
            {
                var setEmailResult = await _userManager.SetEmailAsync(user, model.User.Email);

                if (!setEmailResult.Succeeded)
                {
                    throw new ApplicationException($"Unexpected error occurred setting email for user with ID '{user.Id}'.");
                }
            }

            if (model.User.PhoneNumber != user.PhoneNumber)
            {
                var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, model.User.PhoneNumber);

                if (!setPhoneResult.Succeeded)
                {
                    throw new ApplicationException($"Unexpected error occurred setting phone number for user with ID '{user.Id}'.");
                }
            }

            Address newAddress = await _addressService.AddAddressAsync(new Address()
            {
                City          = model.User.Address.City,
                PostalCode    = model.User.Address.PostalCode,
                StreetAddress = model.User.Address.StreetAddress,
                FirstName     = model.User.Address.FirstName,
                Surname       = model.User.Address.Surname
            });

            user.AddressId = newAddress.AddressId;

            await _userManager.UpdateAsync(user);

            StatusMessage = "Your profile has been updated";
            return(RedirectToAction(nameof(Index)));
        }
        private async Task CreateOrderAsync(bool payByCard)
        {
            //get cart, delivery address, phone number
            List <CartItem> cart = _cartService.GetCart();

            //fetch address from temporary storage and create a db entry
            Address address = _checkoutService.GetAddressFromSession();

            address = await _addressService.AddAddressAsync(address);

            await _checkoutService.CreateOrderAsync(cart, address, payByCard);

            _cartService.EmptyCart();
        }
Beispiel #3
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                Address userAddress = await _addressService.AddAddressAsync(
                    new Address()
                {
                    City          = model.City,
                    StreetAddress = model.Address,
                    PostalCode    = model.PostalCode,
                    FirstName     = model.FirstName,
                    Surname       = model.Surname
                }
                    );

                var user = new ApplicationUser {
                    UserName    = model.Email,
                    Email       = model.Email,
                    AddressId   = userAddress.AddressId,
                    PhoneNumber = model.PhoneNumber
                };
                var result = await _userManager.CreateAsync(user, model.Password);

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

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
                    await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);

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

                    _logger.LogInformation("User created a new account with password.");
                    return(RedirectToLocal(returnUrl));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Beispiel #4
0
        public async void Create_Order(bool customisedItems)
        {
            //Assemble
            List <CartItem> cart = await CreateCart(customisedItems);

            Address address = await _addressService.AddAddressAsync(new Address()
            {
                City = "aaa", FirstName = "bbb", Surname = "ccc", PostalCode = "ddd", StreetAddress = "eee"
            });

            //Act
            await _checkoutService.CreateOrderAsync(cart, address, false);

            //Assert
            Order order = _context.Orders.Include(x => x.DishOrders).SingleOrDefault();

            Assert.NotNull(order);
            Assert.True(order.DishOrders.Count == 3);
        }
Beispiel #5
0
        public static void Initialise(WebshopDbContext context, UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager, AddressService addressService)
        {
            if (context.Roles.Count() == 0)
            {
                IdentityRole adminRole = new IdentityRole() { Name = "Admin" };
                roleManager.CreateAsync(adminRole).Wait();
            }

            if (context.Addresses.Count() == 0)
            {
                Address seededUserAddress = addressService.AddAddressAsync(new Address()
                {
                    StreetAddress = "Hemvägen 5",
                    City = "Huddinge",
                    PostalCode = "14140",
                    FirstName = "Ture",
                    Surname = "Testare"
                }).Result;

                Address adminUserAddress = addressService.AddAddressAsync(new Address()
                {
                    StreetAddress = "Bortgången 3",
                    City = "Molnet",
                    PostalCode = "14141",
                    FirstName = "Admin",
                    Surname = "Admin"
                }).Result;
            }

            if (context.Users.Count() == 0)
            {
                ApplicationUser seededUser = new ApplicationUser()
                {
                    UserName = "******",
                    Email = "*****@*****.**",
                    PhoneNumber = "0701234567",
                    AddressId = 1
                };
                ApplicationUser adminUser = new ApplicationUser()
                {
                    UserName = "******",
                    Email = "*****@*****.**",
                    PhoneNumber = "0707654321",
                    AddressId = 2
                };

                userManager.CreateAsync(seededUser, "Test12#").Wait();
                userManager.CreateAsync(adminUser, "PassWeCan2!").Wait();
                userManager.AddToRoleAsync(adminUser, "Admin").Wait();
            }

            if (context.Dishes.Count() == 0)
            {
                List<Category> categories = new List<Category>() {
                    new Category() { Name = "Pizza", Active = true },
                    new Category() { Name = "Sallad", Active = true },
                    new Category() { Name = "Tillbehör", Active = true },
                    new Category() { Name = "Kebab", Active = true },
                    new Category() { Name = "Övrigt", Active = true }
                };

                List<Ingredient> ingredients = new List<Ingredient>() {
                    new Ingredient() { Name = "Skinka", Price = 10 },
                    new Ingredient() { Name = "Ananas", Price = 10 },
                    new Ingredient() { Name = "Champinjoner", Price = 10 },
                    new Ingredient() { Name = "Oxfilé", Price = 15 }
                };
                List<Dish> dishes = new List<Dish>() {
                    new Dish() { Name = "Margherita", Price = 58, Description = "Den grundläggande pizzan, utan extra pålägg.", CategoryId = 1 },
                    new Dish() { Name = "Vesuvio", Price = 58, Description = "Med skinka på.", CategoryId = 1 },
                    new Dish() { Name = "Hawaii", Price = 58, Description = "Med ananas och skinka.", CategoryId = 1 },
                    new Dish() { Name = "Capricciosa", Price = 58, Description = "En klassiker med champinjoner och skinka.", CategoryId = 1 }
                };
                List<DishIngredient> dishIngredients = new List<DishIngredient>() {
                    new DishIngredient() { Dish = dishes[1], Ingredient = ingredients[0] },
                    new DishIngredient() { Dish = dishes[2], Ingredient = ingredients[0] },
                    new DishIngredient() { Dish = dishes[2], Ingredient = ingredients[1] },
                    new DishIngredient() { Dish = dishes[3], Ingredient = ingredients[0] },
                    new DishIngredient() { Dish = dishes[3], Ingredient = ingredients[2] }
                };

                context.Categories.AddRange(categories);
                context.Ingredients.AddRange(ingredients);
                context.Dishes.AddRange(dishes);
                context.DishIngredients.AddRange(dishIngredients);
            }

            context.SaveChanges();
        }