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
|
type H map[string]any
|
||||||
@ -4,7 +4,7 @@ import (
|
|||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"servicea/internal/app"
|
"pkg"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -74,7 +74,7 @@ func (c *CSV) Parse(r io.Reader) ([]MeteoData, []RejectedMeteoData, error) {
|
|||||||
|
|
||||||
rowValue := strings.Join(row, ";")
|
rowValue := strings.Join(row, ";")
|
||||||
|
|
||||||
record := make(app.H)
|
record := make(pkg.H)
|
||||||
for i, value := range row {
|
for i, value := range row {
|
||||||
if i < len(header) {
|
if i < len(header) {
|
||||||
record[header[i]] = value
|
record[header[i]] = value
|
||||||
@ -104,7 +104,7 @@ func (c *CSV) Parse(r io.Reader) ([]MeteoData, []RejectedMeteoData, error) {
|
|||||||
return meteoDataList, rejectedDataList, nil
|
return meteoDataList, rejectedDataList, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalize(record app.H) (*MeteoData, error) {
|
func normalize(record pkg.H) (*MeteoData, error) {
|
||||||
meteoData := &MeteoData{}
|
meteoData := &MeteoData{}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
@ -142,7 +142,7 @@ func normalize(record app.H) (*MeteoData, error) {
|
|||||||
return meteoData, nil
|
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 != "" {
|
if str, ok := record[key].(string); ok && str != "" {
|
||||||
t, err := time.Parse("2006/01/02", str)
|
t, err := time.Parse("2006/01/02", str)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -153,14 +153,14 @@ func parseDate(record app.H, key string, errMissing error) (time.Time, error) {
|
|||||||
return time.Time{}, errMissing
|
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 != "" {
|
if str, ok := record[key].(string); ok && str != "" {
|
||||||
return str, nil
|
return str, nil
|
||||||
}
|
}
|
||||||
return "", errMissing
|
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 != "" {
|
if str, ok := record[key].(string); ok && str != "" {
|
||||||
str = strings.Replace(str, ",", ".", 1)
|
str = strings.Replace(str, ",", ".", 1)
|
||||||
f, err := strconv.ParseFloat(str, 32)
|
f, err := strconv.ParseFloat(str, 32)
|
||||||
@ -172,7 +172,7 @@ func parseFloatField(record app.H, key string, errMissing error) (float32, error
|
|||||||
return 0, errMissing
|
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 != "" {
|
if str, ok := record[key].(string); ok && str != "" {
|
||||||
str = strings.TrimSpace(str)
|
str = strings.TrimSpace(str)
|
||||||
i, err := strconv.Atoi(str)
|
i, err := strconv.Atoi(str)
|
||||||
|
|||||||
@ -9,7 +9,6 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"pkg"
|
"pkg"
|
||||||
"servicea/internal/app"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -28,14 +27,14 @@ func (h *Handler) IngestCSV(w http.ResponseWriter, r *http.Request) {
|
|||||||
err := r.ParseMultipartForm(10 << 20)
|
err := r.ParseMultipartForm(10 << 20)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error(ErrParsingForm.Error(), "error", err)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
file, header, err := r.FormFile("file")
|
file, header, err := r.FormFile("file")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error(ErrRetrievingFile.Error(), "error", err)
|
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
|
return
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
@ -43,7 +42,7 @@ func (h *Handler) IngestCSV(w http.ResponseWriter, r *http.Request) {
|
|||||||
content, err := io.ReadAll(file)
|
content, err := io.ReadAll(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error(ErrReadingFile.Error(), "error", err)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,7 +59,7 @@ func (h *Handler) IngestCSV(w http.ResponseWriter, r *http.Request) {
|
|||||||
slog.Error(ErrCannotParseFile.Error(),
|
slog.Error(ErrCannotParseFile.Error(),
|
||||||
"filename", header.Filename,
|
"filename", header.Filename,
|
||||||
"error", err)
|
"error", err)
|
||||||
h.ToJSON(w, http.StatusConflict, app.H{"error": err})
|
h.ToJSON(w, http.StatusConflict, pkg.H{"error": err})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fileStats.ElapsedMS = int(time.Since(start).Milliseconds())
|
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,
|
"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) {
|
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) {
|
func (h *Handler) GetCities(w http.ResponseWriter, r *http.Request) {
|
||||||
cities := h.s.GetCities(r.Context())
|
cities := h.s.GetCities(r.Context())
|
||||||
slog.Info("cities retrieved", "count", len(cities))
|
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) {
|
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 {
|
if err := params.Validate(); err != nil {
|
||||||
slog.Error("error validating struct", "error", err)
|
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
|
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)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Info("data retrieved", "location", params.Location)
|
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
|
package app
|
||||||
|
|
||||||
type H map[string]any
|
|
||||||
|
|||||||
@ -4,7 +4,6 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"pkg"
|
"pkg"
|
||||||
"serviceb/internal/app"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
@ -32,14 +31,14 @@ 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)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := h.s.GetWeatherByCity(r.Context(), params)
|
data, err := h.s.GetWeatherByCity(r.Context(), params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("error", "err", err)
|
slog.Error("error", "err", err)
|
||||||
h.ToJSON(w, http.StatusInternalServerError, app.H{"error": err})
|
h.ToJSON(w, http.StatusInternalServerError, pkg.H{"error": err})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user