public void AddTeamMember_ShouldAddTeamMember_WhenValidTeamMember()
        {
            var teamMemberCreateDto = new TeamMemberCreateDto
            {
                TeamId    = teamId,
                Email     = email,
                Firstname = firstname,
                Lastname  = lastname
            };

            var newTeamMember = new TeamMember
            {
                Id        = id,
                Email     = teamMemberCreateDto.Email,
                Firstname = teamMemberCreateDto.Firstname,
                Lastname  = teamMemberCreateDto.Lastname,
                IsActive  = isActive,
                TeamId    = teamId
            };

            _unitOfWork.Setup(x => x.TeamMemberRepository.Add(It.Is <TeamMember>(x => x.Email == teamMemberCreateDto.Email &&
                                                                                 x.Firstname == teamMemberCreateDto.Firstname &&
                                                                                 x.Lastname == teamMemberCreateDto.Lastname &&
                                                                                 x.TeamId == teamMemberCreateDto.TeamId)))
            .Returns(newTeamMember);

            var result = _service.AddTeamMember(teamMemberCreateDto);

            Assert.Equal(id, result.Id);
            Assert.Equal(email, result.Email);
            Assert.Equal(firstname, result.Firstname);
            Assert.Equal(lastname, result.Lastname);
            Assert.Equal(isActive, result.IsActive);
        }
        public void AddTeamMember_ShouldThrowException_WhenDuplicateTeamMember()
        {
            var teamMemberCreateDto = new TeamMemberCreateDto
            {
                TeamId    = teamId,
                Email     = email,
                Firstname = firstname,
                Lastname  = lastname
            };

            var newTeamMember = new TeamMember
            {
                Id        = id,
                Email     = teamMemberCreateDto.Email,
                Firstname = teamMemberCreateDto.Firstname,
                Lastname  = teamMemberCreateDto.Lastname,
                IsActive  = isActive,
                TeamId    = teamId
            };

            _unitOfWork.Setup(x => x.TeamMemberRepository.Add(It.IsAny <TeamMember>())).Returns(newTeamMember);
            _unitOfWork.Setup(x => x.Commit()).Throws(new DbUpdateException());

            var result = Record.Exception(() => _service.AddTeamMember(teamMemberCreateDto));

            Assert.IsType <DbUpdateException>(result);
        }
Пример #3
0
 public TeamReadDto AddTeam(TeamCreateDto teamCreateDto)
 {
     try
     {
         var newTeam = _unitOfWork.TeamRepository.Add(_mapper.Map <Team>(teamCreateDto));
         _unitOfWork.Commit();
         var user          = _unitOfWork.UserRepository.GetById(teamCreateDto.TeamleaderId);
         var newTeamMember = new TeamMemberCreateDto
         {
             Email     = user.Email,
             Firstname = user.Firstname,
             Lastname  = user.Lastname,
             TeamId    = newTeam.Id
         };
         _unitOfWork.TeamMemberRepository.Add(_mapper.Map <TeamMember>(newTeamMember));
         _unitOfWork.Commit();
         var team = _unitOfWork.TeamRepository.GetTeamIncludingTeamMembersById(newTeam.Id);
         return(_mapper.Map <TeamReadDto>(team));
     }
     catch (DbUpdateException ex) when((ex.InnerException as SqlException)?.Number == 2601)
     {
         _unitOfWork.Rollback();
         throw new Exception($"Team already exists with the name {teamCreateDto.Name}", ex);
     }
 }
 public TeamMemberReadDto AddTeamMember(TeamMemberCreateDto teamMemberCreateDto)
 {
     try
     {
         var newTeamMember = _unitOfWork.TeamMemberRepository.Add(_mapper.Map <TeamMember>(teamMemberCreateDto));
         _unitOfWork.Commit();
         return(_mapper.Map <TeamMemberReadDto>(newTeamMember));
     }
     catch (DbUpdateException ex) when((ex.InnerException as SqlException)?.Number == 2601)
     {
         _unitOfWork.Rollback();
         throw new Exception($"Teammember already exists with the email {teamMemberCreateDto.Email}", ex);
     }
 }
Пример #5
0
        public ActionResult <TeamMemberReadDto> AddTeamMember(TeamMemberCreateDto teamMemberCreateDto)
        {
            if (teamMemberCreateDto == null)
            {
                return(BadRequest());
            }

            try
            {
                var teamMember = _service.AddTeamMember(teamMemberCreateDto);
                return(CreatedAtAction(nameof(GetTeamMemberById), new { Id = teamMember.Id }, teamMember));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }