Skip to content

Commit cd6ef8d

Browse files
authored
Merge pull request #51 from FBoucher/v-next
Update to v1.0.1
2 parents 6bd131a + 51fa4dd commit cd6ef8d

File tree

17 files changed

+180
-114
lines changed

17 files changed

+180
-114
lines changed

NoteBookmark.Api/DataStorageService.cs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,34 @@
1111

1212
namespace NoteBookmark.Api;
1313

14-
public class DataStorageService(string connectionString): IDataStorageService
14+
public class DataStorageService(TableServiceClient tblClient, BlobServiceClient blobClient): IDataStorageService
1515
{
16-
private string ConnectionString { get; set; } = connectionString;
17-
private TableServiceClient? TableClient { get; set; }
18-
19-
private TableServiceClient GetTableClient()
20-
{
21-
if (TableClient == null)
22-
{
23-
TableClient = new TableServiceClient(ConnectionString);
24-
}
25-
return TableClient;
26-
}
27-
28-
private TableClient GetTable(string tableName)
29-
{
30-
var client = GetTableClient();
31-
TableItem table = client.CreateTableIfNotExists(tableName);
32-
33-
return client.GetTableClient(tableName);
34-
}
3516

3617
private TableClient GetPostTable()
3718
{
38-
TableClient table = GetTable("Posts");
19+
tblClient.CreateTableIfNotExists("Posts");
20+
TableClient table = tblClient.GetTableClient("Posts");
3921
return table;
4022
}
4123

4224
private TableClient GetSummaryTable()
4325
{
44-
TableClient table = GetTable("Summary");
26+
tblClient.CreateTableIfNotExists("Summary");
27+
TableClient table = tblClient.GetTableClient("Summary");
4528
return table;
4629
}
4730

4831
private TableClient GetNoteTable()
4932
{
50-
TableClient table = GetTable("Notes");
33+
tblClient.CreateTableIfNotExists("Notes");
34+
TableClient table = tblClient.GetTableClient("Notes");
5135
return table;
5236
}
5337

5438
private TableClient GetSettingTable()
5539
{
56-
TableClient table = GetTable("Settings");
40+
tblClient.CreateTableIfNotExists("Settings");
41+
TableClient table = tblClient.GetTableClient("Settings");
5742
return table;
5843
}
5944

@@ -221,8 +206,7 @@ public async Task<Settings> GetSettings()
221206

222207
private async Task<BlobContainerClient> GetReadingNotesContainer()
223208
{
224-
var blobServiceClient = new BlobServiceClient(ConnectionString);
225-
var containerClient = blobServiceClient.GetBlobContainerClient("readingnotes");
209+
var containerClient = blobClient.GetBlobContainerClient("readingnotes");
226210
await containerClient.CreateIfNotExistsAsync();
227211
return containerClient;
228212
}

NoteBookmark.Api/NoteBookmark.Api.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Aspire.Azure.Data.Tables" Version="9.0.0" />
11+
<PackageReference Include="Aspire.Azure.Storage.Blobs" Version="9.0.0" />
1012
<PackageReference Include="Azure.Data.Tables" Version="12.9.1" />
1113
<PackageReference Include="Azure.Storage.Blobs" Version="12.23.0" />
1214
<PackageReference Include="HtmlAgilityPack" Version="1.11.72" />

NoteBookmark.Api/NoteEnpoints.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using Azure.Data.Tables;
3+
using Azure.Storage.Blobs;
24
using Microsoft.AspNetCore.Http.HttpResults;
35
using NoteBookmark.Domain;
46

@@ -27,10 +29,13 @@ public static void MapNoteEndpoints(this IEndpointRouteBuilder app)
2729
.WithDescription("Update the read status of all posts to true if they have a note referencing them.");
2830
}
2931

