예제 #1
0
        public async Task CreateAddress_ShouldCreateAddressToUser()
        {
            string onFalseErrorMessage = "Method does not create a user address correctly.";

            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            var addressService = new AddressService(context);

            await this.SeedTestUser(context);

            AddressCreateBindingModel model = new AddressCreateBindingModel
            {
                AddressText = "AddressText Test 123",
                FirstName   = "Test",
                LastName    = "Testing",
                PhoneNumber = "0001112333",
            };

            // Gets the user and creates the address using the model and the user's Id
            var user = this.GetUser();
            await addressService.CreateAddress(model, user.Id);

            var resultAddress = await context.Addresses.FirstAsync();

            Assert.True(
                resultAddress.ApplicationUserId == user.Id &&
                resultAddress.AddressText == model.AddressText,
                onFalseErrorMessage);
        }
        public async Task <IActionResult> Add(AddressCreateBindingModel addressCreate)
        {
            string userId = this.userManager.GetUserId(this.HttpContext.User);

            await this.addressService.CreateAddress(addressCreate, userId);

            return(this.RedirectToAction("Index", "Orders"));
        }
예제 #3
0
        public async Task <bool> CreateAddress(AddressCreateBindingModel addressCreate, string userId)
        {
            var address = AutoMapper.Mapper.Map <Address>(addressCreate);

            address.ApplicationUserId = userId;

            await this.context.Addresses.AddAsync(address);

            var result = await this.context.SaveChangesAsync();

            return(result > 0);
        }
        public async Task <IActionResult> Index()
        {
            var user = await this.userManager.GetUserAsync(this.User);

            var cities = await this.cityService.GetAllCities();

            var userAddresses = await this.addressService.GetAllUserAddresses(user.Id);

            var cartProducts = await this.shoppingCartService.GetCartProductsByUserId(user.Id);

            // If there are no products in the cart the user will be redirected
            if (cartProducts.Count() == 0)
            {
                return(this.RedirectToAction("Index", "Home"));
            }

            var totalPrice = cartProducts.Sum(p => p.TotalPrice) + GlobalConstants.DeliveryFee;

            AddressCreateBindingModel addressBindingModel = new AddressCreateBindingModel
            {
                PhoneNumber = user.PhoneNumber,
                Cities      = cities,
            };

            OrderBindingModel orderBindingModel = new OrderBindingModel
            {
                Products          = cartProducts.ToList(),
                ApplicationUserId = user.Id,
                TotalPrice        = totalPrice,
                DeliveryFee       = GlobalConstants.DeliveryFee,
            };

            CheckoutViewModel model = new CheckoutViewModel
            {
                AddressCreate = addressBindingModel,
                UserAddresses = userAddresses.ToList(),
                OrderCreate   = orderBindingModel,
            };

            return(this.View(model));
        }