Skip to content
Snippets Groups Projects
Commit 1f179a4e authored by Mathilde Hertaas's avatar Mathilde Hertaas
Browse files

updatet user feedback in population

parent d875847c
Branches
No related tags found
No related merge requests found
......@@ -54,4 +54,7 @@ const DATA = "data"
//extern API
const COUNTRIESNOW_API = "http://129.241.150.113:3500/api/v0.1/countries"
const RESTCOUNTRIES_API = "http://129.241.150.113:8080/v3.1/all"
\ No newline at end of file
const RESTCOUNTRIES_API = "http://129.241.150.113:8080/v3.1/all"
//population
const YEAR_LIMIT_REMINDER = "You can also specify how many years to display with {?<startYear-endYear>}.\n"
\ No newline at end of file
......@@ -47,7 +47,13 @@ func getPopulationForCountry(alpha2Code string, startYear, endYear int) (*Popula
// Konverter ISO 3166-1 alpha-2 til alpha-3
alpha3Code, exists := isoAlpha2ToAlpha3[strings.ToUpper(alpha2Code)]
if !exists {
return nil, fmt.Errorf("invalid country code: %s", alpha2Code)
return nil, fmt.Errorf(
ERROR_RETRIEVING_COUNTRY_DATA +
COUNTRY_CODE_REMINDER+
YEAR_LIMIT_REMINDER+
EXAMPLE_URL +
ASSISTANCE_REMINDER,
alpha2Code,POPULATION_URL_EXAMPLE, HOMEPAGE_DEFAULT)
}
// Finn landet basert på ISO 3166-1 alpha-3 kode
......@@ -59,19 +65,11 @@ func getPopulationForCountry(alpha2Code string, startYear, endYear int) (*Popula
}
}
if countryData == nil {
return nil, fmt.Errorf("country not found")
}
// Filtrer basert på år
// en tom liste fyllt med PopulationCount, altså fylles med spesifikke år og antall
var filteredValues []PopulationYears
var total int
count := 0
// itererer over alle verdiene som følger med et land sin populasjonsdata
// over alle årstall-verdi strukturene
for _, stk := range countryData.PopulationCounts {
if (startYear == 0 || stk.Year >= startYear) && (endYear == 0 || stk.Year <= endYear) {
filteredValues = append(filteredValues, stk)
......@@ -80,7 +78,6 @@ func getPopulationForCountry(alpha2Code string, startYear, endYear int) (*Popula
}
}
// Beregn gjennomsnitt
mean := 0
if count > 0 {
mean = int(math.Round(float64(total) / float64(count)))
......@@ -94,9 +91,8 @@ func getPopulationForCountry(alpha2Code string, startYear, endYear int) (*Popula
}
func PopulationHandler(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/countryinfo/v1/population/")
// NO
countryCode := strings.ToUpper(path) // ISO 3166-2-kode
path := strings.TrimPrefix(r.URL.Path, POPULATION_DEFAULT)
countryCode := strings.ToUpper(path) // ISO 3166-2
query := r.URL.RawQuery
limit := strings.Replace(query, "limit=", "", 1)
......@@ -119,6 +115,6 @@ func PopulationHandler(w http.ResponseWriter, r *http.Request) {
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set(HEADER_CONTENT_TYPE, FORMAT_JSON)
json.NewEncoder(w).Encode(result)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment