add service b
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
package app
|
||||
|
||||
type H map[string]any
|
||||
@@ -0,0 +1,90 @@
|
||||
package meteo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type MeteoDataPerDay struct {
|
||||
MaxTemp float32 `json:"max_temp"`
|
||||
MinTemp float32 `json:"min_temp"`
|
||||
AvgTemp float32 `json:"avg_temp"`
|
||||
Rainfall float32 `json:"rainfall"`
|
||||
Cloudiness int `json:"cloudiness"`
|
||||
}
|
||||
|
||||
func (mtpd *MeteoDataPerDay) ConvertValue() {
|
||||
mtpd.MaxTemp = mtpd.MaxTemp*9/5 + 32
|
||||
mtpd.MinTemp = mtpd.MinTemp*9/5 + 32
|
||||
mtpd.AvgTemp = mtpd.AvgTemp*9/5 + 32
|
||||
}
|
||||
|
||||
type Rolling7Data struct {
|
||||
AvgTemp float32 `json:"avg_temp"`
|
||||
AvgCloudiness int `json:"avg_cloudiness"`
|
||||
SumRainfall float32 `json:"sum_rainfall"`
|
||||
}
|
||||
|
||||
type MeteoData struct {
|
||||
Location string `json:"location"`
|
||||
Unit Unit `json:"unit"`
|
||||
From string `json:"from"`
|
||||
Days *[]MeteoDataPerDay `json:"days,omitempty"`
|
||||
Rolling7 *Rolling7Data `json:"rolling7,omitempty"`
|
||||
}
|
||||
|
||||
type Unit string
|
||||
|
||||
const (
|
||||
UnitC Unit = "C"
|
||||
UnitF Unit = "F"
|
||||
)
|
||||
|
||||
type Agg string
|
||||
|
||||
const (
|
||||
AggDaily Agg = "daily"
|
||||
AggRolling7 Agg = "rolling7"
|
||||
)
|
||||
|
||||
type GetMeteoData struct {
|
||||
Location string
|
||||
Date string
|
||||
Days int
|
||||
Unit Unit
|
||||
Agg Agg
|
||||
}
|
||||
|
||||
func (mt *GetMeteoData) Validate() error {
|
||||
if mt.Date == "" {
|
||||
mt.Date = time.Now().Format("2006-01-02")
|
||||
}
|
||||
|
||||
if _, err := time.Parse("2006-01-02", mt.Date); err != nil {
|
||||
return ErrMissingOrInvalidDate
|
||||
}
|
||||
|
||||
if mt.Days == 0 {
|
||||
mt.Days = 5
|
||||
}
|
||||
if mt.Days < 1 || mt.Days > 10 {
|
||||
return ErrInvalidDays
|
||||
}
|
||||
|
||||
if mt.Unit == "" {
|
||||
mt.Unit = UnitC
|
||||
}
|
||||
|
||||
if mt.Agg == "" {
|
||||
mt.Agg = AggDaily
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
ErrCityNotFound = errors.New("city not found")
|
||||
ErrReadingData = errors.New("error reading data")
|
||||
ErrMissingOrInvalidDate = errors.New("missing or invalid date")
|
||||
ErrInvalidDays = errors.New("days must be between 1 and 10")
|
||||
)
|
||||
@@ -0,0 +1,40 @@
|
||||
package meteo
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"pkg"
|
||||
"serviceb/internal/app"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
pkg.BaseHandler
|
||||
s *Service
|
||||
}
|
||||
|
||||
func NewHandler(service *Service) *Handler {
|
||||
return &Handler{
|
||||
s: service,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) {
|
||||
locationValue := r.PathValue("city")
|
||||
queryParams := r.URL.Query()
|
||||
|
||||
params := GetMeteoData{
|
||||
Location: locationValue,
|
||||
Date: queryParams.Get("date"),
|
||||
Days: h.ParamToInt(queryParams.Get("days"), 5),
|
||||
Unit: Unit(queryParams.Get("unit")),
|
||||
Agg: Agg(queryParams.Get("agg")),
|
||||
}
|
||||
|
||||
if err := params.Validate(); err != nil {
|
||||
slog.Error("error validating struct", "error", err)
|
||||
h.ToJSON(w, http.StatusBadRequest, app.H{"error": err.Error()})
|
||||
}
|
||||
|
||||
slog.Info("loc value", "value", params)
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package meteo
|
||||
|
||||
import "net/http"
|
||||
|
||||
func RegisterRoutes(mux *http.ServeMux, handler *Handler) {
|
||||
mux.HandleFunc("GET /weather/{city}", handler.GetMeteoData)
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package meteo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type inMemory struct {
|
||||
data map[string]MeteoData
|
||||
mu *sync.RWMutex
|
||||
}
|
||||
type Service struct {
|
||||
inMemory
|
||||
}
|
||||
|
||||
func NewService() *Service {
|
||||
return &Service{}
|
||||
}
|
||||
|
||||
func (s *Service) GetWeatherByCity(ctx context.Context, params GetMeteoData) ([]MeteoData, error) {
|
||||
return []MeteoData{}, nil
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func SetupRoutes() *http.ServeMux {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "hello world")
|
||||
})
|
||||
|
||||
return mux
|
||||
}
|
||||
Reference in New Issue
Block a user