-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetTable.cs
74 lines (61 loc) · 2.29 KB
/
GetTable.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
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.WindowsAzure.Storage.Table;
using System.Collections.Generic;
using AF_1cosmosdb.models;
using System.Linq;
namespace AF_1cosmosdb
{
public static class GetTable
{
[FunctionName("GetTable")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
[Table("Messages")] CloudTable cloudtable,
ILogger log)
{
// sensor types
string[] types = { "dht", "volt", "luminosity" };
string type = "";
// check query keys
foreach (var _type in types)
if (req.Query.ContainsKey(_type))
type = _type;
// return collection
IEnumerable<dynamic> results;
switch(type)
{
case "luminosity":
results = await GetItemsAsync<LuminosityMessage>(cloudtable, type);
break;
case "volt":
results = await GetItemsAsync<VoltMessage>(cloudtable, type);
break;
case "dht":
results = await GetItemsAsync<DhtMessage>(cloudtable, type);
break;
default:
results = null;
break;
}
return results != null ?
(ActionResult)new OkObjectResult(results) :
new BadRequestObjectResult("[]");
}
// gets items by partitionkey, returns enumerable
private static async Task<IEnumerable<T>> GetItemsAsync<T>(CloudTable cloudtable, string type)
where T : ITableEntity, ISortable, new()
{
IEnumerable<T> results = await cloudtable
.ExecuteQuerySegmentedAsync(new TableQuery<T>(), null);
results = results.Where(ts => ts.PartitionKey == type);
results = results.OrderByDescending(ts => ts.messageCreated);
results = results.Take(10);
return results;
}
}
}