-
Notifications
You must be signed in to change notification settings - Fork 260
/
Copy pathDriverFunctions.cs
239 lines (221 loc) · 9.75 KB
/
DriverFunctions.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using ServerlessMicroservices.Models;
using ServerlessMicroservices.Shared.Helpers;
using ServerlessMicroservices.Shared.Services;
using System;
using System.IO;
using System.Threading.Tasks;
namespace ServerlessMicroservices.FunctionApp.Drivers
{
public static class DriverFunctions
{
[FunctionName("GetDrivers")]
public static async Task<IActionResult> GetDrivers([HttpTrigger(AuthorizationLevel.Anonymous, "get",
Route = "drivers")] HttpRequest req,
ILogger log)
{
log.LogInformation("GetDrivers triggered....");
try
{
await Utilities.ValidateToken(req);
var persistenceService = ServiceFactory.GetPersistenceService();
return (ActionResult)new OkObjectResult(await persistenceService.RetrieveDrivers());
}
catch (Exception e)
{
var error = $"GetDrivers failed: {e.Message}";
log.LogError(error);
if (error.Contains(Constants.SECURITY_VALITION_ERROR))
return new StatusCodeResult(401);
else
return new BadRequestObjectResult(error);
}
}
[FunctionName("GetDriversWithinLocation")]
public static async Task<IActionResult> GetDriversWithinLocation([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "drivers/{latitude}/{longitude}/{miles}")] HttpRequest req,
double latitude,
double longitude,
double miles,
ILogger log)
{
log.LogInformation("GetDriversWithinLocation triggered....");
try
{
await Utilities.ValidateToken(req);
var persistenceService = ServiceFactory.GetPersistenceService();
return (ActionResult)new OkObjectResult(await persistenceService.RetrieveDrivers(latitude, longitude, miles));
}
catch (Exception e)
{
var error = $"GetDriversWithinLocation failed: {e.Message}";
log.LogError(error);
if (error.Contains(Constants.SECURITY_VALITION_ERROR))
return new StatusCodeResult(401);
else
return new BadRequestObjectResult(error);
}
}
[FunctionName("GetActiveDrivers")]
public static async Task<IActionResult> GetActiveDrivers([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "activedrivers")] HttpRequest req,
ILogger log)
{
log.LogInformation("GetActiveDrivers triggered....");
try
{
await Utilities.ValidateToken(req);
var persistenceService = ServiceFactory.GetPersistenceService();
return (ActionResult)new OkObjectResult(await persistenceService.RetrieveActiveDrivers());
}
catch (Exception e)
{
var error = $"GetActiveDrivers failed: {e.Message}";
log.LogError(error);
if (error.Contains(Constants.SECURITY_VALITION_ERROR))
return new StatusCodeResult(401);
else
return new BadRequestObjectResult(error);
}
}
[FunctionName("GetDriver")]
public static async Task<IActionResult> GetDriver([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "drivers/{code}")] HttpRequest req,
string code,
ILogger log)
{
log.LogInformation("GetDriver triggered....");
try
{
await Utilities.ValidateToken(req);
var persistenceService = ServiceFactory.GetPersistenceService();
return (ActionResult)new OkObjectResult(await persistenceService.RetrieveDriver(code));
}
catch (Exception e)
{
var error = $"GetDriver failed: {e.Message}";
log.LogError(error);
if (error.Contains(Constants.SECURITY_VALITION_ERROR))
return new StatusCodeResult(401);
else
return new BadRequestObjectResult(error);
}
}
[FunctionName("CreateDriver")]
public static async Task<IActionResult> CreateDriver([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "drivers")] HttpRequest req,
ILogger log)
{
log.LogInformation("CreateDriver triggered....");
try
{
await Utilities.ValidateToken(req);
string requestBody = new StreamReader(req.Body).ReadToEnd();
DriverItem driver = JsonConvert.DeserializeObject<DriverItem>(requestBody);
var persistenceService = ServiceFactory.GetPersistenceService();
return (ActionResult)new OkObjectResult(await persistenceService.UpsertDriver(driver));
}
catch (Exception e)
{
var error = $"CreateDriver failed: {e.Message}";
log.LogError(error);
if (error.Contains(Constants.SECURITY_VALITION_ERROR))
return new StatusCodeResult(401);
else
return new BadRequestObjectResult(error);
}
}
[FunctionName("UpdateDriver")]
public static async Task<IActionResult> UpdateDriver([HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "drivers")] HttpRequest req,
ILogger log)
{
log.LogInformation("UpdateDriver triggered....");
try
{
await Utilities.ValidateToken(req);
string requestBody = new StreamReader(req.Body).ReadToEnd();
DriverItem driver = JsonConvert.DeserializeObject<DriverItem>(requestBody);
var persistenceService = ServiceFactory.GetPersistenceService();
return (ActionResult)new OkObjectResult(await persistenceService.UpsertDriver(driver, true));
}
catch (Exception e)
{
var error = $"UpdateDriver failed: {e.Message}";
log.LogError(error);
if (error.Contains(Constants.SECURITY_VALITION_ERROR))
return new StatusCodeResult(401);
else
return new BadRequestObjectResult(error);
}
}
[FunctionName("UpdateDriverLocation")]
public static async Task<IActionResult> UpdateDriverLocation([HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "driverlocations")] HttpRequest req,
ILogger log)
{
log.LogInformation("UpdateDriverLocation triggered....");
try
{
await Utilities.ValidateToken(req);
string requestBody = new StreamReader(req.Body).ReadToEnd();
DriverLocationItem driverLocation = JsonConvert.DeserializeObject<DriverLocationItem>(requestBody);
var persistenceService = ServiceFactory.GetPersistenceService();
return (ActionResult)new OkObjectResult(await persistenceService.UpsertDriverLocation(driverLocation));
}
catch (Exception e)
{
var error = $"UpdateDriverLocation failed: {e.Message}";
log.LogError(error);
if (error.Contains(Constants.SECURITY_VALITION_ERROR))
return new StatusCodeResult(401);
else
return new BadRequestObjectResult(error);
}
}
[FunctionName("GetDriverLocations")]
public static async Task<IActionResult> GetDriverLocations([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "driverlocations/{code}")] HttpRequest req,
string code,
ILogger log)
{
log.LogInformation("GetDriverLocations triggered....");
try
{
await Utilities.ValidateToken(req);
var persistenceService = ServiceFactory.GetPersistenceService();
return (ActionResult)new OkObjectResult(await persistenceService.RetrieveDriverLocations(code));
}
catch (Exception e)
{
var error = $"GetDriverLocations failed: {e.Message}";
log.LogError(error);
if (error.Contains(Constants.SECURITY_VALITION_ERROR))
return new StatusCodeResult(401);
else
return new BadRequestObjectResult(error);
}
}
[FunctionName("DeleteDriver")]
public static async Task<IActionResult> DeleteDriver([HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "drivers/{code}")] HttpRequest req,
string code,
ILogger log)
{
log.LogInformation("DeleteDriver triggered....");
try
{
await Utilities.ValidateToken(req);
var persistenceService = ServiceFactory.GetPersistenceService();
await persistenceService.DeleteDriver(code);
return (ActionResult)new OkObjectResult("Ok");
}
catch (Exception e)
{
var error = $"DeleteDriver failed: {e.Message}";
log.LogError(error);
if (error.Contains(Constants.SECURITY_VALITION_ERROR))
return new StatusCodeResult(401);
else
return new BadRequestObjectResult(error);
}
}
}
}