예제 #1
0
        public ActionResult Create(CreateProjectViewModel project)
        {
            if (project != null && ModelState.IsValid)
            {
                var contributors = new List<ApplicationUser>();

                if (project.Contributors != null)
                {
                    foreach (var userId in project.Contributors)
                    {
                        var contributor = this.Data.GetRepository<ApplicationUser>().All().FirstOrDefault(u => u.Id == userId);
                        contributors.Add(contributor);
                    }
                }

                var manager = this.Data.GetRepository<ApplicationUser>().All()
                    .FirstOrDefault(u => u.Id == project.Manager);

                var newProject = new Project()
                {
                    Name = project.Name,
                    Description = project.Description,
                    Manager = manager,
                    Contributors = contributors
                };

                this.Data.GetRepository<Project>().Add(newProject);
                this.Data.Save();
                return RedirectToAction("Index");
            }

            return View(project);
        }
예제 #2
0
        public ActionResult Edit(CreateProjectViewModel project)
        {
            if (ModelState.IsValid)
            {
                var currentProject = this.Data.GetRepository<Project>().All().FirstOrDefault(p => p.Id == project.Id);
                currentProject.Name = project.Name;
                currentProject.Description = project.Description;

                var oldManager = currentProject.Manager;
                oldManager.ProjectsManaging.Remove(currentProject);

                int contributorsCount = currentProject.Contributors.Count;

                for (int i = 0; i < contributorsCount; i++)
                {
                    var contributor = currentProject.Contributors.ElementAt(i);
                    contributor.ProjectsContributingTo.Remove(currentProject);
                }

                this.Data.Save();

                var newManager = this.Data.GetRepository<ApplicationUser>().All()
                    .FirstOrDefault(u => u.Id == project.Manager);
                currentProject.Manager = newManager;

                var contributors = new List<ApplicationUser>();

                if (project.Contributors != null)
                {
                    foreach (var userId in project.Contributors)
                    {
                        var contributor = this.Data.GetRepository<ApplicationUser>().All().FirstOrDefault(u => u.Id == userId);
                        contributors.Add(contributor);
                    }
                }

                currentProject.Contributors = contributors.ToList();
                this.Data.GetRepository<Project>().Update(currentProject);
                this.Data.Save();
                return RedirectToAction("Index");
            }
            return View(project);
        }