30-
static Results<Created<Note>, BadRequest> CreateNote(Note note, IDataStorageService dataStorageService)
32+
static Results<Created<Note>, BadRequest> CreateNote(Note note,
33+
TableServiceClient tblClient,
34+
BlobServiceClient blobClient)
3135
{
3236
try
3337
{
38+
var dataStorageService = new DataStorageService(tblClient, blobClient);
3439
dataStorageService.CreateNote(note);
3540
var post = dataStorageService.GetPost(note.PostId!);
3641
if(post is not null)
@@ -47,22 +52,31 @@ static Results<Created<Note>, BadRequest> CreateNote(Note note, IDataStorageServ
4752
}
4853
}
4954

50-
static Results<Ok<List<Note>>, NotFound> GetNotes(IDataStorageService dataStorageService)
55+
static Results<Ok<List<Note>>, NotFound> GetNotes(TableServiceClient tblClient,
56+
BlobServiceClient blobClient)
5157
{
58+
var dataStorageService = new DataStorageService(tblClient, blobClient);
5259
var notes = dataStorageService.GetNotes();
5360
return notes == null ? TypedResults.NotFound() : TypedResults.Ok(notes);
5461
}
5562

56-
static Results<Ok<List<ReadingNote>>, NotFound> GetNotesForSummary(string ReadingNotesId, IDataStorageService dataStorageService)
63+
static Results<Ok<List<ReadingNote>>, NotFound> GetNotesForSummary(string ReadingNotesId,
64+
TableServiceClient tblClient,
65+
BlobServiceClient blobClient)
5766
{
67+
68+
var dataStorageService = new DataStorageService(tblClient, blobClient);
5869
var notes = dataStorageService.GetNotesForSummary(ReadingNotesId);
5970
return notes == null ? TypedResults.NotFound() : TypedResults.Ok(notes);
6071
}
6172

62-
private static async Task<Results<Created<string>, BadRequest>> SaveReadingNotes(ReadingNotes readingNotes, IDataStorageService dataStorageService)
73+
private static async Task<Results<Created<string>, BadRequest>> SaveReadingNotes(ReadingNotes readingNotes,
74+
TableServiceClient tblClient,
75+
BlobServiceClient blobClient)
6376
{
6477
try
6578
{
79+
var dataStorageService = new DataStorageService(tblClient, blobClient);
6680
var url = await dataStorageService.SaveReadingNotes(readingNotes);
6781
return url == null ? TypedResults.BadRequest() : TypedResults.Created("url", url);
6882
}
@@ -73,10 +87,12 @@ private static async Task<Results<Created<string>, BadRequest>> SaveReadingNotes
7387
}
7488
}
7589

76-
private static async Task<Results<Ok, BadRequest>> UpdatePostReadStatus(IDataStorageService dataStorageService)
90+
private static async Task<Results<Ok, BadRequest>> UpdatePostReadStatus(TableServiceClient tblClient,
91+
BlobServiceClient blobClient)
7792
{
7893
try
7994
{
95+
var dataStorageService = new DataStorageService(tblClient, blobClient);
8096
await dataStorageService.UpdatePostReadStatus();
8197
return TypedResults.Ok();
8298
}

NoteBookmark.Api/PostEndpoints.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using NoteBookmark.Domain;
44
using static NoteBookmark.Api.DataStorageService;
55
using HtmlAgilityPack;
6+
using Azure.Data.Tables;
7+
using Azure.Storage.Blobs;
68

79
namespace NoteBookmark.Api;
810

@@ -24,29 +26,35 @@ public static void MapPostEndpoints(this IEndpointRouteBuilder app)
2426
endpoints.MapDelete("/{id}", DeletePost)
2527
.WithDescription("Delete a post by id");
2628
}
27-
28-
static List<PostL> GetUnreadPosts(IDataStorageService dataStorageService)
29+
30+
static List<PostL> GetUnreadPosts(TableServiceClient tblClient, BlobServiceClient blobClient)
2931
{
32+
var dataStorageService = new DataStorageService(tblClient, blobClient);
3033
return dataStorageService.GetFilteredPosts("is_read eq false");
3134
}
3235

33-
static Results<Ok<Post>, NotFound> Get(string id, IDataStorageService dataStorageService)
36+
static Results<Ok<Post>, NotFound> Get(string id, TableServiceClient tblClient, BlobServiceClient blobClient)
3437
{
38+
var dataStorageService = new DataStorageService(tblClient, blobClient);
3539
var post = dataStorageService.GetPost(id);
3640
return post is null ? TypedResults.NotFound() : TypedResults.Ok(post);
3741
}
3842

39-
static Results<Ok, BadRequest> SavePost(Post post, IDataStorageService dataStorageService)
43+
static Results<Ok, BadRequest> SavePost(Post post, TableServiceClient tblClient, BlobServiceClient blobClient)
4044
{
45+
var dataStorageService = new DataStorageService(tblClient, blobClient);
46+
4147
if (dataStorageService.SavePost(post))
4248
{
4349
return TypedResults.Ok();
4450
}
4551
return TypedResults.BadRequest();
4652
}
4753

