move H to pkg
This commit is contained in:
parent
145028af37
commit
84ec54a893
@ -1,3 +1,3 @@
|
||||
package app
|
||||
package pkg
|
||||
|
||||
type H map[string]any
|
||||
@ -4,7 +4,7 @@ import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"io"
|
||||
"servicea/internal/app"
|
||||
"pkg"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@ -74,7 +74,7 @@ func (c *CSV) Parse(r io.Reader) ([]MeteoData, []RejectedMeteoData, error) {
|
||||
|
||||
rowValue := strings.Join(row, ";")
|
||||
|
||||
record := make(app.H)
|
||||
record := make(pkg.H)
|
||||
for i, value := range row {
|
||||
if i < len(header) {
|
||||
record[header[i]] = value
|
||||
@ -104,7 +104,7 @@ func (c *CSV) Parse(r io.Reader) ([]MeteoData, []RejectedMeteoData, error) {
|
||||
return meteoDataList, rejectedDataList, nil
|
||||
}
|
||||
|
||||
func normalize(record app.H) (*MeteoData, error) {
|
||||
func normalize(record pkg.H) (*MeteoData, error) {
|
||||
meteoData := &MeteoData{}
|
||||
|
||||
var err error
|
||||
@ -142,7 +142,7 @@ func normalize(record app.H) (*MeteoData, error) {
|
||||
return meteoData, nil
|
||||
}
|
||||
|
||||
func parseDate(record app.H, key string, errMissing error) (time.Time, error) {
|
||||
func parseDate(record pkg.H, key string, errMissing error) (time.Time, error) {
|
||||
if str, ok := record[key].(string); ok && str != "" {
|
||||
t, err := time.Parse("2006/01/02", str)
|
||||
if err != nil {
|
||||
@ -153,14 +153,14 @@ func parseDate(record app.H, key string, errMissing error) (time.Time, error) {
|
||||
return time.Time{}, errMissing
|
||||
}
|
||||
|
||||
func parseString(record app.H, key string, errMissing error) (string, error) {
|
||||
func parseString(record pkg.H, key string, errMissing error) (string, error) {
|
||||
if str, ok := record[key].(string); ok && str != "" {
|
||||
return str, nil
|
||||
}
|
||||
return "", errMissing
|
||||
}
|
||||
|
||||
func parseFloatField(record app.H, key string, errMissing error) (float32, error) {
|
||||
func parseFloatField(record pkg.H, key string, errMissing error) (float32, error) {
|
||||
if str, ok := record[key].(string); ok && str != "" {
|
||||
str = strings.Replace(str, ",", ".", 1)
|
||||
f, err := strconv.ParseFloat(str, 32)
|
||||
@ -172,7 +172,7 @@ func parseFloatField(record app.H, key string, errMissing error) (float32, error
|
||||
return 0, errMissing
|
||||
}
|
||||
|
||||
func parseIntField(record app.H, key string, errMissing error) (int, error) {
|
||||
func parseIntField(record pkg.H, key string, errMissing error) (int, error) {
|
||||
if str, ok := record[key].(string); ok && str != "" {
|
||||
str = strings.TrimSpace(str)
|
||||
i, err := strconv.Atoi(str)
|
||||
|
||||
@ -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})
|
||||
}
|
||||
|
||||
@ -1,3 +1 @@
|
||||
package app
|
||||
|
||||
type H map[string]any
|
||||
|
||||
@ -4,7 +4,6 @@ import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"pkg"
|
||||
"serviceb/internal/app"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
@ -32,14 +31,14 @@ 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
|
||||
}
|
||||
|
||||
data, err := h.s.GetWeatherByCity(r.Context(), params)
|
||||
if err != nil {
|
||||
slog.Error("error", "err", err)
|
||||
h.ToJSON(w, http.StatusInternalServerError, app.H{"error": err})
|
||||
h.ToJSON(w, http.StatusInternalServerError, pkg.H{"error": err})
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user