public ProgrammeCreated(string salesArea, string externalReference, string programmeName, string description, string classification, bool liveBroadcast, DateTime startDateTime, TimeSpan duration, IEnumerable <string> programmeCategories, ProgrammeEpisode episode) { SalesArea = salesArea; ExternalReference = externalReference; ProgrammeName = programmeName; Description = description; Classification = classification; LiveBroadcast = liveBroadcast; StartDateTime = startDateTime; Duration = duration; ProgrammeCategories = programmeCategories; Episode = episode; }
public void Add(IEnumerable <Programme> item) { using (var operationDbContext = _dbContextFactory.Create()) { var prgDictCache = new ProgrammeDictionaryCache(operationDbContext, trackingChanges: false); var prgCatCache = new ProgrammeCategoryCache(operationDbContext, trackingChanges: false); var prgDictToProcess = new HashSet <Entities.Tenant.ProgrammeDictionary>(); var prgCatToProcess = new List <ProgrammeCategory>(); var prgToProcess = new List <Entities.Tenant.Programmes.Programme>(); var prgCatLinkToProcess = new List <ProgrammeCategoryLink>(); var prgDictEpisodeToProcess = new HashSet <ProgrammeEpisode>(); foreach (var model in item) { var programmeDictionary = prgDictCache.Get(model.ExternalReference); if (programmeDictionary is null) { programmeDictionary = _mapper.Map <Entities.Tenant.ProgrammeDictionary>(model); prgDictCache.Add(programmeDictionary); } else { programmeDictionary.Name = model.ProgrammeName; programmeDictionary.Description = model.Description; programmeDictionary.Classification = model.Classification; } _ = prgDictToProcess.Add(programmeDictionary); var programme = _mapper.Map <Entities.Tenant.Programmes.Programme>(model, opts => opts.UseEntityCache(_salesAreaByNameCache)); if (programme.Id == Guid.Empty) { programme.Id = Guid.NewGuid(); } programme.ProgrammeDictionary = programmeDictionary; prgToProcess.Add(programme); foreach (var catName in model.ProgrammeCategories ?? Enumerable.Empty <string>()) { var category = prgCatCache.GetOrAdd(catName, key => { var newCat = new ProgrammeCategory { Name = key }; prgCatToProcess.Add(newCat); return(newCat); }); if (programme.ProgrammeCategoryLinks.All(x => !string.Equals(x.ProgrammeCategory.Name, category.Name))) { var link = new ProgrammeCategoryLink { ProgrammeCategory = category, Programme = programme, ProgrammeId = programme.Id }; programme.ProgrammeCategoryLinks.Add(link); prgCatLinkToProcess.Add(link); } } if (!(model.Episode is null)) { var episode = programmeDictionary.ProgrammeEpisodes.FirstOrDefault(x => x.Number == model.Episode.Number); if (episode is null) { episode = new ProgrammeEpisode { Name = model.Episode.Name, Number = model.Episode.Number, ProgrammeDictionary = programmeDictionary }; programmeDictionary.ProgrammeEpisodes.Add(episode); } else { episode.Name = model.Episode.Name; } programme.Episode = episode; _ = prgDictEpisodeToProcess.Add(episode); } } using (var transaction = operationDbContext.Specific.Database.BeginTransaction()) { operationDbContext.BulkInsertEngine.BulkInsertOrUpdate(prgDictToProcess.ToList(), new BulkInsertOptions { PreserveInsertOrder = true, SetOutputIdentity = true }); foreach (var episode in prgDictEpisodeToProcess) { episode.ProgrammeDictionaryId = episode.ProgrammeDictionary.Id; } operationDbContext.BulkInsertEngine.BulkInsertOrUpdate(prgDictEpisodeToProcess.ToList(), new BulkInsertOptions { PreserveInsertOrder = true, SetOutputIdentity = true }); operationDbContext.BulkInsertEngine.BulkInsert(prgCatToProcess, new BulkInsertOptions { PreserveInsertOrder = true, SetOutputIdentity = true }); foreach (var prg in prgToProcess) { prg.ProgrammeDictionaryId = prg.ProgrammeDictionary.Id; prg.EpisodeId = prg.Episode?.Id; } operationDbContext.BulkInsertEngine.BulkInsert(prgToProcess, new BulkInsertOptions { PreserveInsertOrder = true }); foreach (var catLink in prgCatLinkToProcess) { catLink.ProgrammeCategoryId = catLink.ProgrammeCategory.Id; } operationDbContext.BulkInsertEngine.BulkInsert(prgCatLinkToProcess); transaction.Commit(); } var actionBuilder = new BulkInsertActionBuilder <Entities.Tenant.Programmes.Programme>(prgToProcess, _mapper); actionBuilder.TryToUpdate(item, opts => opts.UseEntityCache(_salesAreaByIdCache)); actionBuilder.Build()?.Execute(); } }
public void Add(Programme item) { if (item is null) { throw new ArgumentNullException(nameof(item)); } var programmeDictionary = _programmeDictionaryCache.Get(item.ExternalReference); if (programmeDictionary is null) { programmeDictionary = _mapper.Map <Entities.Tenant.ProgrammeDictionary>(item); _programmeDictionaryCache.Add(programmeDictionary); } else { programmeDictionary.Name = item.ProgrammeName; programmeDictionary.Description = item.Description; programmeDictionary.Classification = item.Classification; } var programme = _mapper.Map <Entities.Tenant.Programmes.Programme>(item, opts => opts.UseEntityCache(_salesAreaByNameCache)); if (programme.Id == Guid.Empty) { programme.Id = Guid.NewGuid(); } _ = _dbContext.Add(programme, post => post.MapTo(item, opts => opts.UseEntityCache(_salesAreaByIdCache)), _mapper); programme.ProgrammeDictionary = programmeDictionary; foreach (var cat in item.ProgrammeCategories ?? Enumerable.Empty <string>()) { var category = _programmeCategoryCache.GetOrAdd(cat, key => new ProgrammeCategory { Name = key }); if (programme.ProgrammeCategoryLinks.All(x => !string.Equals(x.ProgrammeCategory.Name, category.Name))) { programme.ProgrammeCategoryLinks.Add(new ProgrammeCategoryLink { ProgrammeCategory = category, Programme = programme }); } } if (!(item.Episode is null)) { var episode = programmeDictionary.ProgrammeEpisodes.FirstOrDefault(x => x.Number == item.Episode.Number); if (episode is null) { episode = new ProgrammeEpisode { Name = item.Episode.Name, Number = item.Episode.Number, }; programmeDictionary.ProgrammeEpisodes.Add(episode); } else { episode.Name = item.Episode.Name; } programme.Episode = episode; } }