|
3 | 3 | using System.IO; |
4 | 4 | using System.Linq; |
5 | 5 | using System.Threading.Tasks; |
| 6 | +using AzureFunctions.OidcAuthentication; |
6 | 7 | using Microsoft.AspNetCore.Http; |
7 | 8 | using Microsoft.AspNetCore.Mvc; |
8 | 9 | using Microsoft.Azure.Documents; |
|
13 | 14 | using Microsoft.Azure.WebJobs.Host; |
14 | 15 | using Microsoft.Extensions.Logging; |
15 | 16 | using Newtonsoft.Json; |
16 | | -using AzureFunctions.OidcAuthentication; |
17 | | -using TwoWeeksReady.Common.EmergencyKits; |
18 | 17 | using TwoWeeksReady.Authorization; |
| 18 | +using TwoWeeksReady.Common.EmergencyKits; |
19 | 19 |
|
20 | 20 | namespace TwoWeeksReady.EmergencyKits |
21 | 21 | { |
22 | | - public class BaseKitsApi |
23 | | - { |
24 | | - private readonly IApiAuthentication _apiAuthentication; |
25 | | - |
26 | | - public BaseKitsApi(IApiAuthentication apiAuthentication) |
| 22 | + public class BaseKitsApi |
27 | 23 | { |
28 | | - _apiAuthentication = apiAuthentication; |
29 | | - } |
| 24 | + private readonly IApiAuthentication _apiAuthentication; |
30 | 25 |
|
31 | | - [FunctionName("basekits")] |
32 | | - public async Task<IActionResult> GetKits( |
33 | | - [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] |
| 26 | + public BaseKitsApi(IApiAuthentication apiAuthentication) |
| 27 | + { |
| 28 | + _apiAuthentication = apiAuthentication; |
| 29 | + } |
| 30 | + |
| 31 | + [FunctionName("basekits")] |
| 32 | + public async Task<IActionResult> GetKits( |
| 33 | + [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] |
34 | 34 | HttpRequest req, |
35 | | - [CosmosDB( databaseName: "2wr", collectionName: "basekits", ConnectionStringSetting = "CosmosDBConnection")] |
| 35 | + [CosmosDB( databaseName: "2wr", collectionName: "basekits", ConnectionStringSetting = "CosmosDBConnection")] |
36 | 36 | DocumentClient client, |
37 | | - ILogger log) |
38 | | - { |
| 37 | + ILogger log) |
| 38 | + { |
39 | 39 |
|
40 | | - log.LogInformation($"Getting list of base kits"); |
41 | | - var authorizationResult = await _apiAuthentication.AuthenticateAsync(req.Headers); |
42 | | - if (authorizationResult.Failed) |
43 | | - { |
44 | | - log.LogWarning(authorizationResult.FailureReason); |
45 | | - return new UnauthorizedResult(); |
46 | | - } |
47 | | - Uri collectionUri = UriFactory.CreateDocumentCollectionUri("2wr", "basekits"); |
48 | | - var query = client.CreateDocumentQuery<BaseKit>(collectionUri, new FeedOptions { EnableCrossPartitionQuery = true }) |
49 | | - .AsDocumentQuery(); |
50 | | - |
51 | | - var baseKits = new List<BaseKit>(); |
52 | | - while (query.HasMoreResults) |
53 | | - { |
54 | | - var result = await query.ExecuteNextAsync<BaseKit>(); |
55 | | - baseKits.AddRange(result); |
56 | | - } |
57 | | - |
58 | | - return new OkObjectResult(baseKits); |
59 | | - } |
| 40 | + log.LogInformation($"Getting list of base kits"); |
| 41 | + var authorizationResult = await _apiAuthentication.AuthenticateAsync(req.Headers); |
| 42 | + if (authorizationResult.Failed) |
| 43 | + { |
| 44 | + log.LogWarning(authorizationResult.FailureReason); |
| 45 | + return new UnauthorizedResult(); |
| 46 | + } |
| 47 | + Uri collectionUri = UriFactory.CreateDocumentCollectionUri("2wr", "basekits"); |
| 48 | + var query = client.CreateDocumentQuery<BaseKit>(collectionUri, new FeedOptions { EnableCrossPartitionQuery = true }) |
| 49 | + .AsDocumentQuery(); |
| 50 | + |
| 51 | + var baseKits = new List<BaseKit>(); |
| 52 | + while (query.HasMoreResults) |
| 53 | + { |
| 54 | + var result = await query.ExecuteNextAsync<BaseKit>(); |
| 55 | + baseKits.AddRange(result); |
| 56 | + } |
| 57 | + |
| 58 | + return new OkObjectResult(baseKits); |
| 59 | + } |
60 | 60 |
|
61 | | - [FunctionName("basekit-create")] |
62 | | - public async Task<IActionResult> CreateBaseKit( |
63 | | - [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] |
64 | | - HttpRequest req, |
65 | | - [CosmosDB(databaseName: "2wr", collectionName: "basekits", ConnectionStringSetting = "CosmosDBConnection")] |
66 | | - DocumentClient client, |
67 | | - ILogger log) |
68 | | - { |
69 | | - |
70 | | - log.LogInformation($"Creating a base kit"); |
71 | | - var authorizationResult = await _apiAuthentication.AuthenticateAsync(req.Headers); |
72 | | - if (authorizationResult.Failed) |
| 61 | + [FunctionName("basekit-by-id")] |
| 62 | + public async Task<IActionResult> GetKit( |
| 63 | + [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "basekit-by-id/{id}")] |
| 64 | + HttpRequest req, |
| 65 | + string id, |
| 66 | + [CosmosDB( databaseName: "2wr", collectionName: "basekits", ConnectionStringSetting = "CosmosDBConnection")] |
| 67 | + DocumentClient client, |
| 68 | + ILogger log) |
73 | 69 | { |
74 | | - log.LogWarning(authorizationResult.FailureReason); |
75 | | - return new UnauthorizedResult(); |
| 70 | + var authorizationResult = await _apiAuthentication.AuthenticateAsync(req.Headers); |
| 71 | + if (authorizationResult.Failed) |
| 72 | + { |
| 73 | + log.LogWarning(authorizationResult.FailureReason); |
| 74 | + return new UnauthorizedResult(); |
| 75 | + } |
| 76 | + |
| 77 | + if (String.IsNullOrWhiteSpace(id)) |
| 78 | + { |
| 79 | + return new BadRequestObjectResult("Kit id was not specified."); |
| 80 | + } |
| 81 | + |
| 82 | + log.LogInformation($"Getting single base kit"); |
| 83 | + Uri collectionUri = UriFactory.CreateDocumentCollectionUri("2wr", "basekits"); |
| 84 | + var baseKit = client.CreateDocumentQuery<BaseKit>(collectionUri, new FeedOptions { EnableCrossPartitionQuery = true }) |
| 85 | + .Where(b => b.Id == id).FirstOrDefault(); |
| 86 | + |
| 87 | + if (baseKit == null) |
| 88 | + { |
| 89 | + log.LogWarning($"Kit: {id} not found."); |
| 90 | + return new BadRequestObjectResult("Kit not found."); |
| 91 | + } |
| 92 | + |
| 93 | + return new OkObjectResult(baseKit); |
76 | 94 | } |
77 | 95 |
|
78 | | - if (!authorizationResult.IsInRole(Roles.Admin)) |
| 96 | + [FunctionName("basekit-create")] |
| 97 | + public async Task<IActionResult> CreateBaseKit( |
| 98 | + [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] |
| 99 | + HttpRequest req, |
| 100 | + [CosmosDB(databaseName: "2wr", collectionName: "basekits", ConnectionStringSetting = "CosmosDBConnection")] |
| 101 | + DocumentClient client, |
| 102 | + ILogger log) |
79 | 103 | { |
80 | | - log.LogWarning($"User is not in the {Roles.Admin} role"); |
81 | | - return new UnauthorizedResult(); |
82 | | - } |
| 104 | + |
| 105 | + log.LogInformation($"Creating a base kit"); |
| 106 | + var authorizationResult = await _apiAuthentication.AuthenticateAsync(req.Headers); |
| 107 | + if (authorizationResult.Failed) |
| 108 | + { |
| 109 | + log.LogWarning(authorizationResult.FailureReason); |
| 110 | + return new UnauthorizedResult(); |
| 111 | + } |
| 112 | + |
| 113 | + if (!authorizationResult.IsInRole(Roles.Admin)) |
| 114 | + { |
| 115 | + log.LogWarning($"User is not in the {Roles.Admin} role"); |
| 116 | + return new UnauthorizedResult(); |
| 117 | + } |
83 | 118 |
|
84 | 119 | var content = await new StreamReader(req.Body).ReadToEndAsync(); |
85 | | - var newBaseKit = JsonConvert.DeserializeObject<BaseKit>(content); |
86 | | - newBaseKit.Id = Guid.NewGuid().ToString(); |
87 | | - if(newBaseKit.Items.Count > 0) |
88 | | - { |
89 | | - newBaseKit.Items.ForEach(ki => {ki.Id=Guid.NewGuid().ToString();}); |
90 | | - } |
| 120 | + var newBaseKit = JsonConvert.DeserializeObject<BaseKit>(content); |
| 121 | + newBaseKit.Id = Guid.NewGuid().ToString(); |
| 122 | + if (newBaseKit.Items.Count > 0) |
| 123 | + { |
| 124 | + newBaseKit.Items.ForEach(ki => { ki.Id = Guid.NewGuid().ToString(); }); |
| 125 | + } |
91 | 126 |
|
92 | | - Uri collectionUri = UriFactory.CreateDocumentCollectionUri("2wr", "basekits"); |
93 | | - Document document = await client.CreateDocumentAsync(collectionUri, newBaseKit); |
94 | | - |
95 | | - return new OkObjectResult(newBaseKit); |
96 | | - } |
| 127 | + Uri collectionUri = UriFactory.CreateDocumentCollectionUri("2wr", "basekits"); |
| 128 | + Document document = await client.CreateDocumentAsync(collectionUri, newBaseKit); |
| 129 | + |
| 130 | + return new OkObjectResult(newBaseKit); |
| 131 | + } |
97 | 132 |
|
98 | | - [FunctionName("basekit-update")] |
99 | | - public async Task<IActionResult> UpdateBaseKit( |
100 | | - [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = null)] |
| 133 | + [FunctionName("basekit-update")] |
| 134 | + public async Task<IActionResult> UpdateBaseKit( |
| 135 | + [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = null)] |
101 | 136 | HttpRequest req, |
102 | | - [CosmosDB( databaseName: "2wr", collectionName: "basekits", ConnectionStringSetting = "CosmosDBConnection")] |
| 137 | + [CosmosDB( databaseName: "2wr", collectionName: "basekits", ConnectionStringSetting = "CosmosDBConnection")] |
103 | 138 | DocumentClient client, |
104 | | - ILogger log) |
105 | | - { |
106 | | - |
107 | | - log.LogInformation($"Updating a base kit"); |
108 | | - var authorizationResult = await _apiAuthentication.AuthenticateAsync(req.Headers); |
109 | | - if (authorizationResult.Failed) |
110 | | - { |
111 | | - log.LogWarning(authorizationResult.FailureReason); |
112 | | - return new UnauthorizedResult(); |
113 | | - } |
114 | | - |
115 | | - if (!authorizationResult.IsInRole(Roles.Admin)) |
116 | | - { |
117 | | - log.LogWarning($"User is not in the {Roles.Admin} role"); |
118 | | - return new UnauthorizedResult(); |
119 | | - } |
120 | | - |
121 | | - var content = await new StreamReader(req.Body).ReadToEndAsync(); |
122 | | - var kit = JsonConvert.DeserializeObject<BaseKit>(content); |
123 | | - |
124 | | - //verify existing document (not upserting as this is an update only function) |
125 | | - var baseKitUri = UriFactory.CreateDocumentUri("2wr", "basekits", kit.Id); |
126 | | - BaseKit existingKit = null; |
127 | | - try |
128 | | - { |
129 | | - existingKit = (await client.ReadDocumentAsync<BaseKit>(baseKitUri, new RequestOptions{PartitionKey = new PartitionKey(kit.Id)})).Document; |
130 | | - } |
131 | | - catch(DocumentClientException ex) |
132 | | - { |
133 | | - if(ex.StatusCode == System.Net.HttpStatusCode.NotFound) |
134 | | - { |
135 | | - log.LogWarning($"Base Kit: {kit.Id} not found."); |
136 | | - return new BadRequestObjectResult("Base Kit not found."); |
137 | | - } |
138 | | - } |
139 | | - |
140 | | - if(kit.Items.Count > 0) |
141 | | - { |
142 | | - kit.Items.ForEach(ki => { |
143 | | - if(String.IsNullOrWhiteSpace(ki.Id)){ |
144 | | - ki.Id=Guid.NewGuid().ToString(); |
145 | | - } |
146 | | - }); |
147 | | - } |
148 | | - Document document = await client.ReplaceDocumentAsync(baseKitUri, kit, new RequestOptions{PartitionKey = new PartitionKey(existingKit.Id)}); |
149 | | - |
150 | | - return new OkObjectResult(kit); |
151 | | - } |
152 | | - |
153 | | - [FunctionName("basekit-delete")] |
154 | | - public async Task<IActionResult> DeleteBaseKit( |
155 | | - [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "basekit-delete/{id}")] |
| 139 | + ILogger log) |
| 140 | + { |
| 141 | + |
| 142 | + log.LogInformation($"Updating a base kit"); |
| 143 | + var authorizationResult = await _apiAuthentication.AuthenticateAsync(req.Headers); |
| 144 | + if (authorizationResult.Failed) |
| 145 | + { |
| 146 | + log.LogWarning(authorizationResult.FailureReason); |
| 147 | + return new UnauthorizedResult(); |
| 148 | + } |
| 149 | + |
| 150 | + if (!authorizationResult.IsInRole(Roles.Admin)) |
| 151 | + { |
| 152 | + log.LogWarning($"User is not in the {Roles.Admin} role"); |
| 153 | + return new UnauthorizedResult(); |
| 154 | + } |
| 155 | + |
| 156 | + var content = await new StreamReader(req.Body).ReadToEndAsync(); |
| 157 | + var kit = JsonConvert.DeserializeObject<BaseKit>(content); |
| 158 | + |
| 159 | + //verify existing document (not upserting as this is an update only function) |
| 160 | + var baseKitUri = UriFactory.CreateDocumentUri("2wr", "basekits", kit.Id); |
| 161 | + BaseKit existingKit = null; |
| 162 | + try |
| 163 | + { |
| 164 | + existingKit = (await client.ReadDocumentAsync<BaseKit>(baseKitUri, new RequestOptions { PartitionKey = new PartitionKey(kit.Id) })).Document; |
| 165 | + } |
| 166 | + catch (DocumentClientException ex) |
| 167 | + { |
| 168 | + if (ex.StatusCode == System.Net.HttpStatusCode.NotFound) |
| 169 | + { |
| 170 | + log.LogWarning($"Base Kit: {kit.Id} not found."); |
| 171 | + return new BadRequestObjectResult("Base Kit not found."); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + if (kit.Items.Count > 0) |
| 176 | + { |
| 177 | + kit.Items.ForEach(ki => |
| 178 | + { |
| 179 | + if (String.IsNullOrWhiteSpace(ki.Id)) |
| 180 | + { |
| 181 | + ki.Id = Guid.NewGuid().ToString(); |
| 182 | + } |
| 183 | + }); |
| 184 | + } |
| 185 | + Document document = await client.ReplaceDocumentAsync(baseKitUri, kit, new RequestOptions { PartitionKey = new PartitionKey(existingKit.Id) }); |
| 186 | + |
| 187 | + return new OkObjectResult(kit); |
| 188 | + } |
| 189 | + |
| 190 | + [FunctionName("basekit-delete")] |
| 191 | + public async Task<IActionResult> DeleteBaseKit( |
| 192 | + [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "basekit-delete/{id}")] |
156 | 193 | HttpRequest req, |
157 | | - string id, |
158 | | - [CosmosDB( databaseName: "2wr", collectionName: "basekits", ConnectionStringSetting = "CosmosDBConnection")] |
| 194 | + string id, |
| 195 | + [CosmosDB( databaseName: "2wr", collectionName: "basekits", ConnectionStringSetting = "CosmosDBConnection")] |
159 | 196 | DocumentClient client, |
160 | | - ILogger log) |
161 | | - { |
162 | | - |
163 | | - log.LogInformation($"Deleting a base kit"); |
164 | | - var authorizationResult = await _apiAuthentication.AuthenticateAsync(req.Headers); |
165 | | - if (authorizationResult.Failed) |
166 | | - { |
167 | | - log.LogWarning(authorizationResult.FailureReason); |
168 | | - return new UnauthorizedResult(); |
169 | | - } |
170 | | - if (!authorizationResult.IsInRole(Roles.Admin)) |
171 | | - { |
172 | | - log.LogWarning($"User is not in the {Roles.Admin} role"); |
173 | | - return new UnauthorizedResult(); |
174 | | - } |
| 197 | + ILogger log) |
| 198 | + { |
| 199 | + |
| 200 | + log.LogInformation($"Deleting a base kit"); |
| 201 | + var authorizationResult = await _apiAuthentication.AuthenticateAsync(req.Headers); |
| 202 | + if (authorizationResult.Failed) |
| 203 | + { |
| 204 | + log.LogWarning(authorizationResult.FailureReason); |
| 205 | + return new UnauthorizedResult(); |
| 206 | + } |
| 207 | + if (!authorizationResult.IsInRole(Roles.Admin)) |
| 208 | + { |
| 209 | + log.LogWarning($"User is not in the {Roles.Admin} role"); |
| 210 | + return new UnauthorizedResult(); |
| 211 | + } |
175 | 212 |
|
176 | 213 | if (String.IsNullOrWhiteSpace(id)) |
177 | | - { |
178 | | - return new BadRequestObjectResult("Base Kit id was not specified."); |
179 | | - } |
180 | | - |
181 | | - //verify existing document |
182 | | - var baseKitUri = UriFactory.CreateDocumentUri("2wr", "basekits", id); |
183 | | - BaseKit existingKit = null; |
184 | | - try |
185 | | - { |
186 | | - existingKit = (await client.ReadDocumentAsync<BaseKit>(baseKitUri, new RequestOptions{PartitionKey = new PartitionKey(id)})).Document; |
187 | | - } |
188 | | - catch(DocumentClientException ex) |
189 | | - { |
190 | | - if(ex.StatusCode == System.Net.HttpStatusCode.NotFound) |
191 | | - { |
192 | | - log.LogWarning($"Base Kit: {id} not found."); |
193 | | - return new BadRequestObjectResult("Base Kit not found."); |
194 | | - } |
195 | | - } |
196 | | - |
197 | | - Document document = await client.DeleteDocumentAsync(baseKitUri, new RequestOptions{PartitionKey = new PartitionKey(existingKit.Id)}); |
198 | | - return new OkObjectResult(true); |
199 | | - } |
200 | | - |
201 | | - } |
| 214 | + { |
| 215 | + return new BadRequestObjectResult("Base Kit id was not specified."); |
| 216 | + } |
| 217 | + |
| 218 | + //verify existing document |
| 219 | + var baseKitUri = UriFactory.CreateDocumentUri("2wr", "basekits", id); |
| 220 | + BaseKit existingKit = null; |
| 221 | + try |
| 222 | + { |
| 223 | + existingKit = (await client.ReadDocumentAsync<BaseKit>(baseKitUri, new RequestOptions { PartitionKey = new PartitionKey(id) })).Document; |
| 224 | + } |
| 225 | + catch (DocumentClientException ex) |
| 226 | + { |
| 227 | + if (ex.StatusCode == System.Net.HttpStatusCode.NotFound) |
| 228 | + { |
| 229 | + log.LogWarning($"Base Kit: {id} not found."); |
| 230 | + return new BadRequestObjectResult("Base Kit not found."); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + Document document = await client.DeleteDocumentAsync(baseKitUri, new RequestOptions { PartitionKey = new PartitionKey(existingKit.Id) }); |
| 235 | + return new OkObjectResult(true); |
| 236 | + } |
| 237 | + |
| 238 | + } |
202 | 239 | } |
0 commit comments