public ActionResult ShowRoommatePage()
        {
            //show the home page of roommates
            if (!Authorize())
            {
                return(RedirectToAction("RedirectByUser", "Home"));
            }
            //remove gid from the session (added at showExpenses action)
            Session.Remove("gid");
            //get the groups of the user from DB
            User             u    = (User)(Session["CurrentUser"]);
            GroupRoommateDal gdal = new GroupRoommateDal();
            List <int>       gids = (from g in gdal.GroupsRoommates
                                     where g.userName == u.UserName
                                     select g.groupID).ToList <int>();
            GroupDal     groupd   = new GroupDal();
            List <Group> MyGroups = (from g in groupd.Groups
                                     where gids.Contains(g.gid)
                                     select g).ToList <Group>();
            VMGroups grp = new VMGroups()
            {
                Groups = MyGroups
            };

            return(View(grp));
        }
        public ActionResult SubmitChoosenUser()
        {
            //add the choosen users to DB
            if (!Authorize())
            {
                return(RedirectToAction("RedirectByUser", "Home"));
            }
            VMUsersGroup vmUsrGrp = (VMUsersGroup)TempData["VMUsersGroup"];

            if (Request.Form["users[]"] == null)
            {
                ViewBag.erorAddUser = "******";
                return(View("ChooseUser", vmUsrGrp));
            }
            string           usrs      = Request.Form["users[]"].ToString();
            List <string>    userNames = usrs.Split(',').ToList <string>();
            GroupRoommateDal grpRmtDal = new GroupRoommateDal();

            foreach (string usrName in userNames)
            {
                GroupRoommate newRec = new GroupRoommate()
                {
                    groupID  = vmUsrGrp.group.gid,
                    userName = usrName
                };
                grpRmtDal.GroupsRoommates.Add(newRec);
            }
            grpRmtDal.SaveChanges();
            ViewBag.addUsersToGroupSuccess = "המשתמשים התווספו בהצלחה!";
            return(View("ShowManagerPage"));
        }
        public ActionResult ChooseUser(Group grp)
        {
            //add users to choosen group
            if (!Authorize())
            {
                return(RedirectToAction("RedirectByUser", "Home"));
            }
            User u = (User)(Session["CurrentUser"]);
            //get the choosen group details
            GroupRoommateDal grpRmtDal             = new GroupRoommateDal();
            List <string>    userNamesAlreadyInGrp = (from usr in grpRmtDal.GroupsRoommates
                                                      where usr.groupID == grp.gid
                                                      select usr.userName).ToList <string>();
            //get all the registered users except the current manager and the the users that already belongs to the group
            UserDal     usrDal = new UserDal();
            List <User> usrs   = (from usr in usrDal.Users
                                  where usr.UserName != u.UserName &&
                                  !userNamesAlreadyInGrp.Contains(usr.UserName)
                                  select usr).ToList <User>();
            VMUsersGroup usrsGidVM = new VMUsersGroup()
            {
                group = grp,
                users = usrs
            };

            return(View(usrsGidVM));
        }
        public ActionResult AddGroupSubmit([ModelBinder(typeof(GroupBinder))] Group grp)
        {
            //add the new group to DB

            if (!Authorize())
            {
                return(RedirectToAction("RedirectByUser", "Home"));
            }
            VMGroups groupsVM = new VMGroups();
            GroupDal grpDal   = new GroupDal();

            ModelState.Clear();
            TryValidateModel(grp);
            if (ModelState.IsValid)
            {
                grp.managerUserName = ((User)(Session["CurrentUser"])).UserName;
                grpDal.Groups.Add(grp);
                try
                {
                    grpDal.SaveChanges();
                    //reset a new group object to view
                    groupsVM.Group = new Group();
                    GroupRoommate grpRmt = new GroupRoommate()
                    {
                        groupID  = grp.gid,
                        userName = grp.managerUserName
                    };
                    GroupRoommateDal grpRmtDal = new GroupRoommateDal();
                    grpRmtDal.GroupsRoommates.Add(grpRmt);
                    grpRmtDal.SaveChanges();
                    ViewBag.addGroupSuccess = "הקבוצה התווספה בהצלחה!";
                }
                catch (DbUpdateException)
                {
                    ViewBag.addNewGroupError = "התרחשה שגיאה בהוספת הקבוצה";
                    groupsVM.Group           = grp;
                }
            }
            else
            {
                groupsVM.Group = grp;
            }
            groupsVM.Groups = grpDal.Groups.ToList <Group>();
            return(View("AddGroup", groupsVM));
        }