public async Task <String> PopulateDatabase() { List <Nace> savedNaces = await ds.getAllNaces(); List <Region> savedRegions = await ds.getAllRegions(); List <EuroStatTable> savedTables = await ds.getAllEuroStatTables(); foreach (Nace nace in baseData.getAllNaces()) { try { if (!savedNaces.Contains(nace)) { await ds.createNace(nace); } } catch (Exception exception) { _logger.LogError(exception.ToString()); return(exception.ToString()); } } foreach (Region region in baseData.getAllRegions()) { try { if (!savedRegions.Contains(region)) { await ds.createRegion(region); } } catch (Exception exception) { _logger.LogError(exception.ToString()); return(exception.ToString()); } } foreach (EuroStatTable table in baseData.getAllEuroStatTables()) { try { if (!savedTables.Contains(table)) { await ds.createEuroStatTable(table); } } catch (Exception exception) { _logger.LogError(exception.ToString()); return(exception.ToString()); } } return("The base data was added successfully"); }
/// <summary> /// Converts a JSON-file on string format to a list of corresponding NaceRegionData objects. /// </summary> /// <param name="jsonString">A JSON object represented as a string</param> /// <param name="attributeName">The name of the datafield. Used to find and set the correct property in NaceRegionObjects</param> /// <returns></returns> public async Task <List <NaceRegionData> > Convert(string jsonString, string attributeName) { un = new EuroStatJSONUnNester(jsonString); List <NaceRegionData> nrdList = new List <NaceRegionData>(); List <String> naceRegionYearFields = new List <String>(); List <int> numberOfItemsInFields = new List <int>(); if (!un.IsValidDataset()) { return(nrdList); } List <Region> regions = await databaseStore.getAllRegions(); List <Nace> naces = await databaseStore.getAllNaces(); foreach (KeyValuePair <String, String> entry in un.GetValues()) { List <int> totalElementsList = un.GetFields().ConvertAll(field => field.totalElements); List <int> indexes = findIndexesOfFields(int.Parse(entry.Key), totalElementsList); int naceId = indexes[un.GetFieldIndex("nace")]; int regionId = indexes[un.GetFieldIndex("region")]; int yearId = indexes[un.GetFieldIndex("year")]; string naceCode = un.GetNaceCode(naceId: naceId); string regionCode = un.GetRegionCode(regionId: regionId); int year = un.GetYear(yearId: yearId); double propValue = double.Parse(entry.Value, CultureInfo.InvariantCulture); Nace nace = naces.Find(nace => nace.naceCode == naceCode); Region region = regions.Find(region => region.regionCode == regionCode); if (nace != null && region != null) { NaceRegionData nrd = new NaceRegionData() { naceId = nace.naceId, regionId = region.regionId, year = year }; Type type = nrd.GetType(); PropertyInfo prop = type.GetProperty(attributeName); prop.SetValue(nrd, propValue, null); nrdList.Add(nrd); } } return(nrdList); }
public async Task <ActionResult <IEnumerable <Region> > > GetAll() { return(await databaseStore.getAllRegions()); }