Example #1
0
        public ActionResult Create(NewProjectViewModel vm)
        {
            if (ModelState.IsValid)
            {
                var employee   = _employeeRepository.GetBy(u => u.UserId == WebUser.Id, "User,User.Person,ReportingPerson.User.Person,Manager.User.Person,Location,Department,Designation,Shift");
                var newProject = new Project
                {
                    ClientId        = vm.ClientId,
                    Title           = vm.Title,
                    Description     = vm.Description,
                    StartDate       = vm.StartDate,
                    EndDate         = vm.EndDate,
                    Status          = vm.Status,
                    Billing         = vm.Billing,
                    ParentId        = vm.ParentId,
                    IsPublic        = vm.IsPublic,
                    InheritMembers  = vm.InheritMembers,
                    IsClosed        = vm.IsClosed,
                    CreatedByUserId = WebUser.Id
                };


                _projectRepository.Create(newProject);
                _unitOfWork.Commit();

                // Add user as a Project Member
                var projectMember = new ProjectMember
                {
                    ProjectId  = newProject.Id,
                    EmployeeId = employee.Id,
                    // EmployeeId = WebUser.Id,
                    //ProjectMemberRoleId = projectMember.ProjectMemberRoleId,
                    Billing         = Billing.NonBillable,
                    Rate            = 0,
                    CreatedByUserId = WebUser.Id
                };

                _projectMemberRepository.Create(projectMember);
                _unitOfWork.Commit();

                // If Need to Copy, copy from Project Project
                if (vm.InheritMembers && vm.ParentId.HasValue)
                {
                    var parentMembers = _projectMemberRepository.GetAllBy(m => m.ProjectId == vm.ParentId.Value).ToList();
                    foreach (var member in parentMembers)
                    {
                        // Add user as a Project Member
                        var newProjectMember = new ProjectMember
                        {
                            ProjectId           = newProject.Id,
                            EmployeeId          = employee.Id,
                            ProjectMemberRoleId = member.ProjectMemberRoleId,
                            Billing             = member.Billing,
                            CreatedByUserId     = WebUser.Id
                        };

                        _projectMemberRepository.Create(newProjectMember);
                    }

                    _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(RedirectToAction("Index"));
            }

            return(View(vm));
        }
Example #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));
        }