1- using Microsoft . AspNetCore . Components ;
1+ using GoogleApis . Blazor . Models ;
2+ using Microsoft . AspNetCore . Components ;
23using System ;
34using System . Collections . Generic ;
45using System . Linq ;
6+ using System . Net . Http . Headers ;
57using System . Text ;
8+ using System . Text . Json ;
69using System . Threading . Tasks ;
710
811namespace GoogleApis . Blazor . Calendar
@@ -36,11 +39,11 @@ public string GetCalendars(string accessToken)
3639 }
3740
3841 /// <summary>
39- /// Get all calendars that authenticated user has.
42+ /// Get calendar with given id that authenticated user has.
4043 /// </summary>
41- /// <param name="accessToken"></param>
44+ /// <param name="accessToken">The access token generated with authorization code. Can be generated by AuthService.AuthorizeCredential </param>
4245 /// <returns></returns>
43- public string GetCalendar ( string accessToken , string calendarId )
46+ public string GetCalendarById ( string accessToken , string calendarId )
4447 {
4548 var client = HttpClientFactory . CreateClient ( ) ;
4649
@@ -54,5 +57,105 @@ public string GetCalendar(string accessToken, string calendarId)
5457 return result . Content . ReadAsStringAsync ( ) . Result ;
5558 }
5659
60+ public string GetCalendarBySummary ( string accessToken , string summary )
61+ {
62+ string calendars = GetCalendars ( accessToken ) ;
63+ if ( calendars == "error" )
64+ {
65+ return "error: Can't fetch calendars." ;
66+ }
67+ GoogleCalendarRoot jsonCalendar = JsonSerializer . Deserialize < GoogleCalendarRoot > ( calendars ) ;
68+
69+ if ( jsonCalendar . items == null )
70+ {
71+ return "none" ;
72+ }
73+
74+ string calendarId = "" ;
75+ foreach ( var item in jsonCalendar . items )
76+ {
77+ if ( item . summary == summary )
78+ {
79+ calendarId = item . id ;
80+ }
81+ }
82+
83+ if ( string . IsNullOrEmpty ( calendarId ) )
84+ {
85+ return "none" ;
86+ }
87+
88+ return GetCalendarById ( accessToken , calendarId ) ;
89+ }
90+
91+ public string GetCalendarIdBySummary ( string accessToken , string summary )
92+ {
93+ string result = GetCalendarBySummary ( accessToken , summary ) ;
94+ GoogleCalendarModel jsonCalendar = JsonSerializer . Deserialize < GoogleCalendarModel > ( result ) ;
95+
96+ if ( string . IsNullOrEmpty ( jsonCalendar . id ) )
97+ {
98+ return "none" ;
99+ }
100+ return jsonCalendar . id ;
101+ }
102+
103+ /// <summary>
104+ /// Add a new secondary calendar into user's CalendarList.
105+ /// </summary>
106+ /// <param name="accessToken"></param>
107+ /// <returns></returns>
108+ public string AddCalendar ( string accessToken , string calendarTitle , string description = "" , string timeZone = "" )
109+ {
110+ GoogleCalendarModel calendar = new ( )
111+ {
112+ summary = calendarTitle ,
113+ description = description ,
114+ timeZone = timeZone ,
115+ } ;
116+
117+ string requestBody = JsonSerializer . Serialize ( calendar ) ;
118+
119+ var client = HttpClientFactory . CreateClient ( ) ;
120+ client . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/json" ) ) ;
121+ client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , accessToken ) ;
122+ var content = new StringContent ( requestBody , Encoding . UTF8 , "application/json" ) ;
123+
124+ var result = client . PostAsync ( $ "https://www.googleapis.com/calendar/v3/calendars", content ) . Result ;
125+
126+ if ( ! result . IsSuccessStatusCode )
127+ {
128+ return "error" ;
129+ }
130+
131+ return result . Content . ReadAsStringAsync ( ) . Result ;
132+ }
133+
134+ public string UpdateCalendar ( string accessToken , string calendarId , string calendarTitle , string description = "" , string timeZone = "" )
135+ {
136+ GoogleCalendarModel calendar = new ( )
137+ {
138+ summary = calendarTitle ,
139+ description = description ,
140+ timeZone = timeZone ,
141+ } ;
142+
143+ string requestBody = JsonSerializer . Serialize ( calendar ) ;
144+
145+ var client = HttpClientFactory . CreateClient ( ) ;
146+ client . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/json" ) ) ;
147+ client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , accessToken ) ;
148+ var content = new StringContent ( requestBody , Encoding . UTF8 , "application/json" ) ;
149+
150+ var result = client . PutAsync ( $ "https://www.googleapis.com/calendar/v3/calendars/{ calendarId } ", content ) . Result ;
151+
152+ if ( ! result . IsSuccessStatusCode )
153+ {
154+ return "error" ;
155+ }
156+
157+ return result . Content . ReadAsStringAsync ( ) . Result ;
158+ }
159+
57160 }
58161}
0 commit comments