Skip to content
Snippets Groups Projects
Commit 00a2c1dc authored by Andreas Magnarson Nypan's avatar Andreas Magnarson Nypan
Browse files

Upload New File

parent 42a6bd68
No related branches found
No related tags found
No related merge requests found
Pipeline #30876 canceled
package handlers
import (
"CountryAPI/utils"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
)
func HandlePopulation(rw http.ResponseWriter, r *http.Request) {
pathParts := strings.Split(r.URL.Path, "/")
countryCode := pathParts[len(pathParts)-1]
startYear, endYear := 0, 0
myQuery := r.URL.Query()
limitString := myQuery.Get("limit")
if limitString != "" {
//extracting the years and splitting them
years := strings.Split(limitString, "-")
var err1, err2 error
startYear, err1 = strconv.Atoi(years[0]) // converting the years into ints
endYear, err2 = strconv.Atoi(years[1])
if err1 != nil || err2 != nil {
http.Error(rw, "Seems to be a invalid year range or format", http.StatusBadRequest)
}
}
populationData, err := utils.FetchPopulationData(countryCode, startYear, endYear)
if err != nil {
http.Error(rw, fmt.Sprintf("Error fetching population data: %v", err), http.StatusInternalServerError)
return
}
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(http.StatusOK)
json.NewEncoder(rw).Encode(populationData)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment