コード例 #1
0
        public async Task <ActionResult <string> > GetCovidDataFromPublicAPI()
        {
            // Returns all cases by case type for a country from the first recorded case.
            HttpResponseMessage response = await client.GetAsync("https://api.covid19api.com/dayone/country/croatia");

            if (response.IsSuccessStatusCode)
            {
                List <CovidData> covidList;
                var responseJson = await response.Content.ReadAsStringAsync();

                try
                {
                    covidList = JsonConvert.DeserializeObject <List <CovidData> >(responseJson);
                }
                catch (Exception)
                {
                    return(BadRequest("Error while data serialization."));
                }

                foreach (var covidData in covidList)
                {
                    if (!CovidDataExists(covidData.CountryCode, covidData.Date))
                    {
                        _context.CovidData.Add(covidData);
                    }
                }

                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateException)
                {
                    return(BadRequest("Error while saving in DB."));
                }

                return(Ok(responseJson));
            }
            return("Can not get response from api.covid19api.com. Status: " + response.StatusCode.ToString());
        }
コード例 #2
0
        public async Task <IActionResult> AddCountryToDB(CountryClass country)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    context.Countries.Add(new CountryClass {
                        Country = country.Country, Slug = country.Slug, Code = country.Code
                    });
                    await context.SaveChangesAsync();

                    return(Ok(new Response {
                        Status = "Success", Message = "Successful Insertion", Data = country.Id
                    }));
                }
                catch (DbUpdateException ex)
                {
                    return(Ok(new Response {
                        Status = "Error", Message = ex.Message, Data = null
                    }));
                }
            }
            return(BadRequest());
        }