Skip to content

Commit 048e07c

Browse files
committed
use strongly typed model
1 parent e374f81 commit 048e07c

File tree

2 files changed

+28
-31
lines changed

2 files changed

+28
-31
lines changed

TableStorageApi/Models/StatusEntry.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Azure;
2+
using Azure.Data.Tables;
3+
4+
namespace TableStorageApi.Models;
5+
6+
public class StatusEntry : ITableEntity
7+
{
8+
public string Program { get; set; } = string.Empty;
9+
public DateTime Date { get; set; }
10+
public string Message { get; set; } = string.Empty;
11+
12+
// Below required by ITableEntity
13+
public string PartitionKey { get; set; } = string.Empty;
14+
public string RowKey { get; set; } = Guid.NewGuid().ToString();
15+
16+
// Below not set by user
17+
public DateTimeOffset? Timestamp { get; set; }
18+
public ETag ETag { get; set; }
19+
}

TableStorageApi/Program.cs

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,18 @@
11
using Azure.Data.Tables;
2+
using TableStorageApi.Models;
23

4+
const string tableName = "ProgramStatus";
5+
const string partitionKey = "StatusUpdate";
36
var builder = WebApplication.CreateBuilder(args);
4-
57
var app = builder.Build();
68

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"];
1010
var tableClient = new TableClient(tableConnString, tableName);
1111

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+
});
1417

1518
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

Comments
 (0)