public async Task <DataContracts.Application> AddAsync(string organisationId, string teamId, DataContracts.Application application) { var orgEntity = await organisationRepository.GetByAsync(o => o.Id == organisationId); if (orgEntity == null) { throw new ArgumentException($"The organisation {organisationId} not exists"); } var teamEntity = await this.teamRepository.GetByAsync(t => t.TeamCode == teamId && t.OrganisationId == organisationId); if (teamEntity == null) { throw new ArgumentException($"The team {teamId} from organisation {organisationId} not exists"); } var appEntity = application.ToEntity(); appEntity.TeamCode = teamId; appEntity.OrganisationId = organisationId; var invalidBranchSetting = application.Branches.Any(b => application.Branches.Count(x => x.Name == b.Name) > 1) || application.Branches.Any(b => application.Branches.Count(x => x.BranchPattern == b.BranchPattern) > 1); if (invalidBranchSetting) { throw new ArgumentException("Invalid branch map settings."); } if (!application.KeyBranches.Any()) { throw new ArgumentException("Must create one key branch unless."); } if (!application.Branches.Any()) { throw new ArgumentException("Must create one normal branch unless."); } appEntity.KeyBranchVersionings = appEntity.KeyBranches.Select(b => new Domain.Entities.KeyBranchVersioning { KeyBranchName = b.Name, UpdatedAt = DateTime.UtcNow, CreatedAt = DateTime.UtcNow, CurrentVersion = appEntity.InitialVersion }).ToList(); teamEntity.Applications.Add(new Domain.Entities.TeamApplication(appEntity)); await this.teamRepository.UpdateAsync(teamEntity); var result = await repository.AddAsync(appEntity); appEntity.KeyBranches.ForEach(kb => { var history = new Domain.Entities.KeyBranchVersionHistory { ApplicationId = result.Id, KeyBranchName = kb.Name, OrganisationId = organisationId, TeamId = teamId, VersionHistory = new List <Domain.Entities.KeyBranchVersion>() }; history.VersionHistory.Add(new Domain.Entities.KeyBranchVersion { CreatedAt = DateTime.UtcNow, FormatVersion = kb.FormatVersion, Request = new Domain.Entities.BranchActionRequest { Message = "Initial Version", From = "*", To = "*", BuildLabel = "", PreReleaseLabel = "", Commit = "" }, Version = appEntity.InitialVersion }); this.versionHistoryRepository.AddAsync(history).GetAwaiter().GetResult(); }); return(result.ToContract()); }
public async Task <DataContracts.Organisation> GetByOrganisationNameAsync(string organisationId) { var organisation = await repository.GetByAsync(o => o.Id == organisationId); return(organisation?.ToContract()); }