Ejemplo n.º 1
0
        public void AddFeeder(string type, int typeId, Feeder feeder)
        {
            feeder.Transformers = new List <Transformer>();
            feeder.Id           = typeId;
            var data   = System.IO.File.ReadAllText(string.Format(configuration.AppSettings.DbTablesFilePath, "Address.json"));
            var states = JsonConvert.DeserializeObject <List <State> >(data);

            if (type.Equals("Village", StringComparison.InvariantCultureIgnoreCase))
            {
                var village = states.SelectMany(p => p.Districts.SelectMany(q => q.Taluks.SelectMany(r => r.Villages))).FirstOrDefault(p => p.Id == feeder.Id);
                if (village != null)
                {
                    var feeders = village.Feeders != null?village.Feeders.ToList() : new List <Feeder>();

                    feeder.Id = FeederExtensions.GetNextFeederId(states);
                    feeders.Add(feeder);
                    village.Feeders = feeders;
                }
            }
            else if (type.Equals("Taluk", StringComparison.InvariantCultureIgnoreCase))
            {
                var taluk = states.SelectMany(p => p.Districts.SelectMany(q => q.Taluks)).FirstOrDefault(p => p.Id == feeder.Id);
                if (taluk != null)
                {
                    var feeders = taluk.Feeders != null?taluk.Feeders.ToList() : new List <Feeder>();

                    feeder.Id = FeederExtensions.GetNextFeederId(states);
                    feeders.Add(feeder);
                    taluk.Feeders = feeders;
                }
            }
            else if (type.Equals("District", StringComparison.InvariantCultureIgnoreCase))
            {
                var district = states.SelectMany(p => p.Districts).FirstOrDefault(p => p.Id == feeder.Id);
                if (district != null)
                {
                    var feeders = district.Feeders != null?district.Feeders.ToList() : new List <Feeder>();

                    feeder.Id = FeederExtensions.GetNextFeederId(states);
                    feeders.Add(feeder);
                    district.Feeders = feeders;
                }
            }

            System.IO.File.WriteAllText(string.Format(configuration.AppSettings.DbTablesFilePath, "Address.json"), JsonConvert.SerializeObject(states));
        }
Ejemplo n.º 2
0
        public void AddVillage(int stateId, int districtId, int talukId, Village village)
        {
            var data   = System.IO.File.ReadAllText(string.Format(configuration.AppSettings.DbTablesFilePath, "Address.json"));
            var states = JsonConvert.DeserializeObject <List <State> >(data);

            if (states.Any())
            {
                var state    = states.FirstOrDefault(p => p.Id == stateId);
                var district = state.Districts.FirstOrDefault(p => p.Id == districtId);
                var taluk    = district.Taluks.FirstOrDefault(p => p.Id == talukId);

                if (taluk != null)
                {
                    var vls = states.SelectMany(p => p.Districts.SelectMany(q => q.Taluks.SelectMany(r => r.Villages)));
                    var t   = vls.Any() ? vls.Max(p => p.Id) : 0;
                    village.Id = t + 1;

                    if (village.Feeders != null && village.Feeders.Any())
                    {
                        var feeder = village.Feeders.First();
                        feeder.Id           = FeederExtensions.GetNextFeederId(states);
                        feeder.Transformers = new List <Transformer>();
                        village.Feeders     = new List <Feeder> {
                            feeder
                        };
                    }
                    else
                    {
                        village.Feeders = new List <Feeder>();
                    }

                    var villages = taluk.Villages != null?taluk.Villages.ToList() : new List <Village>();

                    villages.Add(village);
                    taluk.Villages = villages;
                }
            }

            System.IO.File.WriteAllText(string.Format(configuration.AppSettings.DbTablesFilePath, "Address.json"), JsonConvert.SerializeObject(states));
        }
Ejemplo n.º 3
0
        public void AddDistrict(int stateId, District district)
        {
            var data   = System.IO.File.ReadAllText(string.Format(configuration.AppSettings.DbTablesFilePath, "Address.json"));
            var states = JsonConvert.DeserializeObject <List <State> >(data);

            if (states.Any())
            {
                district.Taluks = new List <Taluk>();
                var ds = states.SelectMany(p => p.Districts);
                var t  = ds.Any() ? ds.Max(p => p.Id) : 0;
                district.Id = t + 1;

                if (district.Feeders != null && district.Feeders.Any())
                {
                    var feeder = district.Feeders.First();
                    feeder.Id           = FeederExtensions.GetNextFeederId(states);
                    feeder.Transformers = new List <Transformer>();
                    district.Feeders    = new List <Feeder> {
                        feeder
                    };
                }
                else
                {
                    district.Feeders = new List <Feeder>();
                }

                var state     = states.FirstOrDefault(p => p.Id == stateId);
                var districts = state.Districts != null?state.Districts.ToList() : new List <District>();

                districts.Add(district);

                state.Districts = districts;
            }

            System.IO.File.WriteAllText(string.Format(configuration.AppSettings.DbTablesFilePath, "Address.json"), JsonConvert.SerializeObject(states));
        }