Example #1
0
        public static async Task SeedStations(IExternalClientContext clientContext, DataContext context, IMapper mapper)
        {
            if (await context.Stations.AnyAsync() && await context.Norms.AnyAsync())
            {
                return;
            }

            if (await context.Stations.AnyAsync() == false)
            {
                var stationsDTO = await clientContext.GetStationsAsync();

                var communes = stationsDTO.Select(x => x.City.Commune)
                               .GroupBy(x => new { x.CommuneName, x.DistrictName, x.ProvinceName })
                               .Select(x => x.First()).ToList();
                var provinces = communes.Select(x => x)
                                .GroupBy(x => x.ProvinceName)
                                .Select(x => x.First()).OrderBy(x => x.ProvinceName).Select(x => x.ProvinceName.ToUpper()).ToList();
                var districts = communes.Select(x => x)
                                .GroupBy(x => new { x.DistrictName, x.ProvinceName })
                                .Select(x => x.First()).OrderBy(x => x.DistrictName).Select(x => new { x.DistrictName, x.ProvinceName }).ToList();
                var cities = stationsDTO.Select(x => x.City)
                             .GroupBy(x => new { x.Id, x.Commune.CommuneName })
                             .Select(x => x.First()).OrderBy(x => x.Name).Select(x => x).ToList();

                var provincesList = new List <Province>();
                foreach (var provinceDTO in provinces)
                {
                    var province = new Province()
                    {
                        Id           = provinces.IndexOf(provinceDTO) + 1,
                        ProvinceName = provinceDTO,
                        Districts    = new List <District>()
                    };
                    foreach (var districtDTO in districts.Where(x => x.ProvinceName.ToUpper() == province.ProvinceName))
                    {
                        var district = new District()
                        {
                            Id           = districts.IndexOf(districtDTO) + 1,
                            DistrictName = districtDTO.DistrictName.ToUpper(),
                            ProvienceId  = province.Id,
                            Communes     = new List <Commune>()
                        };
                        foreach (var communeDTO in communes.Where(x => x.DistrictName.ToUpper() == district.DistrictName &&
                                                                  x.ProvinceName.ToUpper() == province.ProvinceName))
                        {
                            var commune = new Commune()
                            {
                                Id          = communes.IndexOf(communeDTO) + 1,
                                CommuneName = communeDTO.CommuneName.ToUpper(),
                                DistrictId  = district.Id,
                                Cities      = new List <City>()
                            };

                            foreach (var cityDTO in cities.Where(x => x.Commune.CommuneName.ToUpper() == commune.CommuneName))
                            {
                                var city = mapper.Map <City>(cityDTO);
                                city.Stations = stationsDTO.Where(x => x.City.Id == city.Id)
                                                .Select(x => mapper.Map <Station>(x))
                                                .ToList();
                                commune.Cities.Add(city);
                            }
                            district.Communes.Add(commune);
                        }
                        province.Districts.Add(district);
                    }
                    provincesList.Add(province);
                }

                context.AddRange(provincesList);
            }

            if (await context.Stations.AnyAsync() == false)
            {
                var normsData = await System.IO.File.ReadAllTextAsync("Data/NormsSeed.json");

                var norms = JsonSerializer.Deserialize <List <Norm> >(normsData);

                context.Norms.AddRange(norms);
            }


            await context.SaveChangesAsync();
        }
Example #2
0
 public UnitOfWork(DataContext context, IMapper mapper, IExternalClientContext clientContext)
 {
     _context       = context;
     _mapper        = mapper;
     _clientContext = clientContext;
 }
Example #3
0
 public StationRepository(DataContext context, IExternalClientContext clientContext, IMapper mapper)
 {
     _context = context;
     _clientContext = clientContext;
     _mapper = mapper;
 }
Example #4
0
 public SensorRepository(IExternalClientContext clientContext, IMapper mapper, DataContext context)
 {
     _clientContext = clientContext;
     _mapper        = mapper;
     _context       = context;
 }