Main project #1

Merged
raul merged 33 commits from testing into main 2024-06-14 07:49:34 +02:00
2 changed files with 44 additions and 10 deletions
Showing only changes of commit 5c08a756c0 - Show all commits

View File

@ -46,10 +46,28 @@ func autoDetectProvince(c *gin.Context) {
func returnProvince(c *gin.Context) { func returnProvince(c *gin.Context) {
// TODO: Return prettified HTML representation of the weather based on url parameter // 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) { func pullWeatherInfo(localidad string) (root, string, error) {
jsonData, err := getJSON(localidades[codPostal]) jsonData, err := getJSON(localidades[localidad])
if err != nil { if err != nil {
return root{}, jsonData, err return root{}, jsonData, err
} }
@ -62,10 +80,10 @@ func pullWeatherInfo(codPostal string) (root, string, error) {
return aemetRequest, jsonData, nil return aemetRequest, jsonData, nil
} }
func localityIsAvailable(codPostal string) bool { func localityIsAvailable(localidad string) bool {
isAvailable := false isAvailable := false
for k := range localidades { for k := range localidades {
if codPostal == k { if localidad == k {
isAvailable = true isAvailable = true
} }
} }
@ -79,15 +97,15 @@ func returnAPIWeather(c *gin.Context) {
c.Writer.Header().Set("Federal-Agents", "Outside my home") c.Writer.Header().Set("Federal-Agents", "Outside my home")
var n int var n int
var err error var err error
codPostal := c.Param("localidad") localidad := c.Param("localidad")
numDias := c.Param("dia") 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") c.String(http.StatusNotFound, "The locality doesn't exist or is currently not supported, sorry\n")
return return
} }
aemetRequest, _, err := pullWeatherInfo(codPostal) aemetRequest, _, err := pullWeatherInfo(localidad)
if err != nil { if err != nil {
e := fmt.Sprint(err) e := fmt.Sprint(err)
c.String(http.StatusInternalServerError, e) c.String(http.StatusInternalServerError, e)

View File

@ -2,15 +2,31 @@
<html lang="en"> <html lang="en">
<head> <head>
<title>Hello TMPL</title> <title>AEMET Client</title>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link href="style.css" rel="stylesheet"> <link href="style.css" rel="stylesheet">
</head> </head>
<body> <body>
<p>Hello!</p> <p>Localidad: {{ .Base.Nombre }}</p>
<p>{{ .title }}</p>
{{ range .Base.Prediccion.Dia}}
<hr>
<p>Dia: {{ .Fecha }}</p>
<p>
Índice ultravioleta:
{{ if eq .UV ""}}
No disponible!
{{ else }}
{{ .UV }}
{{ end }}
</p>
<!-- <p>Índice ultravioleta: {{ .UV }}</p> -->
{{ end }}
</body> </body>
</html> </html>