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.Application> UpdateAsync(string organisationId, string teamId, DataContracts.Application application) { if (!await organisationRepository.ExistsByAsync(o => o.Id == organisationId)) { throw new ArgumentException($"The organisation {organisationId} not exists"); } if (!await this.teamRepository.ExistsByAsync(t => t.TeamCode == teamId && t.OrganisationId == organisationId)) { throw new ArgumentException($"The team {teamId} from organisation {organisationId} not exists"); } var appLocated = await this.repository.GetByAsync(app => app.TeamCode == teamId && app.OrganisationId == organisationId && application.Id == app.Id); if (appLocated == null) { return(null); } var appEntity = application.ToEntity(); appEntity.OrganisationId = organisationId; appEntity.TeamCode = teamId; appEntity.KeyBranchVersionings = appLocated.KeyBranchVersionings; appEntity.GithubSecretKey = string.IsNullOrWhiteSpace(appEntity.GithubSecretKey) ? appLocated.GithubSecretKey : appEntity.GithubSecretKey; 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."); } var removedKeys = appEntity.KeyBranchVersionings.Where(b => !appEntity.KeyBranches.Any(kb => kb.Name == b.KeyBranchName)).ToList(); appEntity.KeyBranchVersionings.RemoveAll(b => !appEntity.KeyBranches.Any(kb => kb.Name == b.KeyBranchName)); appEntity.KeyBranchVersionings.AddRange( appEntity.KeyBranches .Where(b => !appEntity.KeyBranchVersionings.Any(kb => kb.KeyBranchName == b.Name)) .Select(b => new Domain.Entities.KeyBranchVersioning { KeyBranchName = b.Name, UpdatedAt = DateTime.UtcNow, CreatedAt = DateTime.UtcNow, CurrentVersion = appEntity.InitialVersion }).ToArray()); appEntity.KeyBranches.ForEach(kb => { if (!this.versionHistoryRepository.ExistsByAsync(h => h.OrganisationId == organisationId && h.TeamId == teamId && h.ApplicationId == appLocated.Id && h.KeyBranchName == kb.Name).GetAwaiter().GetResult()) { var history = new Domain.Entities.KeyBranchVersionHistory { ApplicationId = appLocated.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(); } }); removedKeys.ForEach(rk => { this.versionHistoryRepository.DeleteAsync(h => h.OrganisationId == organisationId && h.TeamId == teamId && h.ApplicationId == appLocated.Id && h.KeyBranchName == rk.KeyBranchName).GetAwaiter().GetResult(); }); var result = await repository.UpdateAsync(appEntity); return(result.ToContract()); }