func CountryHandler(rw http.ResponseWriter, r *http.Request) {
	var response CountryResponse
	var scoped bool

	country := chi.URLParam(r, "country")
	// Parse the scope query into two dates
	upper, lower, err := ParseScope(r.URL)
	if err != nil {
		log.Printf("Invalid request received: %s", err.Error())
		http.Error(rw, "Bad request: check the scope query.", http.StatusBadRequest)
		return
	}
	if upper == nil {
		scoped = false
	} else {
		scoped = true
	}

	confirmed, recovered, serverErr := getCases(country)
	if serverErr != nil {
		http.Error(rw, serverErr.Error(), serverErr.StatusCode)
	}

	response.Country = confirmed.Country
	response.Continent = confirmed.Continent

	if scoped {
		response.Scope = TimeAsString(*upper) + "-" + TimeAsString(*lower)
		response.Confirmed = confirmed.countInScope(*upper, *lower)
		response.Recovered = recovered.countInScope(*upper, *lower)
	} else {
		response.Scope = "total"
		response.Confirmed = confirmed.latestCount()
		response.Recovered = recovered.latestCount()
	}

	//nolint:gomnd // We want 2 digits of precision, hence 100
	response.PopulationPercentage = math.Round(
		response.Confirmed/confirmed.Population*100,
	) / 100

	err = json.NewEncoder(rw).Encode(response)
	if err != nil {
		log.Printf("Something went wrong: %s", err.Error())
		http.Error(rw, "Something went wrong", http.StatusInternalServerError)
		return
	}
}
// CountryHandler is the handler for the country endpoint.
CountryHandler is the handler for the country endpoint.
// CountryHandler is the handler for the country endpoint .
func CountryHandler(rw http.ResponseWriter, r *http.Request) {
	var response CountryResponse
	var scoped bool
	country := chi.URLParam(r, "country")
	upper, lower, err := ParseScope(r.URL)
	if err != nil {
		log.Printf("Invalid request received: %s", err.Error())
		http.Error(rw, "Bad request: check the scope query.", http.StatusBadRequest)
		return
	}
	if upper == nil {
		scoped = false
	} else {
		scoped = true
	}
	confirmed, recovered, serverErr := getCases(country)
	if serverErr != nil {
		http.Error(rw, serverErr.Error(), serverErr.StatusCode)
	}
	response.Country = confirmed.Country
	response.Continent = confirmed.Continent
	if scoped {
		response.Scope = TimeAsString(*upper) + "-" + TimeAsString(*lower)
		response.Confirmed = confirmed.countInScope(*upper, *lower)
		response.Recovered = recovered.countInScope(*upper, *lower)
	} else {
		response.Scope = "total"
		response.Confirmed = confirmed.latestCount()
		response.Recovered = recovered.latestCount()
	}
	response.PopulationPercentage = math.Round(
		response.Confirmed/confirmed.Population*100,
	) / 100
	err = json.NewEncoder(rw).Encode(response)
	if err != nil {
		log.Printf("Something went wrong: %s", err.Error())
		http.Error(rw, "Something went wrong", http.StatusInternalServerError)
		return
	}
}
func CountryHandler ( rw http . ResponseWriter , r * http . Request ) { var response CountryResponse var scoped bool country := chi . URLParam ( r , " " ) upper , lower , err := ParseScope ( r . URL ) if err != nil { log . Printf ( " " , err . Error ( ) ) http . Error ( rw , " " , http . StatusBadRequest ) return } if upper == nil { scoped = false } else { scoped = true } confirmed , recovered , serverErr := getCases ( country ) if serverErr != nil { http . Error ( rw , serverErr . Error ( ) , serverErr . StatusCode ) } response . Country = confirmed . Country response . Continent = confirmed . Continent if scoped { response . Scope = TimeAsString ( * upper ) + " " + TimeAsString ( * lower ) response . Confirmed = confirmed . countInScope ( * upper , * lower ) response . Recovered = recovered . countInScope ( * upper , * lower ) } else { response . Scope = " " response . Confirmed = confirmed . latestCount ( ) response . Recovered = recovered . latestCount ( ) } response . PopulationPercentage = math . Round ( response . Confirmed / confirmed . Population * 100 , ) / 100 err = json . NewEncoder ( rw ) . Encode ( response ) if err != nil { log . Printf ( " " , err . Error ( ) ) http . Error ( rw , " " , http . StatusInternalServerError ) return } }
CountryHandler is the handler for the country endpoint.
CountryHandler is the handler for the country endpoint .
// Parse the scope query into two dates //nolint:gomnd // We want 2 digits of precision, hence 100