Exemplo n.º 1
0
        public async Task <IActionResult> Index(IndexViewModel model)
        {
            var customerUser = _authenticationService.GetCurrentlyLoggedInUser(Request);

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

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

            var userToUpdate = _context.Customer.Where(c => c.CustomerId == customerUser.Customer.CustomerId).ToList().FirstOrDefault();

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

            try
            {
                if (model.Email != userToUpdate.EmailAddress)
                {
                    userToUpdate.EmailAddress = model.Email;
                }

                if (model.FirstName != userToUpdate.FirstName)
                {
                    userToUpdate.FirstName = model.FirstName;
                }

                if (model.LastName != userToUpdate.LastName)
                {
                    userToUpdate.LastName = model.LastName;
                }

                _context.SaveChanges();
            }
            catch (Exception ex) when(ex is DbUpdateException)
            {
                ModelState.AddModelError(string.Empty, "Could not update your account");
            }


            StatusMessage = "Your profile has been updated";
            return(RedirectToAction(nameof(Index)));
        }
        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"));;
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                using (var dbContextTransaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        //Check for email in view
                        var customer = (from e in _context.CustomerView
                                        where (e.EmailAddress.Equals(model.Email))
                                        select new
                        {
                            customerID = e.CustomerId,
                            source = e.Source
                        }).ToList().FirstOrDefault();

                        int    customerID;
                        string source;
                        if (customer == null)
                        {
                            //Create a new customer object for our database
                            Customer newCust = new Customer()
                            {
                                EmailAddress = model.Email,
                                FirstName    = model.FirstName,
                                LastName     = model.LastName
                            };
                            //add the new user then save our changes
                            var addedCust = _context.Customer.Add(newCust);
                            _context.SaveChanges();

                            // fill our local variables
                            customerID = addedCust.Entity.CustomerId;
                            source     = KinabaluConstants.KinabaluSource;
                        }
                        else
                        {
                            //customer was from another database, use its values for source and ID
                            customerID = customer.customerID;
                            source     = customer.source;
                        }

                        //Create a user object for our database
                        var newUser = new User
                        {
                            Password       = model.Password,
                            CustomerId     = customerID,
                            CustomerSource = source,
                            RoleId         = KinabaluConstants.UserRole
                        };
                        var addedUsr = _context.User.Add(newUser);

                        _context.SaveChanges();
                        dbContextTransaction.Commit();

                        //After registering, redirect to the homepage
                        _cookieService.Set(KinabaluConstants.cookieName, addedUsr.Entity.UserId.ToString(), new TimeSpan(0, 30, 0), Response);
                        return(RedirectToAction(nameof(HomeController.Index), "Home"));
                    }
                    catch (Exception)
                    {
                        dbContextTransaction.Rollback();
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError(string.Empty, "Problem creating the new user");
            return(View(model));
        }