private void DeleteColumnsIfNeeded(List <FieldConfigurationServiceModel> deleteData) { var idsToDelete = deleteData.Select(v => v.Id).ToList(); using (var repo = new RepositoryPattern <FieldConfiguration>()) { var dataToDelete = repo.SelectAll().Where(v => idsToDelete.Contains(v.Id)).ToList(); repo.BulkDelete(dataToDelete); } using (var repo = new RepositoryPattern <FieldMappingConfiguration>()) { var dataToDelete = repo.SelectAll().Where(v => idsToDelete.Contains(v.FieldConfigurationId ?? 0)).ToList(); repo.BulkDelete(dataToDelete); } }
public string UpdateTableAndfieldconfiguration(TableAndFieldConfigurationServiceModel tableAndFieldConfiguration) { try { if (tableAndFieldConfiguration == null) { //return Exception; } var dataToInsert = new List <FieldConfiguration>(); var dataToUpdate = new List <FieldConfiguration>(); var idsToDeleteFieldMapping = new List <FieldMappingConfiguration>(); using (var repo = new RepositoryPattern <TableConfiguration>()) { var detailsToUpdate = Mapper.Map <TableConfiguration>(tableAndFieldConfiguration.tableConfiguration); repo.Update(detailsToUpdate); repo.Save(); } tableAndFieldConfiguration.fieldConfiguration.ForEach(f => { f.TableConfigId = tableAndFieldConfiguration.tableConfiguration.Id; f.IsDisplay = true; }); dataToInsert = Mapper.Map <List <FieldConfiguration> >(tableAndFieldConfiguration.fieldConfiguration.Where(f => f.Id == default(int)).ToList()); dataToUpdate = Mapper.Map <List <FieldConfiguration> >(tableAndFieldConfiguration.fieldConfiguration.Where(u => u.Id != default(int)).ToList()); var configurationIds = dataToUpdate.Select(i => i.Id).ToList(); using (var repo = new RepositoryPattern <FieldConfiguration>()) { //Update existing records which will not display in future var records = repo.SelectAll().Where(f => f.TableConfigId == tableAndFieldConfiguration.tableConfiguration.Id && !configurationIds.Any(c => c == f.Id)).Select(c => { c.IsDisplay = false; return(c); }).ToList(); if (records.Any()) { dataToUpdate.AddRange(records); } if (dataToUpdate.Any()) { repo.BulkUpdate(dataToUpdate); } if (dataToInsert.Any()) { repo.BulkInsert(dataToInsert); } idsToDeleteFieldMapping.AddRange(repo.SelectAll().Where(h => h.TableConfigId == tableAndFieldConfiguration.tableConfiguration.Id && h.FieldMappingConfigurations.Any()) .SelectMany(g => g.FieldMappingConfigurations).ToList()); } tableAndFieldConfiguration.fieldConfiguration.ForEach(field => { if (field.Id == default(int)) { var dataWithIds = dataToInsert.FirstOrDefault(a => a.SourceColumnName == field.SourceColumnName && a.TableConfigId == field.TableConfigId && a.ReferenceTableName == field.ReferenceTableName && a.ReferenceColumnName == field.ReferenceColumnName && field.IsDisplay).Id; field.Id = dataWithIds; } }); List <FieldMappingConfiguration> fieldMappingConfigurations = new List <FieldMappingConfiguration>(); tableAndFieldConfiguration.fieldConfiguration.ForEach(fields => { if (fields.MappedCoumns.Any()) { fields.MappedCoumns.ForEach(m => { var mappingDetails = new FieldMappingConfiguration() { FieldConfigurationId = fields.Id, MapColumnName = m, MapTableName = fields.ReferenceTableName, }; fieldMappingConfigurations.Add(mappingDetails); }); } }); using (var repo = new RepositoryPattern <FieldMappingConfiguration>()) { if (idsToDeleteFieldMapping.Any()) { repo.BulkDelete(idsToDeleteFieldMapping); } if (fieldMappingConfigurations.Any()) { repo.BulkInsert(fieldMappingConfigurations); } } return("Table and field mapping Save succesfully"); } catch (Exception ex) { throw; } }