From 5c08a756c0a311c18405bce86d00fd02b1d6cc14 Mon Sep 17 00:00:00 2001 From: raul Date: Wed, 29 May 2024 10:16:27 +0200 Subject: [PATCH] Figure out templating range/ifelse logic --- cmd/serverFunc.go | 32 +++++++++++++++++++++++++------- cmd/templates/index.html | 22 +++++++++++++++++++--- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/cmd/serverFunc.go b/cmd/serverFunc.go index 92dd8f6..d828b3e 100644 --- a/cmd/serverFunc.go +++ b/cmd/serverFunc.go @@ -46,10 +46,28 @@ func autoDetectProvince(c *gin.Context) { func returnProvince(c *gin.Context) { // TODO: Return prettified HTML representation of the weather based on url parameter + localidad := c.Param("local") + if isAv := localityIsAvailable(localidad); isAv != true { + c.String(http.StatusNotFound, "The locality doesn't exist or is currently not supported, sorry\n") + return + } + + aemetRequest, _, err := pullWeatherInfo(localidad) + if err != nil { + e := fmt.Sprint(err) + c.String(http.StatusInternalServerError, e) + //c.String(500, jsonData) + return + } + + c.HTML(http.StatusOK, "templates/index.html", gin.H{ + "Base": aemetRequest.Base, + }) + } -func pullWeatherInfo(codPostal string) (root, string, error) { - jsonData, err := getJSON(localidades[codPostal]) +func pullWeatherInfo(localidad string) (root, string, error) { + jsonData, err := getJSON(localidades[localidad]) if err != nil { return root{}, jsonData, err } @@ -62,10 +80,10 @@ func pullWeatherInfo(codPostal string) (root, string, error) { return aemetRequest, jsonData, nil } -func localityIsAvailable(codPostal string) bool { +func localityIsAvailable(localidad string) bool { isAvailable := false for k := range localidades { - if codPostal == k { + if localidad == k { isAvailable = true } } @@ -79,15 +97,15 @@ func returnAPIWeather(c *gin.Context) { c.Writer.Header().Set("Federal-Agents", "Outside my home") var n int var err error - codPostal := c.Param("localidad") + localidad := c.Param("localidad") numDias := c.Param("dia") - if isAv := localityIsAvailable(codPostal); isAv != true { + if isAv := localityIsAvailable(localidad); isAv != true { c.String(http.StatusNotFound, "The locality doesn't exist or is currently not supported, sorry\n") return } - aemetRequest, _, err := pullWeatherInfo(codPostal) + aemetRequest, _, err := pullWeatherInfo(localidad) if err != nil { e := fmt.Sprint(err) c.String(http.StatusInternalServerError, e) diff --git a/cmd/templates/index.html b/cmd/templates/index.html index db95a99..9524df2 100644 --- a/cmd/templates/index.html +++ b/cmd/templates/index.html @@ -2,15 +2,31 @@ - Hello TMPL + AEMET Client -

Hello!

-

{{ .title }}

+

Localidad: {{ .Base.Nombre }}

+ + {{ range .Base.Prediccion.Dia}} + +
+

Dia: {{ .Fecha }}

+

+ Índice ultravioleta: + {{ if eq .UV ""}} + No disponible! + {{ else }} + {{ .UV }} + {{ end }} +

+ + + {{ end }} +