@@ -29,7 +29,7 @@ public CalendarService(IHttpClientFactory httpClientFactory)
2929 private string _accessToken = "" ;
3030
3131 /// <summary>
32- /// The access token using throughout the class .
32+ /// The access token using throughout this service .
3333 /// </summary>
3434 public string AccessToken
3535 {
@@ -88,7 +88,7 @@ public string GetCalendarBySummary(string summary)
8888 {
8989 return "error: Can't fetch calendars." ;
9090 }
91- GoogleCalendarRoot jsonCalendar = JsonSerializer . Deserialize < GoogleCalendarRoot > ( calendars ) ;
91+ GoogleCalendarListRoot jsonCalendar = JsonSerializer . Deserialize < GoogleCalendarListRoot > ( calendars ) ;
9292
9393 if ( jsonCalendar . items == null )
9494 {
@@ -114,24 +114,17 @@ public string GetCalendarBySummary(string summary)
114114 }
115115
116116 /// <summary>
117- /// Add a new secondary calendar into user's CalendarList.
117+ /// Creates a new secondary calendar into user's CalendarList.
118118 /// </summary>
119- /// <param name="accessToken">abc </param>
119+ /// <param name="googleCalendarListModel"> </param>
120120 /// <returns>Returns the request's result content.</returns>
121- public string AddCalendar ( string accessToken , string calendarTitle , string description = "" , string timeZone = "" )
121+ public string AddCalendar ( GoogleCalendarListModel googleCalendarListModel )
122122 {
123- GoogleCalendarModel calendar = new ( )
124- {
125- summary = calendarTitle ,
126- description = description ,
127- timeZone = timeZone ,
128- } ;
129-
130- string requestBody = JsonSerializer . Serialize ( calendar ) ;
123+ string requestBody = JsonSerializer . Serialize ( googleCalendarListModel ) ;
131124
132125 var client = HttpClientFactory . CreateClient ( ) ;
133126 client . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/json" ) ) ;
134- client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , accessToken ) ;
127+ client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , _accessToken ) ;
135128 var content = new StringContent ( requestBody , Encoding . UTF8 , "application/json" ) ;
136129
137130 var result = client . PostAsync ( $ "https://www.googleapis.com/calendar/v3/calendars", content ) . Result ;
@@ -144,20 +137,19 @@ public string AddCalendar(string accessToken, string calendarTitle, string descr
144137 return result . Content . ReadAsStringAsync ( ) . Result ;
145138 }
146139
147- public string UpdateCalendar ( string accessToken , string calendarId , string calendarTitle , string description = "" , string timeZone = "" )
140+ /// <summary>
141+ /// Updates the specified calendar with given model.
142+ /// </summary>
143+ /// <param name="calendarId"></param>
144+ /// <param name="googleCalendarListModel"></param>
145+ /// <returns></returns>
146+ public string UpdateCalendar ( string calendarId , GoogleCalendarListModel googleCalendarListModel )
148147 {
149- GoogleCalendarModel calendar = new ( )
150- {
151- summary = calendarTitle ,
152- description = description ,
153- timeZone = timeZone ,
154- } ;
155-
156- string requestBody = JsonSerializer . Serialize ( calendar ) ;
148+ string requestBody = JsonSerializer . Serialize ( googleCalendarListModel ) ;
157149
158150 var client = HttpClientFactory . CreateClient ( ) ;
159151 client . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/json" ) ) ;
160- client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , accessToken ) ;
152+ client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , _accessToken ) ;
161153 var content = new StringContent ( requestBody , Encoding . UTF8 , "application/json" ) ;
162154
163155 var result = client . PutAsync ( $ "https://www.googleapis.com/calendar/v3/calendars/{ calendarId } ", content ) . Result ;
@@ -263,7 +255,7 @@ public string GetEventById(string eventId, string calendarId)
263255 /// <param name="calendarEvent"></param>
264256 /// <param name="calendarId"></param>
265257 /// <returns></returns>
266- public string AddEvent ( GoogleCalendarEvent calendarEvent , string calendarId )
258+ public string AddEvent ( GoogleCalendarEventModel calendarEvent , string calendarId )
267259 {
268260 string requestBody = JsonSerializer . Serialize ( calendarEvent ) ;
269261
@@ -287,7 +279,7 @@ public string AddEvent(GoogleCalendarEvent calendarEvent, string calendarId)
287279 /// <param name="eventId"></param>
288280 /// <param name="calendarId"></param>
289281 /// <returns></returns>
290- public string UpdateEvent ( GoogleCalendarEvent newCalendarEvent , string eventId , string calendarId )
282+ public string UpdateEvent ( GoogleCalendarEventModel newCalendarEvent , string eventId , string calendarId )
291283 {
292284 string requestBody = JsonSerializer . Serialize ( newCalendarEvent ) ;
293285
@@ -426,6 +418,7 @@ public string GetEventByDescription(string description, DateTime dateMin, DateTi
426418
427419 /// <summary>
428420 /// Finds the event with given description in given GoogleCalendarEventRoot.items collection.
421+ /// </summary>
429422 /// <param name="description"></param>
430423 /// <param name="googleCalendarEventRoot"></param>
431424 /// <param name="calendarId"></param>
@@ -502,7 +495,7 @@ public string FindCalendarId(CalendarValueType valueType, object val)
502495 {
503496 return "error: Can't fetch calendars." ;
504497 }
505- GoogleCalendarRoot googleCalendarRoot = JsonSerializer . Deserialize < GoogleCalendarRoot > ( calendars ) ;
498+ GoogleCalendarListRoot googleCalendarRoot = JsonSerializer . Deserialize < GoogleCalendarListRoot > ( calendars ) ;
506499
507500 if ( googleCalendarRoot . items == null )
508501 {
@@ -560,60 +553,54 @@ public string FindCalendarId(CalendarValueType valueType, object val)
560553 /// </summary>
561554 /// <param name="valueType"></param>
562555 /// <param name="val"></param>
556+ /// <param name="timeMin"></param>
557+ /// <param name="timeMax"></param>
558+ /// <param name="calendarId"></param>
563559 /// <returns></returns>
564- public string FindEventId ( EventValueType valueType , object val )
560+ public string FindEventId ( EventValueType valueType , object val , DateTime timeMin , DateTime timeMax , string calendarId )
565561 {
566- //string result = GetCalendarBySummary(summary);
567- //GoogleCalendarModel jsonCalendar = JsonSerializer.Deserialize<GoogleCalendarModel>(result);
568-
569- //if (string.IsNullOrEmpty(jsonCalendar.id))
570- //{
571- // return "none";
572- //}
573- //return jsonCalendar.id;
574-
575- string calendars = GetCalendars ( ) ;
576- if ( calendars == "error" )
562+ string googleEvents = GetEvents ( timeMin , timeMax , calendarId ) ;
563+ if ( googleEvents == "error" )
577564 {
578565 return "error: Can't fetch calendars." ;
579566 }
580- GoogleCalendarRoot googleCalendarRoot = JsonSerializer . Deserialize < GoogleCalendarRoot > ( calendars ) ;
567+ GoogleCalendarEventRoot googleCalendarEventRoot = JsonSerializer . Deserialize < GoogleCalendarEventRoot > ( googleEvents ) ;
581568
582- if ( googleCalendarRoot . items == null )
569+ if ( googleCalendarEventRoot . items == null )
583570 {
584571 return "none" ;
585572 }
586573
587- string calendarId = "" ;
574+ string eventId = "" ;
588575 if ( valueType == EventValueType . Summary )
589576 {
590- foreach ( var item in googleCalendarRoot . items )
577+ foreach ( var item in googleCalendarEventRoot . items )
591578 {
592579 if ( item . summary == val . ToString ( ) )
593580 {
594- calendarId = item . id ;
581+ eventId = item . id ;
595582 break ;
596583 }
597584 }
598585 }
599586 else if ( valueType == EventValueType . Description )
600587 {
601- foreach ( var item in googleCalendarRoot . items )
588+ foreach ( var item in googleCalendarEventRoot . items )
602589 {
603590 if ( item . description == val . ToString ( ) )
604591 {
605- calendarId = item . id ;
592+ eventId = item . id ;
606593 break ;
607594 }
608595 }
609596 }
610597 else if ( valueType == EventValueType . Location )
611598 {
612- foreach ( var item in googleCalendarRoot . items )
599+ foreach ( var item in googleCalendarEventRoot . items )
613600 {
614601 if ( item . location == val . ToString ( ) )
615602 {
616- calendarId = item . id ;
603+ eventId = item . id ;
617604 break ;
618605 }
619606 }
@@ -625,7 +612,7 @@ public string FindEventId(EventValueType valueType, object val)
625612 }
626613 else
627614 {
628- return calendarId ;
615+ return eventId ;
629616 }
630617
631618 }
0 commit comments