public async Task <IActionResult> Edit(int id, [Bind("CustomerId,LastUpdate")] Customer customer)
        {
            if (!_authenticationService.isUserAdmin(Request))
            {
                return(RedirectToAction(nameof(AccountController.AccessDenied), "Account"));
            }

            if (id != customer.CustomerId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(customer);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CustomerExists(customer.CustomerId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(customer));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> AddToWishlist(int?productId, string productSource)
        {
            if (productId == null || productSource == null)
            {
                return(NotFound());
            }

            var customerUser = _authenticationService.GetCurrentlyLoggedInUser(Request);

            var wishlist = new Wishlist
            {
                CustomerId     = customerUser.User.CustomerId,
                CustomerSource = customerUser.User.CustomerSource,
                ProductId      = productId.Value,
                ProductSource  = productSource,
            };

            _context.Add(wishlist);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException e)
            {
                Console.WriteLine(e);
            }

            return(RedirectToAction("Index", "Wishlists"));
        }
        public async Task <IActionResult> Create([Bind("AddressId,State,Zip,City,Street,House,LastUpdate")] Address address, [Bind("ReturnUrl")] string returnUrl = null)
        {
            if (ModelState.IsValid)
            {
                var temp = _authenticationService.GetCurrentlyLoggedInUser(Request);
                if (temp == null)
                {
                    throw new ApplicationException($"Unable to load user with ID");
                }

                using (var contextTransaction = _context.Database.BeginTransaction())
                {
                    _context.Add(address);
                    await _context.SaveChangesAsync();

                    var customer = _context.Customer.FindAsync(temp.Customer.CustomerId);
                    customer.Result.CustomerAddress.Add(new CustomerAddress()
                    {
                        Address = address
                    });
                    await _context.SaveChangesAsync();

                    contextTransaction.Commit();
                }

                if (!string.IsNullOrWhiteSpace(returnUrl))
                {
                    return(Redirect(returnUrl));
                }
            }
            return(View(address));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Create([Bind("CustomerId,CustomerSource,ProductId,ProductSource,LastUpdate")] Wishlist wishlist)
        {
            if (ModelState.IsValid)
            {
                _context.Add(wishlist);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(wishlist));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> ChangePassword(ChangePasswordViewModel model)
        {
            var customerUser = _authenticationService.GetCurrentlyLoggedInUser(Request);

            if (customerUser == null)
            {
                return(RedirectToAction(nameof(AccountController.Login), "Account"));
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            try
            {
                User user = _context.User.Where(u => u.UserId == customerUser.User.UserId).ToList().FirstOrDefault();

                if (user != null)
                {
                    user.Password = model.NewPassword;
                }

                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                ModelState.AddModelError(string.Empty, "Could not update password");
            }

            StatusMessage = "Your password has been changed.";

            return(RedirectToAction(nameof(ChangePassword)));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Create([Bind("CategoryId,Name,LastUpdate")] Category category)
        {
            if (!_authenticationService.isUserAdmin(Request))
            {
                return(RedirectToAction(nameof(AccountController.AccessDenied), "Account"));
            }

            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
        public async Task <IActionResult> Create([Bind("SupplierId,Name,AddressId,LastUpdate")] Supplier supplier)
        {
            if (!CheckAuthentication(Request, Response))
            {
                return(RedirectToAction(nameof(AccountController.AccessDenied), "Account"));
            }

            if (ModelState.IsValid)
            {
                _context.Add(supplier);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AddressId"] = new SelectList(_context.Address, "AddressId", "City", supplier.AddressId);
            return(View(supplier));
        }
        public async Task <IActionResult> PlaceOrder()
        {
            var customerUser = _authenticationService.GetCurrentlyLoggedInUser(Request);

            if (customerUser == null)
            {
                return(RedirectToAction(nameof(AccountController.Login), "Account"));
            }

            var shoppingCart = (from s in _context.ShoppingCart
                                where s.CustomerId == customerUser.User.CustomerId && s.CustomerSource == customerUser.User.CustomerSource
                                select s).ToList();

            var order = new Order
            {
                CustomerId     = customerUser.User.CustomerId,
                CustomerSource = customerUser.User.CustomerSource
            };

            _context.Order.Add(order);

            foreach (var item in shoppingCart)
            {
                var orderProduct = new OrderProduct
                {
                    OrderId       = order.OrderId,
                    ProductSource = item.ProductSource,
                    ProductId     = item.ProductId,
                    Quantity      = item.ProductQuantity
                };

                _context.OrderProduct.Add(orderProduct);
            }

            _context.ShoppingCart.RemoveRange(shoppingCart.ToArray());

            await _context.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }