Skip to content

Commit 190a56f

Browse files
committed
Calendar Api Fundamentals Part 2
1 parent ac6be59 commit 190a56f

6 files changed

Lines changed: 137 additions & 123 deletions

File tree

35 Bytes
Binary file not shown.

.vs/GoogleApis.Blazor/v17/.suo

3 KB
Binary file not shown.

GoogleApis.Blazor/Calendar/CalendarService.cs

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace GoogleApis.Blazor.Models
8+
#pragma warning disable CS1591
9+
{
10+
public class Creator
11+
{
12+
public string id { get; set; }
13+
public string email { get; set; }
14+
public string displayName { get; set; }
15+
public bool self { get; set; }
16+
}
17+
18+
public class Start
19+
{
20+
public string dateTime { get; set; }
21+
public string timeZone { get; set; }
22+
}
23+
24+
public class End
25+
{
26+
public string dateTime { get; set; }
27+
public string timeZone { get; set; }
28+
}
29+
30+
public class Organizer
31+
{
32+
public string id { get; set; }
33+
public string email { get; set; }
34+
public string displayName { get; set; }
35+
public bool self { get; set; }
36+
}
37+
38+
public class Reminders
39+
{
40+
public bool useDefault { get; set; }
41+
}
42+
43+
public class ConferenceProperties
44+
{
45+
public List<string> allowedConferenceSolutionTypes { get; set; }
46+
}
47+
48+
public class DefaultReminder
49+
{
50+
public string method { get; set; }
51+
public int minutes { get; set; }
52+
}
53+
54+
55+
56+
public class Notification
57+
{
58+
public string type { get; set; }
59+
public string method { get; set; }
60+
}
61+
62+
public class NotificationSettings
63+
{
64+
public List<Notification> notifications { get; set; }
65+
}
66+
67+
68+
}
Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
namespace GoogleApis.Blazor.Models
2+
#pragma warning disable CS1591
23
{
3-
public class Creator
4-
{
5-
public string id { get; set; }
6-
public string email { get; set; }
7-
public string displayName { get; set; }
8-
public bool self { get; set; }
9-
}
10-
11-
public class End
4+
/// <summary>
5+
/// A root class represents google calendar event api results.
6+
/// </summary>
7+
public class GoogleCalendarEventRoot
128
{
13-
public string dateTime { get; set; }
9+
public string kind { get; set; } = "calendar#events";
10+
public string etag { get; set; }
11+
public string summary { get; set; }
12+
public string description { get; set; }
13+
public string updated { get; set; }
1414
public string timeZone { get; set; }
15+
public string accessRole { get; set; }
16+
public List<DefaultReminder> defaultReminders { get; set; }
17+
public string nextPageToken { get; set; }
18+
public string nextSyncToken { get; set; }
19+
public List<GoogleCalendarEventModel> items { get; set; }
1520
}
1621

17-
public class GoogleCalendarEvent
22+
public class GoogleCalendarEventModel
1823
{
1924
public string kind { get; set; } = "calendar#event";
2025
public string etag { get; set; } = "";
@@ -39,36 +44,4 @@ public class GoogleCalendarEvent
3944
public Reminders reminders { get; set; } = new();
4045
public string eventType { get; set; } = "default";
4146
}
42-
43-
public class Organizer
44-
{
45-
public string id { get; set; }
46-
public string email { get; set; }
47-
public string displayName { get; set; }
48-
public bool self { get; set; }
49-
}
50-
51-
public class Reminders
52-
{
53-
public bool useDefault { get; set; }
54-
}
55-
56-
public class GoogleCalendarEventRoot
57-
{
58-
public string kind { get; set; }
59-
public string etag { get; set; }
60-
public string summary { get; set; }
61-
//public DateTime updated { get; set; }
62-
public string timeZone { get; set; }
63-
public string accessRole { get; set; }
64-
public List<object> defaultReminders { get; set; }
65-
public string nextSyncToken { get; set; }
66-
public List<GoogleCalendarEvent> items { get; set; }
67-
}
68-
69-
public class Start
70-
{
71-
public string dateTime { get; set; }
72-
public string timeZone { get; set; }
73-
}
7447
}

0 commit comments

Comments
 (0)