//
 // GET: /AlphaContact/
 public ActionResult Index(string alpha)
 {
     var logic = new ContactBUS();
     var model = new ContactListAlphaViewModel();
     var dictionary = logic.GetContacts().GroupBy(g => g.Username.Substring(0, 1).ToUpper()).ToDictionary(g => g.Key, g => g.Count());
     foreach (var key in dictionary.Keys)
         model.LetterDictionary[key] = dictionary[key];
     model.Contacts = logic.GetContacts().Where(c => c.Username.ToUpper().StartsWith(alpha)).ToList();
     model.SelectedLetter = alpha;
     return View(model);
 }
        public ActionResult List(int page = 1)
        {
            var logic = new ContactBUS();
            var pageSize = 50;

            var contacts = logic.GetContacts().Skip((page - 1)*pageSize).Take(pageSize).ToList();
            var model = new ContactListViewModel();
            model.AllRoles = Roles.GetRoles();
            model.IsAdmin = Roles.UserHasRole("Admin");
            foreach(var contact in contacts)
            {
                if(Membership.UserExists(contact.Username))
                    model.UserNameRoles.Add(contact.Username.ToLower(), Roles.GetRolesForUser(contact.Username).ToList());

            }

            model.Contacts = contacts;
            model.CurrentPage = page;
            model.MaxPages = (int) Math.Ceiling(logic.GetContactsCount()/(double) pageSize);

            return View(model);
        }
        public ActionResult Create(ContactUser m)
        {
            m.AllRoles = Roles.GetRoles();
            if (m.UserRoles == null)
                m.UserRoles = new List<string>();
            var logic = new ContactBUS();
            if (ModelState.IsValid)
            {

                logic.CreateContact(m.Contact);
                if (m.IsUser)
                {
                    var userHelper = UserHelper.Create();
                    var contact =
                        logic.GetContacts().Where(c => c.Username == m.Contact.Username).SingleOrDefault();
                    userHelper.CreateUser(contact.Id);
                    if (m.UserRoles.Count() > 0)
                    {
                        foreach (var role in m.UserRoles)
                            userHelper.AddUserToRole(contact.Id, role);
                    }

                }
                return RedirectToAction("List");
            }
            else
                return View(m);
        }
        public ActionResult Grid(GridSortOptions gridSortOptions, int? page)
        {
            var logic = new ContactBUS();
            var contacts = logic.GetContacts().AsQueryable();
            if (string.IsNullOrEmpty(gridSortOptions.Column))
                gridSortOptions.Column = "FirstName";

            var contactPagedList = contacts
                .OrderBy(gridSortOptions.Column, gridSortOptions.Direction)
                .AsPagination(page ?? 1, 10);

            var model = new ContactGridModel
                            {
                                ContactPagedList = contactPagedList,
                                GridSortOptions = gridSortOptions
                            };

            return View(model);
        }