1
1
using Azure . Data . Tables ;
2
+ using TableStorageApi . Models ;
2
3
4
+ const string tableName = "ProgramStatus" ;
5
+ const string partitionKey = "StatusUpdate" ;
3
6
var builder = WebApplication . CreateBuilder ( args ) ;
4
-
5
7
var app = builder . Build ( ) ;
6
8
7
- var tableConnString = builder . Configuration [ "TableStorageConnectionString" ] ?? throw new NullReferenceException ( "need conn string" ) ;
8
- var tableName = "ProgramStatus" ;
9
- var partitionKey = "ProgramStatus" ;
9
+ var tableConnString = builder . Configuration [ "TableStorageConnectionString" ] ;
10
10
var tableClient = new TableClient ( tableConnString , tableName ) ;
11
11
12
- app . MapGet ( "/" , ( ) => Results . Json ( GetStatuses ( tableClient , tableName ) ) ) ;
13
- app . MapPost ( "/" , ( StatusRecord record ) => AddEntityToTable ( tableClient , record , partitionKey ) ) ;
12
+ app . MapGet ( "/" , ( ) => Results . Json ( tableClient . Query < StatusEntry > ( ) ) ) ;
13
+ app . MapPost ( "/" , ( StatusEntry entry ) => {
14
+ entry . PartitionKey = partitionKey ;
15
+ tableClient . AddEntity ( entry ) ;
16
+ } ) ;
14
17
15
18
app . Run ( ) ;
16
-
17
- static void AddEntityToTable ( TableClient tableClient , StatusRecord record , string partitionKey )
18
- {
19
- var entity = new TableEntity ( partitionKey , Guid . NewGuid ( ) . ToString ( ) )
20
- {
21
- { "Program" , record . Program } ,
22
- { "Date" , record . Date } ,
23
- { "Message" , record . Message }
24
- } ;
25
- _ = tableClient . AddEntity ( entity ) ;
26
- }
27
-
28
- static List < StatusRecord > GetStatuses ( TableClient tableClient , string partitionKey )
29
- {
30
- var queryResultsFilter = tableClient . Query < TableEntity > ( filter : $ "PartitionKey eq '{ partitionKey } '") ;
31
- var result = new List < StatusRecord > ( ) ;
32
- foreach ( var item in queryResultsFilter )
33
- {
34
- result . Add ( new StatusRecord ( item . GetString ( "Program" ) , item . GetDateTime ( "Date" ) ?? DateTime . MinValue , item . GetString ( "Message" ) ) ) ;
35
- }
36
-
37
- return result ;
38
- }
39
-
40
- public record StatusRecord ( string Program , DateTime Date , string Message ) ;
0 commit comments