Ejemplo n.º 1
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));
        }
Ejemplo n.º 3
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));
        }
        public ActionResult Rate(int prodId, string source, int rating)
        {
            var loggedInUser = _authenticationService.GetCurrentlyLoggedInUser(Request);

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

            try
            {
                //Check to see if this product id exists in the view first
                var prod = _context.ProductsView.FirstOrDefault(p => p.ProductId == prodId && p.Source == source);
                if (prod == null)
                {
                    return(NotFound());
                }

                //did this user already rate this product?
                Rating ratingObject = _context.Rating.FirstOrDefault(r =>
                                                                     r.CustomerId == loggedInUser.Customer.CustomerId && r.ProductId == prodId && r.ProductSource == source);
                if (ratingObject == null)
                {
                    //Make a new rating then add it to the context
                    ratingObject = new Rating()
                    {
                        CustomerId     = loggedInUser.Customer.CustomerId,
                        CustomerSource = loggedInUser.User.CustomerSource,
                        ProductId      = prodId,
                        ProductSource  = source,
                        Rating1        = rating
                    };
                    _context.Add(ratingObject);
                }
                else
                {
                    //update existing rating
                    ratingObject.Rating1 = rating;
                }

                //save our changes
                _context.SaveChanges();
            }
            catch
            {
                //throw some error??
            }
            return(RedirectToAction("Index", "Products"));;
        }
Ejemplo n.º 5
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));
        }