@@ -13,64 +13,55 @@ func noCache(c echo.Context) {
13
13
c .Response ().Header ().Set (echo .HeaderCacheControl , "private, no-cache, no-store, max-age=0, must-revalidate" )
14
14
}
15
15
16
- func NewAirportsHandler (dh * data.Handler ) echo.HandlerFunc {
16
+ func NewAirportsEndpoint (dh * data.Handler ) echo.HandlerFunc {
17
17
return func (c echo.Context ) error {
18
18
airports , err := dh .Airports (c .Request ().Context ())
19
- if err != nil {
20
- noCache (c )
21
-
22
- if errors .Is (err , context .DeadlineExceeded ) {
23
- return echo .NewHTTPError (http .StatusRequestTimeout , err )
24
- }
25
-
26
- return echo .NewHTTPError (http .StatusInternalServerError )
27
- }
28
-
29
- return c .JSON (http .StatusOK , airports )
19
+ return jsonResponse (c , airports , err , func (v data.AirportsResponse ) bool { return false })
30
20
}
31
21
}
32
22
33
- func NewAircraftHandler (dh * data.Handler ) echo.HandlerFunc {
23
+ func NewAircraftEndpoint (dh * data.Handler ) echo.HandlerFunc {
34
24
return func (c echo.Context ) error {
35
25
aircraft , err := dh .Aircraft (c .Request ().Context ())
36
- if err != nil {
37
- noCache (c )
38
-
39
- if errors .Is (err , context .DeadlineExceeded ) {
40
- return echo .NewHTTPError (http .StatusRequestTimeout , err )
41
- }
42
-
43
- return echo .NewHTTPError (http .StatusInternalServerError )
44
- }
45
-
46
- return c .JSON (http .StatusOK , aircraft )
26
+ return jsonResponse (c , aircraft , err , func (v []data.Aircraft ) bool { return false })
47
27
}
48
28
}
49
29
50
- func NewFlightNumberHandler (dh * data.Handler ) echo.HandlerFunc {
30
+ func NewFlightNumberEndpoint (dh * data.Handler ) echo.HandlerFunc {
51
31
return func (c echo.Context ) error {
52
32
fn := c .Param ("fn" )
53
33
airport := c .Param ("airport" )
54
- d , err := common .ParseLocalDate (c .Param ("date" ))
55
- if err != nil {
56
- return echo .NewHTTPError (http .StatusBadRequest , err )
57
- }
58
-
59
- flight , err := dh .FlightNumber (c .Request ().Context (), fn , airport , d )
60
- if err != nil {
61
- noCache (c )
34
+ dateRaw := c .Param ("date" )
62
35
63
- if errors .Is (err , context .DeadlineExceeded ) {
64
- return echo .NewHTTPError (http .StatusRequestTimeout , err )
36
+ if airport != "" && dateRaw != "" {
37
+ d , err := common .ParseLocalDate (dateRaw )
38
+ if err != nil {
39
+ return echo .NewHTTPError (http .StatusBadRequest , err )
65
40
}
66
41
67
- return echo .NewHTTPError (http .StatusInternalServerError )
42
+ flight , err := dh .FlightNumber (c .Request ().Context (), fn , airport , d )
43
+ return jsonResponse (c , flight , err , func (v * common.Flight ) bool { return v == nil })
44
+ } else {
45
+ fs , err := dh .FlightSchedule (c .Request ().Context (), fn )
46
+ return jsonResponse (c , fs , err , func (v * common.FlightSchedule ) bool { return v == nil })
68
47
}
48
+ }
49
+ }
69
50
70
- if flight == nil {
71
- return echo .NewHTTPError (http .StatusNotFound )
51
+ func jsonResponse [T any ](c echo.Context , v T , err error , isEmpty func (T ) bool ) error {
52
+ if err != nil {
53
+ noCache (c )
54
+
55
+ if errors .Is (err , context .DeadlineExceeded ) {
56
+ return echo .NewHTTPError (http .StatusRequestTimeout , err )
72
57
}
73
58
74
- return c .JSON (http .StatusOK , flight )
59
+ return echo .NewHTTPError (http .StatusInternalServerError )
60
+ }
61
+
62
+ if isEmpty (v ) {
63
+ return echo .NewHTTPError (http .StatusNotFound )
75
64
}
65
+
66
+ return c .JSON (http .StatusOK , v )
76
67
}
0 commit comments