move H to pkg
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"pkg"
|
||||
"servicea/internal/app"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -28,14 +27,14 @@ func (h *Handler) IngestCSV(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseMultipartForm(10 << 20)
|
||||
if err != nil {
|
||||
slog.Error(ErrParsingForm.Error(), "error", err)
|
||||
h.ToJSON(w, http.StatusBadRequest, app.H{"error": ErrParsingForm})
|
||||
h.ToJSON(w, http.StatusBadRequest, pkg.H{"error": ErrParsingForm})
|
||||
return
|
||||
}
|
||||
|
||||
file, header, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
slog.Error(ErrRetrievingFile.Error(), "error", err)
|
||||
h.ToJSON(w, http.StatusBadRequest, app.H{"error": ErrRetrievingFile})
|
||||
h.ToJSON(w, http.StatusBadRequest, pkg.H{"error": ErrRetrievingFile})
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
@@ -43,7 +42,7 @@ func (h *Handler) IngestCSV(w http.ResponseWriter, r *http.Request) {
|
||||
content, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
slog.Error(ErrReadingFile.Error(), "error", err)
|
||||
h.ToJSON(w, http.StatusInternalServerError, app.H{"error": ErrReadingFile})
|
||||
h.ToJSON(w, http.StatusInternalServerError, pkg.H{"error": ErrReadingFile})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -60,7 +59,7 @@ func (h *Handler) IngestCSV(w http.ResponseWriter, r *http.Request) {
|
||||
slog.Error(ErrCannotParseFile.Error(),
|
||||
"filename", header.Filename,
|
||||
"error", err)
|
||||
h.ToJSON(w, http.StatusConflict, app.H{"error": err})
|
||||
h.ToJSON(w, http.StatusConflict, pkg.H{"error": err})
|
||||
return
|
||||
}
|
||||
fileStats.ElapsedMS = int(time.Since(start).Milliseconds())
|
||||
@@ -74,7 +73,7 @@ func (h *Handler) IngestCSV(w http.ResponseWriter, r *http.Request) {
|
||||
"file_checksum", fileStats.FileChecksum,
|
||||
)
|
||||
|
||||
h.ToJSON(w, http.StatusOK, app.H{"stats": fileStats})
|
||||
h.ToJSON(w, http.StatusOK, pkg.H{"stats": fileStats})
|
||||
}
|
||||
|
||||
func (h *Handler) IngestExcel(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -84,7 +83,7 @@ func (h *Handler) IngestExcel(w http.ResponseWriter, r *http.Request) {
|
||||
func (h *Handler) GetCities(w http.ResponseWriter, r *http.Request) {
|
||||
cities := h.s.GetCities(r.Context())
|
||||
slog.Info("cities retrieved", "count", len(cities))
|
||||
h.ToJSON(w, http.StatusOK, app.H{"cities": cities})
|
||||
h.ToJSON(w, http.StatusOK, pkg.H{"cities": cities})
|
||||
}
|
||||
|
||||
func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -100,18 +99,18 @@ func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
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, pkg.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
meteoData, err := h.s.GetMeteoData(r.Context(), params)
|
||||
if err != nil {
|
||||
slog.Error(ErrReadingData.Error(), "error", err)
|
||||
h.ToJSON(w, http.StatusNotFound, app.H{"error": ErrReadingData.Error()})
|
||||
h.ToJSON(w, http.StatusNotFound, pkg.H{"error": ErrReadingData.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
slog.Info("data retrieved", "location", params.Location)
|
||||
|
||||
h.ToJSON(w, http.StatusOK, app.H{"meteo_data": meteoData})
|
||||
h.ToJSON(w, http.StatusOK, pkg.H{"meteo_data": meteoData})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user