-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetFromCosmos.cs
More file actions
47 lines (41 loc) · 1.54 KB
/
GetFromCosmos.cs
File metadata and controls
47 lines (41 loc) · 1.54 KB
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
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.Documents.Client;
using System.Linq;
using System.Collections.Generic;
namespace AF_1cosmosdb
{
public static class GetFromCosmos
{
[FunctionName("GetFromCosmos")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
[CosmosDB(
databaseName:"con-cosmos",
collectionName:"Messages",
ConnectionStringSetting = "CosmosConnection"
)] DocumentClient cosmosdb,
ILogger log)
{
Uri collectionUri = UriFactory.CreateDocumentCollectionUri("con-cosmos", "Messages");
string[] types = { "humidity", "Volt", "luminosity" };
string condition = "";
// check for keys
foreach (var type in types)
if (req.Query.ContainsKey(type))
condition = type;
// create and execute documentquery
IEnumerable<dynamic> query = cosmosdb.CreateDocumentQuery<dynamic>(collectionUri, "SELECT TOP 10 * " +
" FROM c" +
$" WHERE c.{condition} >= 0" +
" ORDER BY c.messageCreated DESC")
.AsEnumerable<dynamic>();
return new OkObjectResult(query);
}
}
}