public async Task <Team> CreateAsync(TeamSubmit teamSubmit, Guid createdById) { // Start transactions to allow complete rollback in case of an error BeginAllTransactions(); try { // This will only map the first level of members onto the model. User IDs and Policy IDs will not be. TeamModel teamModel = mapper.Map <TeamModel>(teamSubmit); teamModel.ChangedBy = createdById; await AssignTeamsToTeamFromTeamIdList(teamModel, teamSubmit.TeamIds); await AssignApplicationDataPoliciesToTeamFromDataPolicyIdList(teamModel, teamSubmit.DataPolicies); // All successful CommitAllTransactions(); return(mapper.Map <Team>(await teamRepository.CreateAsync(teamModel))); } catch (Exception ex) { logger.Error(ex); RollbackAllTransactions(); throw; } }
public async Task CreateTeamAsync_WithTestTeam_ReturnsCreatedTeam() { // Arrange var teamService = Substitute.For <ITeamService>(); var inputModel = new TeamSubmit() { Uuid = Guid.NewGuid(), Name = "Test Team Name" }; teamService.CreateAsync(inputModel, Arg.Any <Guid>()) .Returns(new Team() { Uuid = inputModel.Uuid, Name = inputModel.Name } ); var controller = new TeamController(teamService, orderByHelper, paginationHelper, mapper); // Act IActionResult actionResult = await controller.CreateTeamAsync(inputModel); // Assert var okResult = actionResult as OkObjectResult; Assert.NotNull(okResult); var team = okResult.Value as Team; Assert.NotNull(team); Assert.True(team.Uuid == inputModel.Uuid, $"Retrieved Id {team.Uuid} not the same as sample id {inputModel.Uuid}."); Assert.True(team.Name == inputModel.Name, $"Retrieved Name {team.Name} not the same as sample Name {inputModel.Name}."); }
public async Task <Team> UpdateAsync(TeamSubmit teamSubmit, Guid updatedById) { // Start transactions to allow complete rollback in case of an error InitSharedTransaction(); try { TeamModel existingTeam = await teamRepository.GetByIdAsync(teamSubmit.Uuid, true); if (existingTeam == null) { throw new ItemNotFoundException($"Team with ID '{teamSubmit.Uuid}' not found when attempting to update a team using this ID!"); } if (existingTeam.Name != teamSubmit.Name) { // Confirm the new name is available var checkExistingNameModel = await teamRepository.GetByNameAsync(teamSubmit.Name, false); if (checkExistingNameModel != null) { throw new ItemNotProcessableException($"Team with name '{teamSubmit.Name}' already exists."); } } // Confirm this team isn't becoming a compound team while already part of a compound team if (teamSubmit.TeamIds.Any() && existingTeam.ParentTeams != null && existingTeam.ParentTeams.Any()) { throw new ItemNotProcessableException($"This team is already part of a compound team, and as such, cannot become a compound team itself."); } // Map the first level team submit attributes onto the team model. existingTeam.Name = teamSubmit.Name; existingTeam.Description = teamSubmit.Description; existingTeam.TermsOfServiceId = teamSubmit.TermsOfServiceId; existingTeam.ChangedBy = updatedById; // Note: Sub-Realms cannot be changed once create, hence the absense of a call to 'CheckForSubRealmAndAssignToTeamIfExists' function. await AssignTeamsToTeamFromTeamIdList(existingTeam, teamSubmit.TeamIds); await AssignApplicationDataPoliciesToTeamFromDataPolicyIdList(existingTeam, teamSubmit.DataPolicyIds, updatedById); await ValidateTermsOfServiceEntry(existingTeam.TermsOfServiceId); var updatedTeam = await teamRepository.UpdateAsync(existingTeam); // All successful CommitTransaction(); return(mapper.Map <Team>(updatedTeam)); } catch { RollbackTransaction(); throw; } }
private async Task CheckForSubRealmAndAssignToTeamIfExists(TeamModel team, TeamSubmit teamSubmit) { // Recall that submit models with empty GUIDs will not be null but rather Guid.Empty. if (teamSubmit.SubRealmId == null || teamSubmit.SubRealmId == Guid.Empty) { return; } var existingSubRealm = await subRealmRepository.GetByIdAsync(teamSubmit.SubRealmId, false); team.SubRealm = existingSubRealm ?? throw new ItemNotFoundException($"Sub-realm with ID '{teamSubmit.SubRealmId}' does not exist."); }
public async Task <Team> UpdateAsync(TeamSubmit teamSubmit, Guid updatedById) { // Start transactions to allow complete rollback in case of an error BeginAllTransactions(); try { TeamModel existingTeam = await teamRepository.GetByIdAsync(teamSubmit.Uuid, true); if (existingTeam == null) { throw new ItemNotFoundException($"Team with ID '{teamSubmit.Uuid}' not found when attempting to update a team using this ID!"); } if (existingTeam.Name != teamSubmit.Name) { // Confirm the new name is available var checkExistingNameModel = await teamRepository.GetByNameAsync(teamSubmit.Name, false); if (checkExistingNameModel != null) { throw new ItemNotProcessableException($"Team with name '{teamSubmit.Name}' already exists."); } } // Map the first level team submit attributes onto the team model. existingTeam.Name = teamSubmit.Name; existingTeam.Description = teamSubmit.Description; existingTeam.ChangedBy = updatedById; await AssignTeamsToTeamFromTeamIdList(existingTeam, teamSubmit.TeamIds); await AssignApplicationDataPoliciesToTeamFromDataPolicyIdList(existingTeam, teamSubmit.DataPolicies); // All successful CommitAllTransactions(); return(mapper.Map <Team>(await teamRepository.UpdateAsync(existingTeam))); } catch (Exception ex) { logger.Error(ex); RollbackAllTransactions(); throw; } }
public async Task <Team> CreateAsync(TeamSubmit teamSubmit, Guid createdById) { // Start transactions to allow complete rollback in case of an error InitSharedTransaction(); try { TeamModel existingTeam = await teamRepository.GetByNameAsync(teamSubmit.Name, false); if (existingTeam != null) { throw new ItemNotProcessableException($"Team with Name '{teamSubmit.Name}' already exist."); } // This will only map the first level of members onto the model. User IDs and Policy IDs will not be. var teamModel = mapper.Map <TeamModel>(teamSubmit); teamModel.ChangedBy = createdById; await CheckForSubRealmAndAssignToTeamIfExists(teamModel, teamSubmit); await AssignTeamsToTeamFromTeamIdList(teamModel, teamSubmit.TeamIds); await AssignApplicationDataPoliciesToTeamFromDataPolicyIdList(teamModel, teamSubmit.DataPolicyIds, createdById); await ValidateTermsOfServiceEntry(teamModel.TermsOfServiceId); var createdTeam = mapper.Map <Team>(await teamRepository.CreateAsync(teamModel)); // All successful CommitTransaction(); return(createdTeam); } catch { RollbackTransaction(); throw; } }
public TeamService_Tests() { var config = new MapperConfiguration(cfg => { cfg.AddProfile(new TeamResourceTeamModelProfile()); cfg.AddProfile(new TeamSubmitResourceTeamModelProfile()); cfg.AddProfile(new UserResourceUserModelProfile()); }); mapper = config.CreateMapper(); teamGuid = Guid.NewGuid(); childTeamGuid = Guid.NewGuid(); userGuid = Guid.NewGuid(); policyGuid = Guid.NewGuid(); termGuid = Guid.NewGuid(); mockedTeamModel = new TeamModel { Name = "Test team", Id = teamGuid, Description = "Test Description", TermsOfServiceId = termGuid, TermsOfService = new TermsOfServiceModel() { Id = termGuid, AgreementName = "Test Agreement" } }; mockedTeamModel.UserTeams = new List <UserTeamModel> { new UserTeamModel() { Team = mockedTeamModel, TeamId = teamGuid, // The identity server user primary keys are stored as strings, not Guids. UserId = userGuid.ToString(), User = new UserModel { // The mapper will attempt to map the user IDs and flatten them, so it needs to be set on the mock. Id = userGuid.ToString(), } } }; mockedTeamModel.ChildTeams = new List <TeamTeamModel>() { new TeamTeamModel() { ParentTeamId = teamGuid, ChildTeamId = childTeamGuid, ParentTeam = mockedTeamModel, ChildTeam = new TeamModel() { Id = childTeamGuid } } }; mockedTeamModel.ApplicationDataPolicies = new List <TeamApplicationDataPolicyModel>() { new TeamApplicationDataPolicyModel() { ApplicationDataPolicyId = policyGuid, Team = mockedTeamModel, TeamId = mockedTeamModel.Id, ApplicationDataPolicy = new ApplicationDataPolicyModel() { Name = "Test Policy Name", Id = policyGuid, Description = "Test Policy Description" } } }; mockedTeamSubmitModel = new TeamSubmit() { Uuid = mockedTeamModel.Id, Name = mockedTeamModel.Name, Description = mockedTeamModel.Description, TermsOfServiceId = mockedTeamModel.TermsOfServiceId, TeamIds = new List <Guid>(), DataPolicyIds = new List <Guid>() }; foreach (var team in mockedTeamModel.ChildTeams) { mockedTeamSubmitModel.TeamIds.Add(team.ChildTeamId); } foreach (var policy in mockedTeamModel.ApplicationDataPolicies) { mockedTeamSubmitModel.DataPolicyIds.Add(policy.ApplicationDataPolicyId); } }
public async override Task <IActionResult> UpdateTeamAsync([FromRoute, Required] Guid teamId, [FromBody] TeamSubmit teamSubmit) { if (teamId == Guid.Empty || teamSubmit.Uuid == Guid.Empty) { return(BadRequest()); } var loggedOnUser = ClaimsHelper.GetScalarClaimValue <Guid>(User, ClaimTypes.NameIdentifier, Guid.Empty); return(Ok(await teamService.UpdateAsync(teamSubmit, loggedOnUser))); }
public async override Task <IActionResult> CreateTeamAsync([FromBody] TeamSubmit teamSubmit) { var loggedOnUser = ClaimsHelper.GetScalarClaimValue <Guid>(User, ClaimTypes.NameIdentifier, Guid.Empty); return(Ok(await teamService.CreateAsync(teamSubmit, loggedOnUser))); }
public abstract Task <IActionResult> CreateTeamAsync([FromBody] TeamSubmit teamSubmit);
public abstract Task <IActionResult> UpdateTeamAsync([FromRoute][Required] Guid teamId, [FromBody] TeamSubmit teamSubmit);