/** * Create the list of regions of the template * * @param parsed object of the JSON * @param id of the template * @param list of regions of the template */ private List <ITemplate> CreateListOfRegions(JObject parsedObject, object KeyTemplate, ref List <ITemplate> listRegions) { // get the regions in JSON format var templatesFillRegionsJson = parsedObject[CommonVariables.KEY_JSON_TEMPLATES_FILL_REGIONS].ToString(Formatting.None); // deserialize the JSON in classes var obj = JsonConvert.DeserializeObject <List <TemplatesFillRegions> >(templatesFillRegionsJson); TemplatesFillRegions templateRegion = null; try { // get the regions of the current template templateRegion = obj.Where(i => i.TemplateName.Equals(KeyTemplate)).First(); } catch (Exception) { } // if the template has regions if (templateRegion != null) { // for each region in the template foreach (int idRegion in templateRegion.Regions) { // the id regions are the index position in the array int id = idRegion - 1; try { // if the id region is in the range of the regions if (id < 0 || id >= listTemplateRegions.Length) { throw new IndexOutOfRangeException("Please, check the index of the region"); } // get the region and its associated products IDictionary <string, object> region = listTemplateRegions[id]; TypeProductsInRegions regionsInProducts = null; var list = listRegionsInProducts.Where(i => i.RegionName == (int)(long)region["Id"]); if (list.Any()) { regionsInProducts = list.First(); } // add to the list of regions if (region.ContainsKey(CommonVariables.KEY_JSON_REGIONS_REGION_NAME)) { listRegions.Add(GetRegion(((string)region[CommonVariables.KEY_JSON_REGIONS_REGION_NAME]).Trim().ToLower(), ref region, ref regionsInProducts, TextGeneratorProvider)); } } catch (Exception) { throw new Exception("Please check the definition of the region id " + idRegion); } } } return(listRegions); }
protected virtual Region GetRegion(string regionName, ref IDictionary <string, object> region, ref TypeProductsInRegions regionsInProducts, string TextGeneratorProvider) { IDictionary <string, object>[] type = null; if (regionsInProducts != null) { type = regionsInProducts.Products; } return(regionName switch { "price" => new RegionPrice(ref region, type, TextGeneratorProvider), "color" => new RegionColor(ref region, type, TextGeneratorProvider), _ => new Region(ref region, type, TextGeneratorProvider), });