This commit is contained in:
Pedro Pérez 2025-10-28 23:39:13 +01:00
parent b38e77218e
commit 5560fad92e
3 changed files with 15 additions and 7 deletions

View File

@ -6,12 +6,12 @@ import (
) )
type MeteoData struct { type MeteoData struct {
Timestamp time.Time `csv:"fecha"` Timestamp time.Time `csv:"fecha" json:"timestamp"`
Location string `csv:"ciudad"` Location string `csv:"ciudad" json:"location"`
MaxTemp float32 `csv:"temperatura maxima"` MaxTemp float32 `csv:"temperatura maxima" json:"max_temp"`
MinTemp float32 `csv:"temperatura minima"` MinTemp float32 `csv:"temperatura minima" json:"min_temp"`
Rainfall float32 `csv:"precipitacion"` Rainfall float32 `csv:"precipitacion" json:"rainfall"`
Cloudiness int `csv:"nubosidad"` Cloudiness int `csv:"nubosidad" json:"cloudiness"`
} }
type RejectedMeteoData struct { type RejectedMeteoData struct {

View File

@ -70,6 +70,7 @@ func (h *Handler) IngestCSV(w http.ResponseWriter, r *http.Request) {
"elapsed_ms", fileStats.ElapsedMS, "elapsed_ms", fileStats.ElapsedMS,
"file_checksum", fileStats.FileChecksum, "file_checksum", fileStats.FileChecksum,
) )
h.ToJSON(w, http.StatusOK, app.H{"stats": fileStats}) h.ToJSON(w, http.StatusOK, app.H{"stats": fileStats})
} }
@ -77,6 +78,9 @@ func (h *Handler) IngestExcel(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello from excel") fmt.Fprintf(w, "hello from excel")
} }
func (h *Handler) GetCities(w http.ResponseWriter, r *http.Request) {
}
func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) { func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) {
queryParams := r.URL.Query() queryParams := r.URL.Query()
@ -89,16 +93,19 @@ func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) {
} }
if err := params.Validate(); err != nil { if err := params.Validate(); err != nil {
slog.Error("Error validating struct", "error", err)
h.ToJSON(w, http.StatusBadRequest, app.H{"error": err.Error()}) h.ToJSON(w, http.StatusBadRequest, app.H{"error": err.Error()})
return return
} }
meteoData, err := h.s.GetMeteoData(r.Context(), params) meteoData, err := h.s.GetMeteoData(r.Context(), params)
if err != nil { if err != nil {
slog.Error(ErrReadingData.Error(), "error", err)
h.ToJSON(w, http.StatusNotFound, app.H{"error": ErrReadingData.Error()}) h.ToJSON(w, http.StatusNotFound, app.H{"error": ErrReadingData.Error()})
return return
} }
slog.Info("Data retrieved", "location", params.Location)
h.ToJSON(w, http.StatusOK, app.H{"meteo_data": meteoData}) h.ToJSON(w, http.StatusOK, app.H{"meteo_data": meteoData})
return
} }

View File

@ -6,5 +6,6 @@ func RegisterRoutes(mux *http.ServeMux, handler *Handler) {
mux.HandleFunc("POST /ingest/csv", handler.IngestCSV) mux.HandleFunc("POST /ingest/csv", handler.IngestCSV)
mux.HandleFunc("POST /ingest/excel", handler.IngestExcel) mux.HandleFunc("POST /ingest/excel", handler.IngestExcel)
mux.HandleFunc("GET /cities", handler.GetCities)
mux.HandleFunc("GET /data", handler.GetMeteoData) mux.HandleFunc("GET /data", handler.GetMeteoData)
} }