forked from microsoftgraph/aspnet-snippets-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventsController.cs
184 lines (174 loc) · 7.29 KB
/
EventsController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
* See LICENSE in the source repository root for complete license information.
*/
using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft_Graph_ASPNET_Snippets.Helpers;
using Microsoft_Graph_ASPNET_Snippets.Models;
using Resources;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
namespace Microsoft_Graph_ASPNET_Snippets.Controllers
{
[Authorize]
public class EventsController : Controller
{
EventsService eventsService;
public EventsController()
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
eventsService = new EventsService(graphClient);
}
public ActionResult Index()
{
return View("Events");
}
// Get events.
public async Task<ActionResult> GetMyEvents()
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Get events.
results.Items = await eventsService.GetMyEvents();
}
catch (ServiceException se)
{
if ((se.InnerException as AuthenticationException)?.Error.Code == Resource.Error_AuthChallengeNeeded)
{
HttpContext.Request.GetOwinContext().Authentication.Challenge();
return new EmptyResult();
}
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Events", results);
}
// Get user's calendar view.
public async Task<ActionResult> GetMyCalendarView()
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Get a calendar view.
results.Items = await eventsService.GetMyCalendarView();
}
catch (ServiceException se)
{
if ((se.InnerException as AuthenticationException)?.Error.Code == Resource.Error_AuthChallengeNeeded)
{
HttpContext.Request.GetOwinContext().Authentication.Challenge();
return new EmptyResult();
}
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Events", results);
}
// Create an event.
// This snippet creates an hour-long event three days from now.
public async Task<ActionResult> CreateEvent()
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Create the event.
results.Items = await eventsService.CreateEvent();
}
catch (ServiceException se)
{
if ((se.InnerException as AuthenticationException)?.Error.Code == Resource.Error_AuthChallengeNeeded)
{
HttpContext.Request.GetOwinContext().Authentication.Challenge();
return new EmptyResult();
}
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Events", results);
}
// Get a specified event.
public async Task<ActionResult> GetEvent(string id)
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Get the event.
results.Items = await eventsService.GetEvent(id);
}
catch (ServiceException se)
{
if ((se.InnerException as AuthenticationException)?.Error.Code == Resource.Error_AuthChallengeNeeded)
{
HttpContext.Request.GetOwinContext().Authentication.Challenge();
return new EmptyResult();
}
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Events", results);
}
// Update an event.
// This snippets updates the event subject, time, and attendees.
public async Task<ActionResult> UpdateEvent(string id, string name)
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Update the event.
results.Items = await eventsService.UpdateEvent(id, name);
}
catch (ServiceException se)
{
if ((se.InnerException as AuthenticationException)?.Error.Code == Resource.Error_AuthChallengeNeeded)
{
HttpContext.Request.GetOwinContext().Authentication.Challenge();
return new EmptyResult();
}
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Events", results);
}
// Delete an event.
public async Task<ActionResult> DeleteEvent(string id)
{
ResultsViewModel results = new ResultsViewModel(false);
try
{
// Delete the event.
results.Items = await eventsService.DeleteEvent(id);
}
catch (ServiceException se)
{
if ((se.InnerException as AuthenticationException)?.Error.Code == Resource.Error_AuthChallengeNeeded)
{
HttpContext.Request.GetOwinContext().Authentication.Challenge();
return new EmptyResult();
}
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Events", results);
}
// Accept a meeting request.
// If the current user is the organizer of the meeting, the snippet will not work since organizers can't accept their
// own invitations.
public async Task<ActionResult> AcceptMeetingRequest(string id)
{
ResultsViewModel results = new ResultsViewModel(false);
try
{
// Accept the meeting.
results.Items = await eventsService.AcceptMeetingRequest(id);
}
catch (ServiceException se)
{
if ((se.InnerException as AuthenticationException)?.Error.Code == Resource.Error_AuthChallengeNeeded)
{
HttpContext.Request.GetOwinContext().Authentication.Challenge();
return new EmptyResult();
}
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Events", results);
}
}
}