示例#1
0
        public async Task AddMemberAsync(string senderUserId, int projectId, string userId, AppUserRoleDto roleDto)
        {
            AppUserRole senderRole = await GetRoleIfMember(senderUserId, projectId);

            if (!senderRole.CanAddNewMember())
            {
                throw new ForbiddenResponseException("You haven't permissions to add members to this project");
            }

            if (!await _userRepo.ExistsWithId(userId))
            {
                throw new NotFoundResponseException("Cannot find user to add into project.");
            }

            if (!((await _puRepo.GetRoleOfMember(userId, projectId)).IsNone()))
            {
                throw new BadRequestResponseException("This user already exists in project.");
            }

            AppUserRole role = _mapper.Map <AppUserRoleDto, AppUserRole>(roleDto);

            if (role.IsNone())
            {
                throw new BadRequestResponseException("Given role is bad.");
            }

            if (role.IsOwner())
            {
                throw new BadRequestResponseException("Cannot add Owner to project.");
            }

            if (role.IsScrumMaster() && await _puRepo.DoesExistScrumMasterInProjectAsync(projectId))
            {
                throw new BadRequestResponseException("Scrum Master already exists in project.");
            }

            var pu = new ProjectUser()
            {
                ProjectId  = projectId,
                UserId     = userId,
                UserRoleId = role.Id
            };
            await _puRepo.CreateRecordAsync(pu);
        }