code refactoring
This commit is contained in:
@@ -11,29 +11,23 @@ import (
|
||||
"github.com/ceticamarco/zephyr/types"
|
||||
)
|
||||
|
||||
// Structure representing the JSON response
|
||||
type tempRes struct {
|
||||
Min float64 `json:"min"`
|
||||
Max float64 `json:"max"`
|
||||
}
|
||||
|
||||
type fsRes struct {
|
||||
Day float64 `json:"day"`
|
||||
}
|
||||
|
||||
type weatherRes struct {
|
||||
Title string `json:"main"`
|
||||
Description string `json:"description"`
|
||||
Icon string `json:"icon"`
|
||||
}
|
||||
|
||||
// Structures representing the JSON response
|
||||
type dailyRes struct {
|
||||
Temp tempRes `json:"temp"`
|
||||
FeelsLike fsRes `json:"feels_like"`
|
||||
Weather []weatherRes `json:"weather"`
|
||||
WindSpeed float64 `json:"wind_speed"`
|
||||
WindDeg float64 `json:"wind_deg"`
|
||||
Timestamp int64 `json:"dt"`
|
||||
Temp struct {
|
||||
Min float64 `json:"min"`
|
||||
Max float64 `json:"max"`
|
||||
} `json:"temp"`
|
||||
FeelsLike struct {
|
||||
Day float64 `json:"day"`
|
||||
} `json:"feels_like"`
|
||||
Weather []struct {
|
||||
Title string `json:"main"`
|
||||
Description string `json:"description"`
|
||||
Icon string `json:"icon"`
|
||||
} `json:"weather"`
|
||||
WindSpeed float64 `json:"wind_speed"`
|
||||
WindDeg float64 `json:"wind_deg"`
|
||||
Timestamp int64 `json:"dt"`
|
||||
}
|
||||
|
||||
type forecastRes struct {
|
||||
@@ -76,7 +70,6 @@ func getForecastEntity(dailyForecast dailyRes) types.ForecastEntity {
|
||||
Speed: strconv.FormatFloat(dailyForecast.WindSpeed, 'f', 2, 64),
|
||||
},
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func GetForecast(city *types.City, apiKey string) (types.Forecast, error) {
|
||||
|
||||
@@ -31,17 +31,15 @@ func GetMetrics(city *types.City, apiKey string) (types.Metrics, error) {
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
// Structures representing the JSON response
|
||||
type CurrentRes struct {
|
||||
Humidity int `json:"humidity"`
|
||||
Pressure int `json:"pressure"`
|
||||
DewPoint float64 `json:"dew_point"`
|
||||
UvIndex float64 `json:"uvi"`
|
||||
Visibility float64 `json:"visibility"`
|
||||
}
|
||||
|
||||
// Structure representing the JSON response
|
||||
type MetricsRes struct {
|
||||
Current CurrentRes `json:"current"`
|
||||
Current struct {
|
||||
Humidity int `json:"humidity"`
|
||||
Pressure int `json:"pressure"`
|
||||
DewPoint float64 `json:"dew_point"`
|
||||
UvIndex float64 `json:"uvi"`
|
||||
Visibility float64 `json:"visibility"`
|
||||
} `json:"current"`
|
||||
}
|
||||
|
||||
var metricRes MetricsRes
|
||||
|
||||
@@ -63,20 +63,18 @@ func GetWeather(city *types.City, apiKey string) (types.Weather, error) {
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
// Structures representing the JSON response
|
||||
type InfoRes struct {
|
||||
Title string `json:"main"`
|
||||
Description string `json:"description"`
|
||||
Icon string `json:"icon"`
|
||||
}
|
||||
type CurrentRes struct {
|
||||
FeelsLike float64 `json:"feels_like"`
|
||||
Temperature float64 `json:"temp"`
|
||||
Timestamp int64 `json:"dt"`
|
||||
Weather []InfoRes `json:"weather"`
|
||||
}
|
||||
// Structure representing the JSON response
|
||||
type WeatherRes struct {
|
||||
Current CurrentRes `json:"current"`
|
||||
Current struct {
|
||||
FeelsLike float64 `json:"feels_like"`
|
||||
Temperature float64 `json:"temp"`
|
||||
Timestamp int64 `json:"dt"`
|
||||
Weather []struct {
|
||||
Title string `json:"main"`
|
||||
Description string `json:"description"`
|
||||
Icon string `json:"icon"`
|
||||
} `json:"weather"`
|
||||
} `json:"current"`
|
||||
}
|
||||
|
||||
var weather WeatherRes
|
||||
|
||||
@@ -62,14 +62,12 @@ func GetWind(city *types.City, apiKey string) (types.Wind, error) {
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
// Structures representing the JSON response
|
||||
type CurrentRes struct {
|
||||
Speed float64 `json:"wind_speed"`
|
||||
Degrees float64 `json:"wind_deg"`
|
||||
}
|
||||
|
||||
// Structure representing the JSON response
|
||||
type WindRes struct {
|
||||
Current CurrentRes `json:"current"`
|
||||
Current struct {
|
||||
Speed float64 `json:"wind_speed"`
|
||||
Degrees float64 `json:"wind_deg"`
|
||||
} `json:"current"`
|
||||
}
|
||||
|
||||
var windRes WindRes
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -39,8 +40,8 @@ func InitCache() *Caches {
|
||||
}
|
||||
}
|
||||
|
||||
func (cache *Cache[T]) GetEntry(key string, ttl int8) (T, bool) {
|
||||
val, isPresent := cache.Data[key]
|
||||
func (cache *Cache[T]) GetEntry(cityName string, ttl int8) (T, bool) {
|
||||
val, isPresent := cache.Data[strings.ToUpper(cityName)]
|
||||
|
||||
// If key is not present, return a zero value
|
||||
if !isPresent {
|
||||
@@ -60,11 +61,10 @@ func (cache *Cache[T]) GetEntry(key string, ttl int8) (T, bool) {
|
||||
func (cache *Cache[T]) AddEntry(entry T, cityName string) {
|
||||
currentTime := time.Now()
|
||||
|
||||
cache.Data[cityName] = CacheEntity[T]{
|
||||
cache.Data[strings.ToUpper(cityName)] = CacheEntity[T]{
|
||||
element: entry,
|
||||
timestamp: currentTime,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (moon *CacheEntity[Moon]) GetEntry(ttl int8) (Moon, bool) {
|
||||
@@ -86,8 +86,6 @@ func (moon *CacheEntity[Moon]) GetEntry(ttl int8) (Moon, bool) {
|
||||
}
|
||||
|
||||
func (cache *CacheEntity[Moon]) AddEntry(entry Moon) {
|
||||
currentTime := time.Now()
|
||||
|
||||
cache.element = entry
|
||||
cache.timestamp = currentTime
|
||||
cache.timestamp = time.Now()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user