public ActionResult Account(int id = -1)
        {
            if (RouteData.Values["id"] != null)
            {
                if (int.TryParse(RouteData.Values["id"].ToString(), out id))
                {
                }
            }
            if (id == -1)
            {
                //string error1 = "To view a User's account, you must pass in a User Id.";
                //ViewBag.ErrorMessages = new string[] { error1 };
                //return View("PageNotFound");
                id = WebSecurity.CurrentUserId;
            }
            PortfolioUnleashed.User user = db.retrieveUser(id);
            if (user == null)
            {
                string error1 = "The User whose account you tried to view either does not exist or could not be found.";
                //string error2 = "User Id: "+ id;
                TempData["ErrorMessages"] = new string[] { error1 };
                return(RedirectToAction("Http404", "Error"));
            }
            VMUser vmUser = new VMUser(user);

            if (Request.IsAuthenticated)
            {
                if (id != WebSecurity.CurrentUserId)
                {
                    ViewBag.IsQuickReference = db.retrieveQuickReferences(WebSecurity.CurrentUserId).Any(q => q.QuickReferenceId == id);
                }
            }

            return(View(vmUser));
        }
        public ActionResult RemoveQuickContact(int id)
        {
            if (RouteData.Values["id"] != null)
            {
                if (int.TryParse(RouteData.Values["id"].ToString(), out id))
                {
                }
            }
            PortfolioUnleashed.User user = db.retrieveUser(id);
            if (user == null)
            {
                string error1 = "The User you tried to remove as a QuickContact either does not exist or could not be found.";
                string error2 = "User Id: " + id;
                ViewBag.ErrorMessages = new string[] { error1, error2 };
                return(View("PageNotFound"));
            }

            db.removeQuickReference(WebSecurity.CurrentUserId, id);

            return(RedirectToAction("Account", new { id = id }));
        }
        public ActionResult AccountEdit(int id)
        {
            if (RouteData.Values["id"] != null)
            {
                if (int.TryParse(RouteData.Values["id"].ToString(), out id))
                {
                }
            }
            PortfolioUnleashed.User user = db.retrieveUser(id);
            if (user == null)
            {
                string error1 = "The User Account you tried to edit either does not exist or could not be found.";
                string error2 = "User Id: " + id;
                ViewBag.ErrorMessages = new string[] { error1, error2 };
                return(View("PageNotFound"));
            }

            VMEditingUser userToEdit = new VMEditingUser(user);

            return(View(model: userToEdit));
        }
        public ActionResult PortfolioCreateEdit(VMEditingPortfolio model)
        {
            //model.Id = 4;
            //int.TryParse(Request.Form["Id"], out model.Id))

            if (ModelState.IsValid)
            {
                List <string> keys = Request.Form.AllKeys.Where(k => k.Contains("AddBox")).ToList();

                bool      isNewPortoflio = true;
                Portfolio existing       = db.retrievePortfolio(model.Id);
                if (existing != null)//portfolio exists
                {
                    isNewPortoflio           = false;
                    existing.Title           = model.Title;
                    existing.Description     = model.Description;
                    existing.IsMainPortfolio = model.IsMainPortfolio;
                    db.updatePortfolio(existing, model.UserId);

                    if (keys != null && keys.Count > 0)
                    {
                        foreach (string key in keys)
                        {
                            int  projectId = int.Parse(key.Substring(0, key.IndexOf("AddBox")));
                            bool addAsProj = Request.Form.GetValues(key).FirstOrDefault().Equals("true");
                            if (addAsProj)                                          //They want it in portfolio
                            {
                                if (!existing.Projects.Any(p => p.Id == projectId)) //Isn't already in portfolio
                                {
                                    db.addProjectToPortfolio(db.retrieveProject(projectId), model.Id);
                                }
                            }
                            else//don't want in portfolio
                            {
                                if (existing.Projects.Any(p => p.Id == projectId))//Is in portfolio
                                {
                                    db.removeProjectFromPortfolio(db.retrieveProject(projectId), model.Id);
                                }
                            }
                        }
                    }
                }
                else
                {
                    existing                 = new Portfolio();
                    existing.Title           = model.Title;
                    existing.Description     = model.Description;
                    existing.IsMainPortfolio = model.IsMainPortfolio;
                    db.addPortfolio(existing, WebSecurity.CurrentUserId);

                    if (keys != null && keys.Count > 0)
                    {
                        foreach (string key in keys)
                        {
                            int  projectId = int.Parse(key.Substring(0, key.IndexOf("AddBox")));
                            bool addAsProj = Request.Form.GetValues(key).FirstOrDefault().Equals("true");
                            if (addAsProj)
                            {
                                existing.Projects.Add(db.retrieveProject(projectId));
                                db.addProjectToPortfolio(db.retrieveProject(projectId), existing.Id);
                            }
                        }
                    }
                }

                if (model.IsMainPortfolio)
                {
                    int userId = (isNewPortoflio) ? WebSecurity.CurrentUserId : WebSecurity.CurrentUserId;
                    PortfolioUnleashed.User user = db.retrieveUser(userId);
                    if (user.Portfolios != null && user.Portfolios.Count > 0)
                    {
                        foreach (Portfolio p in user.Portfolios.Where(p => p.Id != existing.Id))
                        {
                            if (p.IsMainPortfolio)
                            {
                                p.IsMainPortfolio = false;
                                db.updatePortfolio(p, userId);
                            }
                        }
                    }
                }

                return(RedirectToAction("Account", "User", new { id = WebSecurity.CurrentUserId }));
            }
            Portfolio port = new Portfolio();

            port.Id              = model.Id;
            port.Title           = model.Title;
            port.Description     = model.Description;
            port.IsMainPortfolio = model.IsMainPortfolio;
            port.UserId          = model.UserId;
            //port.Projects = model.Projects;

            return(View(model: new VMEditingPortfolio(port, model.UserId)
            {
                ProjectCatalog = model.ProjectCatalog
            }));
        }
        public ActionResult AccountEdit(VMEditingUser model)
        {
            bool isValidInput           = true;
            int  userId                 = int.Parse(Request.Form["UserId"]);
            List <ContactInfo> contacts = new List <ContactInfo>();

            if (!AllContactInfosVerified(out contacts))
            {
                isValidInput = false;
            }

            List <Education> educations = new List <Education>();

            if (!AllEducationsVerified(out educations))
            {
                isValidInput = false;
            }

            List <Link> links = new List <Link>();

            if (!AllLinksVerified(out links))
            {
                isValidInput = false;
            }

            if ((WebSecurity.CurrentUserId == userId) && !string.IsNullOrEmpty(model.NewPassword))
            {
                if (model.NewPassword.Length < 6)
                {
                    isValidInput = false;
                    ModelState.AddModelError("NewPassword", "New Password must contain at least six characters.");
                }
                else if (string.IsNullOrEmpty(model.ConfirmNewPassword) || !model.NewPassword.Equals(model.ConfirmNewPassword))
                {
                    isValidInput = false;
                    ModelState.AddModelError("ConfirmNewPassword", "New Password and Confirm New Password do not match.");
                }
            }

            if ((WebSecurity.CurrentUserId == userId) && isValidInput && ModelState.IsValid)
            {
                if (string.IsNullOrEmpty(model.CurrentPassword))
                {
                    isValidInput = false;
                    ModelState.AddModelError("CurrentPassword", "You must enter your current password.");
                }
                else if (!WebSecurity.Login(model.Email, model.CurrentPassword))
                {
                    isValidInput = false;
                    ModelState.AddModelError("CurrentPassword", "The Password you entered is incorrect.");
                }
                else
                {
                    if (!string.IsNullOrEmpty(model.NewPassword))
                    {
                        if (!WebSecurity.ChangePassword(model.Email, model.CurrentPassword, model.NewPassword))
                        {
                            isValidInput = false;
                            ModelState.AddModelError("NewPassword", "Could not change password to the new one provided. Please try again with a different password.");
                        }
                    }
                }
            }

            #region Update User's Properties
            if (isValidInput && ModelState.IsValid)
            {
                PortfolioUnleashed.User updatedUser = db.retrieveUser(userId);
                updatedUser.FirstName = model.FirstName;
                updatedUser.LastName  = model.LastName;
                db.updateUser(updatedUser);
                foreach (ContactInfo c in contacts)
                {
                    if (c.UserId == userId)//It's an existing entry
                    {
                        if (string.IsNullOrEmpty(c.Title) && string.IsNullOrEmpty(c.Information))
                        {
                            db.deleteContactInfo(c, userId);
                        }
                        else
                        {
                            db.updateContactInfo(c);
                        }
                    }
                    else//new entry
                    {
                        db.addContactInfo(c, userId);
                    }
                }
                foreach (Education e in educations)
                {
                    if (e.UserId == userId)//It's an existing entry
                    {
                        if (string.IsNullOrEmpty(e.School) && string.IsNullOrEmpty(e.Degree))
                        {
                            //They blanked out the fields, want to remove the entry
                            db.deleteEducation(e, userId);
                        }
                        else
                        {
                            //edit the user's education info via e.Id and UserId
                            db.updateEducation(e, userId);
                        }
                    }
                    else//new entry
                    {
                        db.addEducation(e, userId);
                    }
                }
                foreach (Link l in links)
                {
                    if (l.UserId == userId)//It's an existing entry
                    {
                        if (string.IsNullOrEmpty(l.DisplayText) && string.IsNullOrEmpty(l.URL))
                        {
                            //They blanked out the fields, want to remove the entry
                            db.deleteLink(l, userId);
                        }
                        else
                        {
                            //edit the user's education info via e.Id and UserId
                            db.updateLink(l, userId);
                        }
                    }
                    else//new entry
                    {
                        db.addLink(l, userId);
                    }
                }

                return(RedirectToAction("Account", "User", new { id = userId }));
            }
            #endregion

            User u = new User();
            u.Id            = userId;
            u.FirstName     = model.FirstName;
            u.LastName      = model.LastName;
            u.Email         = model.Email;
            u.ContactInfoes = contacts;
            u.Links         = links;
            u.Educations    = educations;

            return(View(new VMEditingUser(u)));
        }