@@ -7,13 +7,22 @@ defmodule KsuiteMiddlewareWeb.CalendarController do
7
7
8
8
require Logger
9
9
10
+ action_fallback KsuiteMiddleware.FallbackController
11
+
10
12
def get_events ( conn , % { "from" => from , "to" => to , "calendar_id" => calendar_id } ) ,
11
13
do:
12
14
parse_params ( from , to , calendar_id )
13
15
|> then ( & read_events / 1 )
14
16
|> then ( & translate_to_events / 1 )
15
17
|> then ( & send_response ( conn , & 1 ) )
16
18
19
+ def get_events ( conn , _ ) ,
20
+ do:
21
+ conn
22
+ |> put_status ( :bad_request )
23
+ |> put_resp_content_type ( "application/problem+json" )
24
+ |> render ( :"400" , reason: "The arguments were missing." )
25
+
17
26
# Private
18
27
19
28
defp read_events ( { :error , error , reason } ) , do: { :error , error , reason }
@@ -28,19 +37,28 @@ defmodule KsuiteMiddlewareWeb.CalendarController do
28
37
client |> CalDAVClient.Event . get_events ( calendar_url , from , to )
29
38
end
30
39
31
- defp send_response ( conn , { :ok , events } ) , do: json ( conn , events )
40
+ defp send_response ( conn , { :ok , events } ) ,
41
+ do: render ( conn , :events , events: events )
32
42
33
- defp send_response ( conn , { :error , error , reason } ) ,
43
+ defp send_response ( conn , { :error , :bad_request , reason } ) ,
34
44
do:
35
45
conn
36
- |> put_status ( error )
37
- |> json ( % { reason: reason } )
46
+ |> put_status ( :bad_request )
47
+ |> put_resp_content_type ( "application/problem+json" )
48
+ |> render ( :"400" , reason: reason )
49
+
50
+ defp send_response ( conn , { :error , :not_found , reason } ) ,
51
+ do:
52
+ conn
53
+ |> put_status ( :not_found )
54
+ |> put_resp_content_type ( "application/problem+json" )
55
+ |> render ( :"404" , reason: reason )
38
56
39
57
defp send_response ( conn , { :error , reason } ) ,
40
58
do:
41
59
conn
42
- |> put_status ( 500 )
43
- |> json ( % { reason: reason } )
60
+ |> put_status ( :internal_server_error )
61
+ |> render ( :"500" , reason: reason )
44
62
45
63
defp parse_params ( from , to , calendar_id ) do
46
64
Logger . info ( "Parsing the arguments ..." )
@@ -50,28 +68,31 @@ defmodule KsuiteMiddlewareWeb.CalendarController do
50
68
{ :ok , calendar_id } <- parse_calendar_id ( calendar_id ) do
51
69
{ :ok , from , to , calendar_id }
52
70
else
53
- :invalid_to -> { :error , :bad_request , "The argument 'to' was invalid." }
54
- :invalid_from -> { :error , :bad_request , "The argument 'from' was invalid." }
71
+ { :invalid_to , reason } -> { :error , :bad_request , "The argument 'to' was invalid. #{ reason } " }
72
+ { :invalid_from , reason } -> { :error , :bad_request , "The argument 'from' was invalid. #{ reason } " }
55
73
{ :invalid_calendar_id , reason } -> { :error , :bad_request , reason }
74
+ _ -> { :error , :internal_server_error , "Unhandled error occured" }
56
75
end
57
76
end
58
77
59
78
defp parse_calendar_id ( calendar_id ) when byte_size ( calendar_id ) <= 100 , do: { :ok , calendar_id }
60
79
defp parse_calendar_id ( _ ) , do: { :invalid_calendar_id , "The calendar_id was too long." }
61
80
62
81
defp parse_datetime ( error_atom , datetime ) when is_atom ( error_atom ) and is_bitstring ( datetime ) do
63
- case Timex . parse! ( datetime , "{ISO:Extended:Z}" ) do
64
- % DateTime { } = x -> { :ok , x }
65
- _ -> error_atom
82
+ case Timex . parse ( datetime , "{ISO:Extended:Z}" ) do
83
+ { :ok , % DateTime { } = x } -> { :ok , x }
84
+ { :error , reason } -> { error_atom , reason }
66
85
end
67
86
end
68
87
69
- defp translate_to_events ( { :error , :unauthorized } ) , do: { :error , :unauthorized , "Unauthorized access to the CalDAV server, double check your credentials." }
88
+ defp translate_to_events ( { :error , :unauthorized } ) ,
89
+ do: { :error , :unauthorized , "Unauthorized access to the CalDAV server, double check your credentials." }
90
+
70
91
defp translate_to_events ( { :error , :not_found } ) , do: { :error , :not_found , "The given calendar_id was not found in the given server CalDAV." }
71
92
defp translate_to_events ( { :error , reason } ) , do: { :error , reason }
72
93
defp translate_to_events ( { :error , error , reason } ) , do: { :error , error , reason }
73
94
defp translate_to_events ( { :ok , [ ] } ) , do: { :ok , [ ] }
74
- defp translate_to_events ( { :ok , icalendar_events } ) , do: translate_to_events ( icalendar_events , [ ] )
95
+ defp translate_to_events ( { :ok , [ % CalDAVClient.Event { } | _ ] = icalendar_events } ) , do: translate_to_events ( icalendar_events , [ ] )
75
96
defp translate_to_events ( [ ] , acc ) when is_list ( acc ) , do: { :ok , acc }
76
97
77
98
defp translate_to_events ( [ head | tail ] , acc ) when is_list ( acc ) do
@@ -83,7 +104,12 @@ defmodule KsuiteMiddlewareWeb.CalendarController do
83
104
from = translate_date_to_utc ( from )
84
105
to = translate_date_to_utc ( to )
85
106
86
- new_event = % KsuiteCalendarEvent { subject: summary , from: Timex . format! ( from , "{ISO:Extended:Z}" ) , to: Timex . format! ( to , "{ISO:Extended:Z}" ) , description: description }
107
+ new_event = % KsuiteCalendarEvent {
108
+ subject: summary ,
109
+ from: Timex . format! ( from , "{ISO:Extended:Z}" ) ,
110
+ to: Timex . format! ( to , "{ISO:Extended:Z}" ) ,
111
+ description: description
112
+ }
87
113
88
114
translate_to_events ( tail , [ new_event | acc ] )
89
115
end
0 commit comments