Skip to content

Commit 659e057

Browse files
committed
Calendar Add and Update
1 parent d0f0673 commit 659e057

5 files changed

Lines changed: 159 additions & 4 deletions

File tree

58 Bytes
Binary file not shown.
145 Bytes
Binary file not shown.

.vs/GoogleApis.Blazor/v17/.suo

6.5 KB
Binary file not shown.

GoogleApis.Blazor/Calendar/CalendarService.cs

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
using Microsoft.AspNetCore.Components;
1+
using GoogleApis.Blazor.Models;
2+
using Microsoft.AspNetCore.Components;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
6+
using System.Net.Http.Headers;
57
using System.Text;
8+
using System.Text.Json;
69
using System.Threading.Tasks;
710

811
namespace 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
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
namespace GoogleApis.Blazor.Models
2+
{
3+
public class ConferenceProperties
4+
{
5+
public List<string> allowedConferenceSolutionTypes { get; set; }
6+
}
7+
8+
public class DefaultReminder
9+
{
10+
public string method { get; set; }
11+
public int minutes { get; set; }
12+
}
13+
14+
public class GoogleCalendarModel
15+
{
16+
// should be location parameter?
17+
public string kind { get; set; }
18+
public string etag { get; set; }
19+
public string id { get; set; }
20+
public string summary { get; set; }
21+
public string description { get; set; }
22+
public string timeZone { get; set; }
23+
public string colorId { get; set; }
24+
public string backgroundColor { get; set; }
25+
public string foregroundColor { get; set; }
26+
public bool selected { get; set; }
27+
public string accessRole { get; set; }
28+
public List<DefaultReminder> defaultReminders { get; set; }
29+
public ConferenceProperties conferenceProperties { get; set; }
30+
public NotificationSettings notificationSettings { get; set; }
31+
public bool? primary { get; set; }
32+
}
33+
34+
public class Notification
35+
{
36+
public string type { get; set; }
37+
public string method { get; set; }
38+
}
39+
40+
public class NotificationSettings
41+
{
42+
public List<Notification> notifications { get; set; }
43+
}
44+
45+
public class GoogleCalendarRoot
46+
{
47+
public string kind { get; set; }
48+
public string etag { get; set; }
49+
public string nextSyncToken { get; set; }
50+
public List<GoogleCalendarModel> items { get; set; }
51+
}
52+
}

0 commit comments

Comments
 (0)