add base handler and refactor H

This commit is contained in:
2025-10-28 21:38:58 +01:00
parent fb4d31afba
commit c7024d7f4e
7 changed files with 118 additions and 22 deletions
+44 -6
View File
@@ -4,15 +4,18 @@ import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"servicea/internal/app"
"servicea/internal/domains"
"strconv"
"time"
)
type Handler struct {
domains.BaseHandler
*Service
}
@@ -68,13 +71,48 @@ func (h *Handler) IngestCSV(w http.ResponseWriter, r *http.Request) {
"elapsed_ms", fileStats.ElapsedMS,
"file_checksum", fileStats.FileChecksum,
)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(H{
"stats": fileStats,
})
h.ToJSON(w, http.StatusOK, app.H{"stats": fileStats})
}
func (h *Handler) IngestExcel(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello from excel")
}
func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) {
queryParams := r.URL.Query()
pageInt := int(1)
limitInt := int(10)
page := queryParams.Get("page")
limit := queryParams.Get("limit")
if page != "" {
p, err := strconv.Atoi(page)
if err == nil {
pageInt = p
}
}
if limit != "" {
l, err := strconv.Atoi(limit)
if err == nil {
limitInt = l
} else {
limitInt = 10
}
}
params := GetMeteoData{
Location: queryParams.Get("city"),
From: queryParams.Get("from"),
To: queryParams.Get("to"),
Page: pageInt,
Limit: limitInt,
}
if err := params.Validate(); err != nil {
h.ToJSON(w, http.StatusBadRequest, app.H{"error": err.Error()})
}
slog.Info("params", "params", params)
}