public async Task <DataSourceDTO> Register(DataSourceTransferDTO dto) { NetworkEntity datamartEntity = DataContext.NetworkEntities.Add(new NetworkEntity { NetworkID = dto.NetworkID.Value, NetworkEntityID = dto.ID, EntityType = EntityType.DataSource }); Data.DataSource datamart = DataContext.DataSources.Add(new Data.DataSource() { ID = datamartEntity.ID, Name = dto.Name, Acronym = dto.Acronym, OrganizationID = dto.OrganizationID, AdapterSupportedID = dto.AdapterSupportedID }); await DataContext.SaveChangesAsync(); if (dto.Metadata != null && dto.Metadata.Count() > 0) { IList <Data.DataSourceDomainData> metadata = new List <Data.DataSourceDomainData>(); foreach (var meta in dto.Metadata) { var orgMeta = new DataSourceDomainData() { DataSourceID = datamart.ID, DomainUseID = meta.DomainUseID, Value = meta.Value, SequenceNumber = meta.SequenceNumber, }; if (meta.DomainReferenceID.HasValue) { orgMeta.DomainReferenceID = meta.DomainReferenceID.Value; } metadata.Add(orgMeta); } if (metadata.Count > 0) { DataContext.DomainDatas.AddRange(metadata); } await DataContext.SaveChangesAsync(); } await DataContext.Entry(datamart).Reference(dm => dm.AdapterSupported).LoadAsync(); return(datamart.Map <DataSource, DataSourceDTO>()); }
public async Task <DataSourceDTO> Update(DataSourceTransferDTO dto) { var datamart = await DataContext.DataSources.FindAsync(dto.ID); if (datamart.Deleted) { throw new Exception("The Specified DataSource was not found"); } datamart.Name = dto.Name; datamart.Acronym = dto.Acronym; datamart.OrganizationID = dto.OrganizationID; datamart.AdapterSupportedID = dto.AdapterSupportedID; await DataContext.SaveChangesAsync(); var dataMartMetaData = await DataContext.DomainDatas.OfType <DataSourceDomainData>().Where(x => x.DataSourceID == dto.ID).ToArrayAsync(); if (dto.Metadata != null && dto.Metadata.Count() > 0) { IList <DataSourceDomainData> metaDataToAdd = new List <DataSourceDomainData>(); foreach (var meta in dto.Metadata.Where(m => !m.ID.HasValue)) { var dsMeta = new DataSourceDomainData() { DataSourceID = dto.ID, DomainUseID = meta.DomainUseID, Value = meta.Value, SequenceNumber = meta.SequenceNumber, }; if (meta.DomainReferenceID.HasValue) { dsMeta.DomainReferenceID = meta.DomainReferenceID.Value; } metaDataToAdd.Add(dsMeta); } if (metaDataToAdd.Count > 0) { DataContext.DomainDatas.AddRange(metaDataToAdd); } foreach (var meta in dataMartMetaData.Where(org => dto.Metadata.Any(m => m.ID == org.ID && (org.Value != m.Value || org.SequenceNumber != m.SequenceNumber || org.DomainReferenceID != m.DomainReferenceID)))) { var diff = dto.Metadata.Where(m => m.ID == meta.ID).FirstOrDefault(); DataContext.DomainDatas.Attach(meta); if (meta.Value != diff.Value) { meta.Value = diff.Value; } if (meta.SequenceNumber != diff.SequenceNumber) { meta.SequenceNumber = diff.SequenceNumber; } if (meta.DomainReferenceID != diff.DomainReferenceID) { meta.DomainReferenceID = diff.DomainReferenceID; } } await DataContext.SaveChangesAsync(); } var metadataIDs = dto.Metadata.Where(x => x.ID.HasValue).Select(x => x.ID.Value); var remove = (from d in dataMartMetaData where !metadataIDs.Contains(d.ID) && d.DataSourceID == dto.ID select d.ID).ToArray(); if (remove.Count() > 0) { //Have to do this cause of trigger var removeIDs = String.Join(",", remove.Select(x => String.Format("'{0}'", x))); await DataContext.Database.ExecuteSqlCommandAsync(string.Format("delete from DataSourceDomainData where ID IN ({0})", removeIDs)); } await DataContext.Entry(datamart).Reference(dm => dm.AdapterSupported).LoadAsync(); return(datamart.Map <DataSource, DataSourceDTO>()); }