Exemple #1
0
        public async Task Run()
        {
            /*var client = new RestClient("https://opensky-network.org/datasets/metadata");
             * var request = new RestRequest("aircraftDatabase.csv");
             * client.DownloadData(request).SaveAs("aircraftDatabase.csv");*/
            using (var streamRdr = new StreamReader("aircraftDatabase.csv"))
            {
                var csvReader = new CsvReader(streamRdr, ",");
                List <AircraftDataModel> list = new List <AircraftDataModel>();
                csvReader.Read();
                csvReader.Read();
                while (csvReader.Read())
                {
                    var tmp = new AircraftDataModel
                    {
                        Icao                = csvReader[0],
                        Registration        = csvReader[1] == "" ? null : csvReader[1],
                        ManufacturerIcao    = csvReader[2] == "" ? null : csvReader[2],
                        ManufacturerName    = csvReader[3] == "" ? null : csvReader[3],
                        Model               = csvReader[4] == "" ? null : csvReader[4],
                        TypeCode            = csvReader[5] == "" ? null : csvReader[5],
                        SerialNumber        = csvReader[6] == "" ? null : csvReader[6],
                        LineNumber          = csvReader[7] == "" ? null : csvReader[7],
                        IcaoAircraftType    = csvReader[8] == "" ? null : csvReader[8],
                        Operator            = csvReader[9] == "" ? null : csvReader[9],
                        OperatorCallsign    = csvReader[10] == "" ? null : csvReader[10],
                        OperatorIcao        = csvReader[11] == "" ? null : csvReader[11],
                        OperatorIata        = csvReader[12] == "" ? null : csvReader[12],
                        Owner               = csvReader[13] == "" ? null : csvReader[13],
                        TestReg             = csvReader[14] == "" ? null : csvReader[14],
                        Registered          = ParseDate(csvReader[15]),
                        RegUntil            = ParseDate(csvReader[16]),
                        Status              = csvReader[17] == "" ? null : csvReader[17],
                        Built               = ParseDate(csvReader[18]),
                        FirstFlightDate     = ParseDate(csvReader[19]),
                        SeatConfiguration   = csvReader[20] == "" ? null : csvReader[20],
                        Engines             = csvReader[21] == "" ? null : csvReader[21],
                        Modes               = ParseBoolean(csvReader[22]),
                        Adsb                = ParseBoolean(csvReader[23]),
                        Acars               = ParseBoolean(csvReader[24]),
                        Notes               = csvReader[25] == "" ? null : csvReader[25],
                        CategoryDescription = csvReader[26] == "" ? null : csvReader[26],
                    };

                    list.Add(tmp);
                    if (list.Count > 10000)
                    {
                        await _context.Aircrafts.UpsertRange(list).On(x => x.Icao).RunAsync();

                        await _context.SaveChangesAsync();

                        list.Clear();
                    }
                }
            }
        }
        public async Task <IActionResult> Plane(string icao)
        {
            var c = await _redis.Db.StringGetAsync(icao);

            if (c.ToString() != null)
            {
                return(Content(c));
            }

            var plane = _context.Aircrafts.FirstOrDefault(a => a.Icao == icao);

            if (plane == null)
            {
                var client   = new RestClient("https://api.joshdouch.me/api/aircraft/");
                var request  = new RestRequest(icao, DataFormat.Json);
                var response = await client.GetAsync <ApiJoshDouchModel>(request);

                if (response.Status == "404")
                {
                    return(Content("[]", "application/json"));
                }

                var toAdd = new AircraftDataModel
                {
                    Icao             = response.ModeS.ToLower(),
                    ManufacturerName = response.Manufacturer,
                    Model            = response.Type,
                    TypeCode         = response.ICAOTypeCode,
                    Owner            = response.RegisteredOwners,
                    OperatorIcao     = response.OperatorFlagCode
                };
                await _context.Aircrafts.AddAsync(toAdd);

                await _context.SaveChangesAsync();

                var json = JsonConvert.SerializeObject(toAdd);
                await _redis.Db.StringSetAsync(icao, json, TimeSpan.FromHours(1));

                return(Content(json, "application/json"));
            }

            var jsonPlane = JsonConvert.SerializeObject(plane);
            await _redis.Db.StringSetAsync(icao, jsonPlane, TimeSpan.FromHours(1));

            return(Content(jsonPlane, "application/json"));
        }