From 1f179a4eb491dcb3d7b2a9ff3dc35c9107870656 Mon Sep 17 00:00:00 2001
From: Mathilde Hertaas <maimh@stud.ntnu.no>
Date: Thu, 6 Mar 2025 20:56:02 +0100
Subject: [PATCH] updatet user feedback in population

---
 handelers/constants.go         |  5 ++++-
 handelers/populasjonhandler.go | 26 +++++++++++---------------
 2 files changed, 15 insertions(+), 16 deletions(-)

diff --git a/handelers/constants.go b/handelers/constants.go
index 5e73a1c..def4dd1 100644
--- a/handelers/constants.go
+++ b/handelers/constants.go
@@ -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
diff --git a/handelers/populasjonhandler.go b/handelers/populasjonhandler.go
index 581725e..eedb0c2 100644
--- a/handelers/populasjonhandler.go
+++ b/handelers/populasjonhandler.go
@@ -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)
 }
-- 
GitLab