Exemplo n.º 1
0
        public ActionResult Edit(EditProjectViewModel vm)
        {
            if (ModelState.IsValid)
            {
                if (!DoIHaveAccessToProject(vm.Id))
                {
                    return(RedirectToAction("NotAuthorized", "Error", new { area = "" }));
                }

                var selectedProject = _projectRepository.Get(vm.Id);

                if (selectedProject != null)
                {
                    selectedProject.ClientId              = vm.ClientId;
                    selectedProject.Title                 = vm.Title;
                    selectedProject.Description           = vm.Description;
                    selectedProject.StartDate             = vm.StartDate;
                    selectedProject.EndDate               = vm.EndDate;
                    selectedProject.Status                = vm.Status;
                    selectedProject.Billing               = vm.Billing;
                    selectedProject.ExpectedBillingAmount = vm.ExpectedBillingAmount;
                    selectedProject.ParentId              = vm.ParentId;
                    selectedProject.IsPublic              = vm.IsPublic;

                    selectedProject.UpdatedByUserId = WebUser.Id;

                    _projectRepository.Update(selectedProject);
                    _unitOfWork.Commit();


                    // Remove the existing mapped Technologies
                    var existingMaps = _projectTechnologyMapRepository.GetAllBy(m => m.ProjectId == selectedProject.Id);
                    foreach (var map in existingMaps)
                    {
                        _projectTechnologyMapRepository.Delete(map);
                    }

                    _unitOfWork.Commit();

                    // Map the New Technologies
                    if (vm.TechnologyIds != null)
                    {
                        foreach (var technologyId in vm.TechnologyIds)
                        {
                            var newMap = new ProjectTechnologyMap
                            {
                                ProjectId    = vm.Id,
                                TechnologyId = technologyId
                            };

                            _projectTechnologyMapRepository.Create(newMap);
                        }

                        _unitOfWork.Commit();
                    }

                    return(RedirectToAction("Index"));
                }
            }

            return(View(vm));
        }
Exemplo n.º 2
0
        public ActionResult Update(ProjectModel vm)
        {
            ApiResult <Project> apiResult;

            if (ModelState.IsValid)
            {
                if (vm.Id > 0)
                {
                    apiResult = TryExecute(() =>
                    {
                        var project                   = _projectRepository.Get(vm.Id);
                        project.ClientId              = vm.ClientId;
                        project.Title                 = vm.Title;
                        project.Description           = vm.Description;
                        project.StartDate             = vm.StartDate;
                        project.EndDate               = vm.EndDate;
                        project.Status                = vm.Status.Value;
                        project.Billing               = vm.Billing;
                        project.ExpectedBillingAmount = vm.ExpectedBillingAmount;
                        project.Rate                  = vm.Rate;
                        project.ParentId              = vm.ParentId;
                        project.IsPublic              = vm.IsPublic;
                        project.InheritMembers        = vm.InheritMembers;
                        project.IsClosed              = vm.IsClosed;
                        project.ProjectType           = vm.ProjectType;
                        project.UpdatedByUserId       = WebUser.Id;
                        project.UpdatedOn             = DateTime.UtcNow;

                        _projectRepository.Update(project);
                        _unitOfWork.Commit();

                        // Remove the existing mapped Technologies
                        var existingMaps = _projectTechnologyMapRepository.GetAllBy(m => m.ProjectId == project.Id).ToList();
                        foreach (var map in existingMaps)
                        {
                            _projectTechnologyMapRepository.Delete(map);
                        }

                        _unitOfWork.Commit();


                        // Map the New Technologies
                        if (vm.TechnologyIds != null)
                        {
                            foreach (var technologyId in vm.TechnologyIds)
                            {
                                var newMap = new ProjectTechnologyMap
                                {
                                    ProjectId    = vm.Id,
                                    TechnologyId = technologyId
                                };

                                _projectTechnologyMapRepository.Create(newMap);
                            }



                            _unitOfWork.Commit();
                        }
                        return(project);
                    }, "Project updated sucessfully");
                }
                else
                {
                    apiResult = TryExecute(() =>
                    {
                        var newProject = new Project
                        {
                            ClientId        = vm.ClientId,
                            Title           = vm.Title,
                            Description     = vm.Description,
                            StartDate       = vm.StartDate,
                            EndDate         = vm.EndDate,
                            Status          = vm.Status.Value,
                            Billing         = vm.Billing,
                            ParentId        = vm.ParentId,
                            IsPublic        = vm.IsPublic,
                            InheritMembers  = vm.InheritMembers,
                            IsClosed        = vm.IsClosed,
                            CreatedByUserId = WebUser.Id,
                            ProjectType     = vm.ProjectType
                        };
                        _projectRepository.Create(newProject);
                        _unitOfWork.Commit();

                        // Map the Technologies
                        if (vm.TechnologyIds != null)
                        {
                            foreach (var technologyId in vm.TechnologyIds)
                            {
                                var newMap = new ProjectTechnologyMap
                                {
                                    ProjectId    = newProject.Id,
                                    TechnologyId = technologyId
                                };

                                _projectTechnologyMapRepository.Create(newMap);
                            }

                            _unitOfWork.Commit();
                        }

                        return(newProject);
                    }, "Project created sucessfully");
                }
            }
            else
            {
                apiResult = ApiResultFromModelErrors <Project>();
            }

            return(Json(apiResult, JsonRequestBehavior.AllowGet));
        }