48-
static async Task<Results<Ok<Post>, BadRequest>> ExtractPostDetails(string url, IDataStorageService dataStorageService)
54+
static async Task<Results<Ok<Post>, BadRequest>> ExtractPostDetails(string url, TableServiceClient tblClient, BlobServiceClient blobClient)
4955
{
56+
var dataStorageService = new DataStorageService(tblClient, blobClient);
57+
5058
try
5159
{
5260
var post = await ExtractPostDetailsFromUrl(url);
@@ -64,8 +72,9 @@ static async Task<Results<Ok<Post>, BadRequest>> ExtractPostDetails(string url,
6472
}
6573
}
6674

67-
static Results<Ok, NotFound> DeletePost(string id, IDataStorageService dataStorageService)
75+
static Results<Ok, NotFound> DeletePost(string id, TableServiceClient tblClient, BlobServiceClient blobClient)
6876
{
77+
var dataStorageService = new DataStorageService(tblClient, blobClient);
6978
if (dataStorageService.DeletePost(id))
7079
{
7180
return TypedResults.Ok();

NoteBookmark.Api/Program.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,18 @@
44
var builder = WebApplication.CreateBuilder(args);
55

66
builder.AddServiceDefaults();
7+
builder.AddAzureTableClient("nb-tables");
8+
builder.AddAzureBlobClient("nb-blobs");
79

810
// Add services to the container.
911
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
1012
builder.Services.AddEndpointsApiExplorer();
1113
builder.Services.AddSwaggerGen();
1214

13-
string connStr = Environment.GetEnvironmentVariable("data-storage-connstr")?? string.Empty;
14-
15-
builder.Services.AddTransient<IDataStorageService, DataStorageService>(sp => new DataStorageService(connStr));
16-
1715
var app = builder.Build();
1816

1917
app.MapDefaultEndpoints();
2018

21-
if (string.IsNullOrEmpty(connStr))
22-
{
23-
app.Logger.LogWarning("Connection string 'data-storage-connstr' is not configured.");
24-
}
25-
2619
// Configure the HTTP request pipeline.
2720
if (app.Environment.IsDevelopment())
2821
{
@@ -37,7 +30,4 @@
3730
app.MapSummaryEndpoints();
3831
app.MapSettingEndpoints();
3932

40-
4133
app.Run();
42-
43-

NoteBookmark.Api/SettingEndpoints.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using Azure.Data.Tables;
3+
using Azure.Storage.Blobs;
24
using Microsoft.AspNetCore.Http.HttpResults;
35
using NoteBookmark.Domain;
46

@@ -21,17 +23,21 @@ public static void MapSettingEndpoints(this IEndpointRouteBuilder app)
2123
.WithDescription("Save updated settings");
2224
}
2325

24-
static async Task<string> GetNextReadingNotesCounter(IDataStorageService dataStorageService)
26+
27+
28+
static async Task<string> GetNextReadingNotesCounter(TableServiceClient tblClient, BlobServiceClient blobClient)
2529
{
30+
var dataStorageService = new DataStorageService(tblClient, blobClient);
2631
var settings = await dataStorageService.GetSettings();
2732
var counter = settings.ReadingNotesCounter ?? "0";
2833
return counter;
2934
}
3035

31-
static async Task<Results<Ok, BadRequest>> SaveSettings(Settings settings, IDataStorageService dataStorageService)
36+
static async Task<Results<Ok, BadRequest>> SaveSettings(Settings settings, TableServiceClient tblClient, BlobServiceClient blobClient)
3237
{
3338
try
3439
{
40+
var dataStorageService = new DataStorageService(tblClient, blobClient);
3541
var result = await dataStorageService.SaveSettings(settings);
3642
return result ? TypedResults.Ok() : TypedResults.BadRequest();
3743
}
@@ -42,8 +48,9 @@ static async Task<Results<Ok, BadRequest>> SaveSettings(Settings settings, IData
4248
}
4349
}
4450

45-
static async Task<Results<Ok<Settings>, BadRequest>> GetSettings(IDataStorageService dataStorageService)
51+
static async Task<Results<Ok<Settings>, BadRequest>> GetSettings(TableServiceClient tblClient, BlobServiceClient blobClient)
4652
{
53+
var dataStorageService = new DataStorageService(tblClient, blobClient);
4754
var settings = await dataStorageService.GetSettings();
4855
return settings != null ? TypedResults.Ok(settings) : TypedResults.BadRequest();
4956
}

NoteBookmark.Api/SummaryEndpoints.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Azure.Data.Tables;
2+
using Azure.Storage.Blobs;
13
using Microsoft.AspNetCore.Http.HttpResults;
24
using NoteBookmark.Domain;
35

@@ -19,15 +21,17 @@ public static void MapSummaryEndpoints(this IEndpointRouteBuilder app)
1921
endpoints.MapPost("/summary", SaveSummary)
2022
.WithDescription("Create or update the summary");
2123
}
22-
static List<Summary> GetSummaries(IDataStorageService dataStorageService)
24+
static List<Summary> GetSummaries(TableServiceClient tblClient, BlobServiceClient blobClient)
2325
{
26+
var dataStorageService = new DataStorageService(tblClient, blobClient);
2427
return dataStorageService.GetSummaries();
2528
}
2629

27-
static Results<Created<Summary>, BadRequest> SaveSummary(Summary summary, IDataStorageService dataStorageService)
30+
static Results<Created<Summary>, BadRequest> SaveSummary(Summary summary, TableServiceClient tblClient, BlobServiceClient blobClient)
2831
{
2932
try
3033
{
34+
var dataStorageService = new DataStorageService(tblClient, blobClient);
3135
var response = dataStorageService.SaveSummary(summary);
3236
return TypedResults.Created($"/api/summary/{summary.RowKey}", summary);
3337
}
@@ -39,8 +43,9 @@ static Results<Created<Summary>, BadRequest> SaveSummary(Summary summary, IDataS
3943
}
4044

4145
// Get a ReadingNote by number and return it as a results object
42-
static async Task<Results<Ok<ReadingNotes>, NotFound>> GetReadingNotes(string number, IDataStorageService dataStorageService)
46+
static async Task<Results<Ok<ReadingNotes>, NotFound>> GetReadingNotes(string number, TableServiceClient tblClient, BlobServiceClient blobClient)
4347
{
48+
var dataStorageService = new DataStorageService(tblClient, blobClient);
4449
var readingNotes = await dataStorageService.GetReadingNotes(number);
4550
if (readingNotes == null)
4651
{

NoteBookmark.AppHost/NoteBookmark.AppHost.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
</PropertyGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="Aspire.Hosting.AppHost" Version="8.2.2" />
15+
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.0.0" />
16+
<PackageReference Include="Aspire.Hosting.Azure.Storage" Version="9.0.0" />
1617
</ItemGroup>
1718

1819
<ItemGroup>

NoteBookmark.AppHost/Program.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
using Microsoft.Extensions.Configuration;
2+
using Microsoft.Extensions.Hosting;
23
using Projects;
34

45
var builder = DistributedApplication.CreateBuilder(args);
56

6-
var connectionString = builder.AddConnectionString("data-storage-connstr");
7+
var noteStorage = builder.AddAzureStorage("nb-storage");
8+
9+
if (builder.Environment.IsDevelopment())
10+
{
11+
noteStorage.RunAsEmulator();
12+
}
13+
14+
var tables = noteStorage.AddTables("nb-tables");
15+
var blobs = noteStorage.AddBlobs("nb-blobs");
716

817
var api = builder.AddProject<NoteBookmark_Api>("api")
9-
.WithEnvironment("data-storage-connstr",connectionString);
18+
.WithReference(tables)
19+
.WithReference(blobs)
20+
.WaitFor(tables)
21+
.WaitFor(blobs);
1022

1123
builder.AddProject<NoteBookmark_BlazorApp>("blazor-app")
1224
.WithReference(api)
25+
.WaitFor(api)
1326
.WithExternalHttpEndpoints();
1427

1528
builder.Build().Run();

NoteBookmark.AppHost/appsettings.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,5 @@
55
"Microsoft.AspNetCore": "Warning",
66
"Aspire.Hosting.Dcp": "Warning"
77
}
8-
},
9-
"ConnectionStrings": {
10-
"data-storage-connstr": ""
118
}
129
}

0 commit comments

Comments
 (0)