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

fixed wrong pathway in population endpoint

parent a44e94d1
Branches
No related tags found
No related merge requests found
......@@ -43,13 +43,13 @@ http://localhost:8080/countryinfo/v1/population/{country_code}{?startyear-endYea
```
* startYear-endYear er en valfri parameter som gir begreninger på spesifikke år du får populasjonsdata fra (Default er alle tilgjengelige år)
* Metode: GET
* Eksempel: http://localhost:8080/population/NO?limit=2010-2015
* Eksempel: http://localhost:8080/countryinfo/v1/population/NO?limit=2010-2015
### status
Viser status for de eksterne API-ene, versjon og applikasjonens oppetid.
```sh
http://localhost:8080/countryinfo/v1/status
http://localhost:8080/status
```
......
......@@ -3,7 +3,7 @@ package handelers
//URL
const HOMEPAGE_DEFAULT = "http://localhost:8080"
const INFO_DEFAULT = "/countryinfo/v1/info/"
const POPULATION_DEFAULT = "/population/"
const POPULATION_DEFAULT = "/countryinfo/v1/population/"
const STATUS_DEFAULT = "/status/"
const SLASH_DEFAULT = "/"
......@@ -27,7 +27,7 @@ const STATUS_DESCRIPTION = "Retrieve information about the API status."
const STATUS_URL = "http://localhost:8080/status/"
const POPULATION_DESCRIPTION = "It requires the ISO 3166-2 country code and a optional limit of the years in the repsonse"
const POPULATION_URL = "http://localhost:8080/countryinfo/v1/population/{ISO_3166-2_country_code}{?startYear-endYear}"
const POPULATION_URL_EXAMPLE = "http://localhost:8080/countryinfo/v1/population/NO?2010-2015}"
const POPULATION_URL_EXAMPLE = "http://localhost:8080/countryinfo/v1/population/NO?2010-2015"
//status
const CNA = "countriesnowapi"
......
......@@ -66,7 +66,7 @@ func CountryInfoHandler(w http.ResponseWriter, r *http.Request) {
Population: country.Population,
Languages: country.Languages,
Borders: country.Borders,
Flags: country.Flags.PNG,
Flags: country.Flags,
Capital: "",
Cities: strings.Join(cities, ", "),
}
......
......@@ -9,7 +9,7 @@ import (
"strings"
)
// ISO 3166-1 alpha-2 til alpha-3 landkoder
// ISO 3166-1 alpha-2 to alpha-3 countrycodes
var isoAlpha2ToAlpha3 = make(map[string]string)
// Henter befolkningsdata fra eksternt API
......@@ -66,7 +66,7 @@ func getPopulationForCountry(alpha2Code string, startYear, endYear int) (*Popula
// Filtrer basert på år
// en tom liste fyllt med PopulationCount, altså fylles med spesifikke år og antall
var filteredValues []PopulationCount
var filteredValues []PopulationYears
var total int
count := 0
......@@ -94,17 +94,13 @@ func getPopulationForCountry(alpha2Code string, startYear, endYear int) (*Popula
}
func PopulationHandler(w http.ResponseWriter, r *http.Request) {
// r.URL.Path er den fulle URL som ble sendt til serveren altså /population/NO
// deretter fjerner den /population/ så vi sitter igjen med NO
path := strings.TrimPrefix(r.URL.Path, "/population/")
path := strings.TrimPrefix(r.URL.Path, "/countryinfo/v1/population/")
// NO
countryCode := strings.ToUpper(path) // ISO 3166-2-kode
query := r.URL.RawQuery
limit := strings.Replace(query, "limit=", "", 1)
startYear, endYear := 0, 0
if len(limit) > 0 {
......
package handelers
type Flags struct{
PNG string `json:"png"`
}
// Struktur for data fra REST Countries API
type CountryInfo struct {
Name struct {
Common string `json:"common"`
......@@ -12,54 +7,44 @@ type CountryInfo struct {
} `json:"name"`
Capital []string `json:"capital"`
Languages map[string]string `json:"languages"`
Continents []string `json:"continents"`
Continents []string `json:"continent"`
Borders []string `json:"borders"`
Population int `json:"population"`
Flags Flags `json:"flags"`
Flags string `json:"flag"`
}
//type CityInfo struct {
// Cities []string `json:"cities"`
//}
// Struktur for kombinert respons i info
type CombinedInfo struct {
Name string `json:"name"`
Continents []string `json:"continenents"`
Continents []string `json:"continent"`
Population int `json:"population"`
Languages map[string]string `json:"languages"`
Borders []string `json:"borders"`
Flags string `json:"flags"`
Flags string `json:"flag"`
Capital string `json:"capital"`
Cities string `json:"cities"`
}
// responsstruktur for befolkningsdata
type PopulationResponse struct {
Error bool `json:"error"`
Msg string `json:"msg"`
Data []Country `json:"data"`
}
// land og dens befolkningsdata
type Country struct {
Name string `json:"country"`
Code string `json:"iso3"` // Trebokstavskode (ISO 3166-1 alpha-3)
PopulationCounts []PopulationCount `json:"populationCounts"`
Code string `json:"iso3"` //(ISO 3166-1 alpha-3)
PopulationCounts []PopulationYears `json:"populationCounts"`
}
// befolkningsdate for et bestemt år (brukes i den endelige repsonsen under values)
type PopulationCount struct {
type PopulationYears struct {
Year int `json:"year"`
Value int `json:"value"`
}
// Responsstruktur for vårt API(det som vises på skjerm)
type PopulationAPIResponse struct {
Name string `json:"land"`
Gjennomsnitt int `json:"gjennomsnittlig populasjon"`
Values []PopulationCount `json:"årlige populasjoner"`
Name string `json:"country"`
Gjennomsnitt int `json:"average population"`
Values []PopulationYears `json:"yearly population"`
}
type Response struct {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment