示例#1
0
        public ActionResult Create(ProjectCreateViewModel viewmodel)
        {
            if (ModelState.IsValid)
            {
                Project project = new Project();
                //add basic stuff to project
                project.EndDate = viewmodel.EndDate;
                project.Name = viewmodel.Name;
                project.ProjectId = viewmodel.ProjectId;

                List<PersonProjectRecord> newrecords = new List<PersonProjectRecord>();

                //add project groups if there are selected groups
                if (viewmodel.SelectedGroupIds != null)
                {
                    List<Group> projectgroups = new List<Group>();
                    foreach (int groupId in viewmodel.SelectedGroupIds)
                    {
                        Group group = db.Groups.Find(groupId);
                        projectgroups.Add(group);
                    }
                    project.ProjectGroups = projectgroups;

                    //create personprojectrecords for individuals added through groups
                    foreach (Group group in projectgroups)
                    {
                        foreach (Person member in group.Members)
                        {
                            PersonProjectRecord newrecord = new PersonProjectRecord();
                            newrecord.PersonId = member.PersonId;
                            newrecord.ProjectId = project.ProjectId;
                            newrecord.RemoveWithGroup = true;
                            newrecords.Add(newrecord);
                        }
                    }
                }

                //create personprojectrecords for individuals added outside of groups
                if (viewmodel.SelectedPersonIds != null)
                {
                    foreach (int id in viewmodel.SelectedPersonIds)
                    {
                        PersonProjectRecord newrecord = new PersonProjectRecord();
                        newrecord.PersonId = id;
                        newrecord.ProjectId = project.ProjectId;
                        newrecord.RemoveWithGroup = false;
                        newrecords.Add(newrecord);
                    }
                }

                //save to db
                if (newrecords.Count != 0)
                {
                    foreach (PersonProjectRecord record in newrecords)
                    {
                        db.PersonProjectRecords.Add(record);
                    }
                }

                db.Projects.Add(project);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(viewmodel);
        }
示例#2
0
        public ActionResult AddMemberToGroup(PersonGroupingViewModel viewmodel)
        {
            if (ModelState.IsValid)
            {
                //add the person to the group
                int PersonId = viewmodel.PersonId;
                int GroupId = viewmodel.SelectedGroupId;
                Person person = db.People.Find(PersonId);
                Group group = db.Groups.Find(GroupId);

                group.Members.Add(person);
                db.SaveChanges();

                //add person to group's projects & create ppr with removewithgroup = true
                List<Person> isingroupalready = new List<Person>();
                foreach(Project project in group.Projects) //for every project the group is assigned to
                {
                    foreach(PersonProjectRecord ppr in project.PersonProjectRecords) //for every ppr on the project
                    {
                        //if they are already on the project and their rwg flag is false add them to this list
                        if (ppr.PersonId == person.PersonId && ppr.RemoveWithGroup == false)
                        {
                            isingroupalready.Add(person);
                        }
                    }
                    //if the list is empty (person not already in group  w/ false rwg flag)
                    //then make a new ppr and add them to project with true rwg flag
                    if(isingroupalready.Count == 0)
                    {
                        PersonProjectRecord newrecord = new PersonProjectRecord();
                        newrecord.PersonId = person.PersonId;
                        newrecord.ProjectId = project.ProjectId;
                        newrecord.RemoveWithGroup = true;

                        db.PersonProjectRecords.Add(newrecord);
                    }
                }
                db.SaveChanges();

                //create a new viewmodel to pass back to the view with updated memberships (v.important)
                PersonGroupingViewModel vm = new PersonGroupingViewModel();
                SelectList allthegroups = new SelectList(db.Groups, "GroupId", "Name");
                vm.PersonId = person.PersonId;
                vm.FirstName = person.FirstName;
                vm.LastName = person.LastName;
                vm.Memberships = person.Memberships;
                vm.AllGroups = allthegroups;

                return View(vm);

            }

            return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Something is wrong with the returned viewmodel.");
        }