Exemple #1
0
        public async Task <int> AddProjectMember(int projectId, int userId, int roleId, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            var project = await _projectRepository.GetById(projectId, cancellationToken);

            if (project == null)
            {
                throw new ProjectNotFoundException(projectId);
            }

            var user = await _userRepository.GetById(userId, cancellationToken);

            if (user == null)
            {
                throw new UserNotFoundException(userId);
            }

            var projectMemberByProjectSpec = new ProjectMemberFilterSpecification(projectId, userId);
            var projectMember = await _projectMemberRepository.GetSingleBySpec(projectMemberByProjectSpec, cancellationToken);

            if (projectMember != null)
            {
                projectMember.ProjectMemberRoleId = roleId;
                await _projectMemberRepository.Update(projectMember, cancellationToken);

                return(projectMember.Id);
            }

            var newProjectMember = new ProjectMember {
                ProjectId = projectId, UserId = userId, ProjectMemberRoleId = roleId
            };

            return(await _projectMemberRepository.Create(newProjectMember, cancellationToken));
        }
        public ActionResult Create(ProjectMember projectMember)
        {
            if (ModelState.IsValid)
            {
                if (!DoIHaveAccessToProject(projectMember.ProjectId))
                {
                    return(RedirectToAction("NotAuthorized", "Error", new { area = "" }));
                }

                projectMember.CreatedByUserId = WebUser.Id;

                _projectMemberRepository.Create(projectMember);
                _unitOfWork.Commit();
                return(RedirectToAction("Details", "Projects", new { Id = projectMember.ProjectId }));
            }

            return(View(projectMember));
        }
        public ActionResult Update(ProjectMember vm)
        {
            ApiResult <ProjectMember> apiResult;

            if (ModelState.IsValid)
            {
                if (vm.Id > 0)
                {
                    apiResult = TryExecute(() =>
                    {
                        var project                 = _projectMemberRepository.Get(vm.Id);
                        project.EmployeeId          = vm.EmployeeId;
                        project.Rate                = vm.Rate;
                        project.ProjectMemberRoleId = vm.ProjectMemberRoleId;
                        project.ProjectId           = vm.ProjectId;
                        project.Billing             = vm.Billing;
                        project.UpdatedByUserId     = WebUser.Id;
                        project.MemberStatus        = vm.MemberStatus;
                        project.UpdatedOn           = DateTime.UtcNow;

                        _projectMemberRepository.Update(project);
                        _unitOfWork.Commit();
                        return(project);
                    }, "ProjectMember updated sucessfully");
                }
                else
                {
                    apiResult = TryExecute(() =>
                    {
                        var newProject = new ProjectMember
                        {
                            EmployeeId          = vm.EmployeeId,
                            Rate                = vm.Rate,
                            ProjectMemberRoleId = vm.ProjectMemberRoleId,
                            ProjectId           = vm.ProjectId,
                            Billing             = vm.Billing,
                            MemberStatus        = vm.MemberStatus,
                            CreatedByUserId     = WebUser.Id
                        };
                        _projectMemberRepository.Create(newProject);
                        _unitOfWork.Commit();
                        return(newProject);
                    }, "ProjectMember created sucessfully");
                }
            }
            else
            {
                apiResult = ApiResultFromModelErrors <ProjectMember>();
            }

            return(Json(apiResult, JsonRequestBehavior.AllowGet));
        }
        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));
        }