@@ -4,7 +4,6 @@ package handlers
4
4
import (
5
5
"encoding/json"
6
6
"fmt"
7
- "math"
8
7
"net/http"
9
8
"os"
10
9
"sort"
@@ -53,7 +52,15 @@ func WeatherForecastHandler(c *gin.Context) {
53
52
return
54
53
}
55
54
56
- forecastList := openWeatherData ["list" ].([]interface {})
55
+ forecastList , ok := openWeatherData ["list" ].([]interface {})
56
+ if ! ok {
57
+ Respond (c , http .StatusInternalServerError , models.APIResponse {
58
+ Message : "invalid forecast data format" ,
59
+ Success : false ,
60
+ })
61
+ return
62
+ }
63
+
57
64
forecastResponse := & models.ForecastResponse {
58
65
Name : openWeatherData ["city" ].(map [string ]interface {})["name" ].(string ),
59
66
}
@@ -76,7 +83,6 @@ func WeatherForecastHandler(c *gin.Context) {
76
83
sort .Strings (dates )
77
84
78
85
now := time .Now ().UTC ()
79
- currentTimeOfDaySeconds := now .Hour ()* 3600 + now .Minute ()* 60 + now .Second ()
80
86
81
87
datesProcessed := 0
82
88
for _ , dateStr := range dates {
@@ -89,21 +95,20 @@ func WeatherForecastHandler(c *gin.Context) {
89
95
continue
90
96
}
91
97
92
- if datesProcessed >= 3 {
93
- break
94
- }
95
-
96
98
forecasts := forecastsByDate [dateStr ]
97
99
98
- minTimeDiff := int64 (1 << 63 - 1 )
99
100
var closestForecast map [string ]interface {}
101
+ minTimeDiff := int64 (1 << 63 - 1 )
100
102
101
103
for _ , fMap := range forecasts {
102
104
fDt := int64 (fMap ["dt" ].(float64 ))
103
105
fTime := time .Unix (fDt , 0 ).UTC ()
104
106
105
- fTimeOfDaySeconds := fTime .Hour () * 3600 + fTime .Minute () * 60 + fTime .Second ()
106
- timeDiff := int64 (math .Abs (float64 (fTimeOfDaySeconds - currentTimeOfDaySeconds )))
107
+ if fTime .Before (now ) {
108
+ continue
109
+ }
110
+
111
+ timeDiff := fDt - now .Unix ()
107
112
108
113
if timeDiff < minTimeDiff {
109
114
minTimeDiff = timeDiff
@@ -112,32 +117,53 @@ func WeatherForecastHandler(c *gin.Context) {
112
117
}
113
118
114
119
if closestForecast != nil {
115
- condition := closestForecast ["weather" ].([]interface {})[0 ].(map [string ]interface {})
116
- temperature := closestForecast ["main" ].(map [string ]interface {})
117
- sunrise := int64 (openWeatherData ["city" ].(map [string ]interface {})["sunrise" ].(float64 ))
118
- sunset := int64 (openWeatherData ["city" ].(map [string ]interface {})["sunset" ].(float64 ))
120
+ conditionList , ok := closestForecast ["weather" ].([]interface {})
121
+ if ! ok || len (conditionList ) == 0 {
122
+ continue
123
+ }
124
+ conditionMap , ok := conditionList [0 ].(map [string ]interface {})
125
+ if ! ok {
126
+ continue
127
+ }
128
+
129
+ temperatureMap , ok := closestForecast ["main" ].(map [string ]interface {})
130
+ if ! ok {
131
+ continue
132
+ }
133
+
134
+ cityMap , ok := openWeatherData ["city" ].(map [string ]interface {})
135
+ if ! ok {
136
+ continue
137
+ }
138
+
139
+ sunrise := int64 (cityMap ["sunrise" ].(float64 ))
140
+ sunset := int64 (cityMap ["sunset" ].(float64 ))
119
141
120
142
dt := int64 (closestForecast ["dt" ].(float64 ))
121
143
t := time .Unix (dt , 0 ).UTC ()
122
144
dayOfWeek := int64 (t .Weekday ())
123
145
124
146
forecastResponse .Forecast = append (forecastResponse .Forecast , & models.Forecast {
125
147
Condition : & models.Condition {
126
- Name : condition ["main" ].(string ),
127
- Description : condition ["description" ].(string ),
148
+ Name : conditionMap ["main" ].(string ),
149
+ Description : conditionMap ["description" ].(string ),
128
150
},
129
151
Temperature : & models.Temperature {
130
- Current : temperature ["temp" ].(float64 ),
131
- Min : temperature ["temp_min" ].(float64 ),
132
- Max : temperature ["temp_max" ].(float64 ),
152
+ Current : temperatureMap ["temp" ].(float64 ),
153
+ Min : temperatureMap ["temp_min" ].(float64 ),
154
+ Max : temperatureMap ["temp_max" ].(float64 ),
133
155
},
134
156
Sunrise : sunrise ,
135
157
Sunset : sunset ,
136
158
DayOfWeek : dayOfWeek ,
137
159
})
138
- }
139
160
140
- datesProcessed ++
161
+ datesProcessed ++
162
+
163
+ if datesProcessed >= 3 {
164
+ break
165
+ }
166
+ }
141
167
}
142
168
143
169
Respond (c , http .StatusOK , forecastResponse )
0 commit comments