public async Task <bool> ChangeEmployeeProjectPositionAsync(ProjectParticipantChangeDto participantToChange) { var projectParticipantRoleToDelete = await this.context.EmployeesProjectsRoles .Where(p => p.ProjectId == participantToChange.ProjectId && p.EmployeeId == participantToChange.EmployeeId).SingleOrDefaultAsync(); if (projectParticipantRoleToDelete == null) { throw new NullReferenceException(ErrorMessages.ProjectParticipantNullReference); } var isProjectPositionValid = await this.context.ProjectPositions.AnyAsync(pp => pp.Id == participantToChange.ProjectPositionId); if (!isProjectPositionValid) { throw new NullReferenceException(string.Format(ErrorMessages.ProjectPositionNullReference, participantToChange.ProjectPositionId)); } if (projectParticipantRoleToDelete.ProjectPositionId == participantToChange.ProjectPositionId) { return(true); } var newProjectParticiapntRole = participantToChange.To <EmployeesProjectsPositions>(); this.context.EmployeesProjectsRoles.Remove(projectParticipantRoleToDelete); var removeResult = await this.context.SaveChangesAsync(); await this.context.EmployeesProjectsRoles.AddAsync(newProjectParticiapntRole); var resultAdd = await this.context.SaveChangesAsync(); return(resultAdd > 0 && removeResult > 0); }
public async Task <bool> CheckIsParticipantLastManagerAsync(ProjectParticipantChangeDto input) { var project = await this.context.Projects.SingleOrDefaultAsync(p => p.Id == input.ProjectId); if (project == null) { throw new NullReferenceException(string.Format(ErrorMessages.ProjectIdNullReference, input.ProjectId)); } var projectParticipants = project.Participants; var participantToChange = projectParticipants.SingleOrDefault(p => p.EmployeeId == input.EmployeeId); if (participantToChange == null) { throw new ArgumentException(string.Format(ErrorMessages.ProjectParticipantArgumentException, input.EmployeeId, input.ProjectId)); } var newProjectPosition = await this.projectPositionsService.GetProjectPositionNameByIdAsync(input.ProjectPositionId); if (participantToChange.ProjectPosition.Name == Constants.PROJECT_MANAGER_ROLE && newProjectPosition != Constants.PROJECT_MANAGER_ROLE) { var projectManagerRolesCount = projectParticipants.Where(p => p.ProjectPosition.Name == Constants.PROJECT_MANAGER_ROLE).Count(); if (projectManagerRolesCount > 1) { return(false); } return(true); } return(false); }
public async Task <bool> RemoveParticipantAsync(ProjectParticipantChangeDto participantToRemove) { var projectParticipantToDelete = await this.context.EmployeesProjectsRoles .Where(p => p.ProjectId == participantToRemove.ProjectId && p.EmployeeId == participantToRemove.EmployeeId).SingleOrDefaultAsync(); if (projectParticipantToDelete == null) { throw new NullReferenceException(ErrorMessages.ProjectParticipantNullReference); } this.context.EmployeesProjectsRoles.Remove(projectParticipantToDelete); var result = await this.context.SaveChangesAsync(); return(result > 0); }
public async Task RemoveParticipantAsync_WithInvalidProjectId_ShouldThrowNullReferenceException() { var context = OmmDbContextInMemoryFactory.InitializeContext(); await SeedData(context); this.employeesProjectsPositionsService = new EmployeesProjectsPositionsService(context); ProjectParticipantChangeDto entityToRemove = (await context.EmployeesProjectsRoles.FirstAsync()).To <ProjectParticipantChangeDto>(); entityToRemove.ProjectId = "Invalid id"; var ex = await Assert.ThrowsAsync <NullReferenceException>(() => this.employeesProjectsPositionsService.RemoveParticipantAsync(entityToRemove)); Assert.Equal(ErrorMessages.ProjectParticipantNullReference, ex.Message); }
public async Task ChangeEmployeeProjectPositionAsync_WithInvalidProjectPositionId_ShouldThrowNullReferenceException() { var context = OmmDbContextInMemoryFactory.InitializeContext(); await SeedData(context); this.employeesProjectsPositionsService = new EmployeesProjectsPositionsService(context); ProjectParticipantChangeDto expectedResult = GetEmployeeProjectsPositionsDummyData().First().To <ProjectParticipantChangeDto>(); int invalidProjectPositionId = 22; expectedResult.ProjectPositionId = invalidProjectPositionId; var ex = await Assert.ThrowsAsync <NullReferenceException>(() => this.employeesProjectsPositionsService.ChangeEmployeeProjectPositionAsync(expectedResult)); Assert.Equal(string.Format(ErrorMessages.ProjectPositionNullReference, invalidProjectPositionId), ex.Message); }
public async Task RemoveParticipantAsync_WithValidData_ShouldRemoveProjectParticipantAndReturnTrue() { string errorMessagePrefix = "EmployeesProjectsPositions RemoveParticipantAsync() method does not work properly."; var context = OmmDbContextInMemoryFactory.InitializeContext(); await SeedData(context); this.employeesProjectsPositionsService = new EmployeesProjectsPositionsService(context); ProjectParticipantChangeDto entityToRemove = (await context.EmployeesProjectsRoles.FirstAsync()).To <ProjectParticipantChangeDto>(); var expectedCount = context.EmployeesProjectsRoles.Count() - 1; bool result = await this.employeesProjectsPositionsService.RemoveParticipantAsync(entityToRemove); var actualCount = context.EmployeesProjectsRoles.Count(); Assert.True(result, errorMessagePrefix); Assert.True(expectedCount == actualCount, errorMessagePrefix); }
public async Task ChangeEmployeeProjectPositionAsync_WithValidData_ShouldChangeProjectPositionAndReturnTrue() { string errorMessagePrefix = "EmployeesProjectsPositions ChangeEmployeeProjectPositionAsync() method does not work properly."; var context = OmmDbContextInMemoryFactory.InitializeContext(); await SeedData(context); this.employeesProjectsPositionsService = new EmployeesProjectsPositionsService(context); ProjectParticipantChangeDto expectedResult = GetEmployeeProjectsPositionsDummyData().First().To <ProjectParticipantChangeDto>(); expectedResult.ProjectPositionId = ProjectPosition_Id_2; bool returnResult = await this.employeesProjectsPositionsService.ChangeEmployeeProjectPositionAsync(expectedResult); ProjectParticipantChangeDto actualResult = (await context.EmployeesProjectsRoles.FirstAsync()).To <ProjectParticipantChangeDto>(); Assert.True(expectedResult.EmployeeId == actualResult.EmployeeId, errorMessagePrefix + " " + "EmployeeId is not returned properly."); Assert.True(expectedResult.ProjectId == actualResult.ProjectId, errorMessagePrefix + " " + "ProjectId is not returned properly."); Assert.True(expectedResult.ProjectPositionId == actualResult.ProjectPositionId, errorMessagePrefix + " " + "ProjectPositionId is not changed properly."); Assert.True(returnResult, errorMessagePrefix); }