add service b
This commit is contained in:
@@ -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")
|
||||
)
|
||||
Reference in New Issue
Block a user