-
Notifications
You must be signed in to change notification settings - Fork 260
/
Copy pathPassengerFunctions.cs
72 lines (66 loc) · 2.6 KB
/
PassengerFunctions.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
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using ServerlessMicroservices.Shared.Helpers;
using ServerlessMicroservices.Shared.Services;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace ServerlessMicroservices.FunctionApp.Passengers
{
public static class PassengerFunctions
{
[FunctionName("GetPassengers")]
public static async Task<IActionResult> GetPassengers([HttpTrigger(AuthorizationLevel.Anonymous, "get",
Route = "passengers")] HttpRequest req,
ILogger log)
{
log.LogInformation("GetPassengers triggered....");
try
{
await Utilities.ValidateToken(req);
var passengers = ServiceFactory.GetUserService();
var (users, error) = await passengers.GetUsers();
if (!string.IsNullOrWhiteSpace(error))
throw new Exception(error);
return (ActionResult)new OkObjectResult(users.ToList());
}
catch (Exception e)
{
var error = $"GetPassengers failed: {e.Message}";
log.LogError(error);
if (error.Contains(Constants.SECURITY_VALITION_ERROR))
return new StatusCodeResult(401);
else
return new BadRequestObjectResult(error);
}
}
[FunctionName("GetPassenger")]
public static async Task<IActionResult> GetPassenger([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "passengers/{userid}")] HttpRequest req,
string userid,
ILogger log)
{
log.LogInformation("GetPassenger triggered....");
try
{
await Utilities.ValidateToken(req);
var passengers = ServiceFactory.GetUserService();
var (passenger, error) = await passengers.GetUserById(userid);
if (!string.IsNullOrWhiteSpace(error))
throw new Exception(error);
return (ActionResult)new OkObjectResult(passenger);
}
catch (Exception e)
{
var error = $"GetPassenger failed: {e.Message}";
log.LogError(error);
if (error.Contains(Constants.SECURITY_VALITION_ERROR))
return new StatusCodeResult(401);
else
return new BadRequestObjectResult(error);
}
}
}
}