Esempio n. 1
0
        public async Task <ActionResult> InsertFullDownload([FromBody] FileParameters fileParameters)
        {
            //02-13-2020.csv
            //MM-dd-yyyy

            int year  = 0;
            int month = 0;
            int day   = 0;

            bool success;

            string fileName = fileParameters.Date + ".csv";

            if (fileParameters == null)
            {
                return(BadRequest("FileParameters is null"));
            }
            else if (fileParameters.Date.Length != 10)
            {
                return(BadRequest("FileParameters.Date.Length != 10"));
            }

            success = Int32.TryParse(fileParameters.Date.Substring(0, 2), out month);
            if (!success || month > 12)
            {
                return(BadRequest("Invalid month value"));
            }

            success = Int32.TryParse(fileParameters.Date.Substring(3, 2), out day);
            if (!success || month > 31)
            {
                return(BadRequest("Invalid day value"));
            }

            success = Int32.TryParse(fileParameters.Date.Substring(6, 4), out year);
            if (!success)
            {
                return(BadRequest("Invalid year value"));
            }

            var existingDownload = _downloadRepository.GetDownloadByFileName(fileName);

            if (existingDownload != null)
            {
                //detailOperations.DeleteDetailsByDownloadId(existingDownload.Id);
                //downloadOperations.DeleteDownloadByFileName(fileName);

                _downloadRepository.DeleteDownloadCascadingByFileName(fileName);
            }

            var fileNamePath = fileParameters.Url + fileName;

            Download download = new Download
            {
                DownloadedDate     = DateTime.Now,
                DownloadedFileName = fileParameters.Date + ".csv"
            };



            var engine = new FileHelperEngine <CSSEGISandDataDailyReport>();

            try
            {
                using (var result = new StreamReader(await client.GetStreamAsync(fileNamePath)))
                {
                    rows = engine.ReadStream(result).ToList();
                }

                details = rows.Select(x => new Detail
                {
                    Province_State = x.Province_State,
                    Country_Region = x.Country_Region,
                    Last_Update    = x.Last_Update,
                    Latitude       = x.Lat,
                    Longitude      = x.Long_,
                    Confirmed      = x.Confirmed,
                    Deaths         = x.Deaths,
                    Recovered      = x.Recovered,
                    Active         = x.Active
                }).ToList();

                //download.Details = details;

                _downloadRepository.InsertFullDownload(download, details);
            }
            catch (Exception ex)
            {
                var message = ex.Message;
                Console.WriteLine(ex.ToString());
                return(StatusCode(500));
            }

            return(Ok());
        }