Example #1
0
        public async Task OnGetAsync()
        {
            CurrentUser = await _userManager.GetUserAsync(HttpContext.User);

            float totalPrice = 0;

            Maps = await _context.ProductCartMap
                   .Include(a => a.Product)
                   .Include(a => a.Cart)
                   .ThenInclude(a => a.Customer)
                   .Where(a => a.Cart.Customer.Id == CurrentUser.Id)
                   .ToListAsync();

            foreach (var map in Maps)
            {
                map.ProductTotalPrice = map.Product.Price * map.ProductQuantity;
                totalPrice           += map.ProductTotalPrice;
            }

            if (Maps.Count > 0)
            {
                Maps.First().Cart.TotalPrice = totalPrice;
                _context.Attach(Maps.First().Cart).State = EntityState.Modified;
            }
        }
Example #2
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Attach(ProductCategory).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductCategoryExists(ProductCategory.ProductCategoryId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
Example #3
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            ApplicationUser currentUser = await _userManager.GetUserAsync(HttpContext.User);

            ProductId = Convert.ToInt32(Request.Form["ProductId"]);
            var product = await _context.Product.FindAsync(ProductId);

            var cart = await _context.Cart
                       .Include(a => a.Customer)
                       .Where(a => a.UserId == currentUser.Id)
                       .SingleOrDefaultAsync();

            if (cart == null)
            {
                cart          = new Models.Cart();
                cart.UserId   = currentUser.Id;
                cart.Customer = currentUser;
            }

            cart.TotalPrice += product.Price;

            var map = await _context.ProductCartMap
                      .Where(a => a.CartId == cart.CartId)
                      .Where(a => a.ProductId == ProductId)
                      .SingleOrDefaultAsync();

            if (map == null)
            {
                map = new ProductCartMap(ProductId, product, cart.CartId, cart);
                await _context.ProductCartMap.AddAsync(map);
            }
            else
            {
                map.ProductQuantity++;
                _context.Attach(map).State = EntityState.Modified;
            }

            await _context.SaveChangesAsync();

            TempData[MessageKey] = $"Product #{product.ProductId} {product.Name}";

            return(Redirect("/Product" + Request.QueryString));
        }
Example #4
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var maps = _context.ProductPropertyMap
                       .Where(a => a.ProductId == Product.ProductId);

            _context.ProductPropertyMap.RemoveRange(maps);

            foreach (var value in _context.ProductPropertyValue)
            {
                if (Request.Form[value.Key].Count > 0)
                {
                    var map = new ProductPropertyMap(Product.ProductId, Product, value.ProductPropertyValueId, value);
                    _context.ProductPropertyMap.Add(map);
                }
            }

            _context.Attach(Product).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(Product.ProductId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
Example #5
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Order = await _context.Order
                    .Include(o => o.Customer).FirstOrDefaultAsync(m => m.OrderId == id);

            if (Order == null)
            {
                return(NotFound());
            }

            var   customer   = Order.Customer;
            float totalPrice = 0;

            Maps = await _context.ProductOrderMap
                   .Include(a => a.Product)
                   .Include(a => a.Order)
                   .ThenInclude(a => a.Customer)
                   .Where(a => a.Order.Customer.Id == customer.Id)
                   .ToListAsync();

            foreach (var map in Maps)
            {
                map.ProductTotalPrice = map.Product.Price * map.ProductQuantity;
                totalPrice           += map.ProductTotalPrice;
            }

            if (Maps.Count > 0)
            {
                Maps.First().Order.TotalPrice = totalPrice;
                _context.Attach(Maps.First().Order).State = EntityState.Modified;
            }

            return(Page());
        }