Skip to content

Commit 3ac4448

Browse files
authored
Merge pull request #61 from FBoucher/60-a-few-changes-on-the-summary-page
A few changes on the summary editor
2 parents 27c12a2 + 1fa3c7c commit 3ac4448

File tree

6 files changed

+88
-11
lines changed

6 files changed

+88
-11
lines changed

NoteBookmark.Api/DataStorageService.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,4 +288,23 @@ public async Task UpdatePostReadStatus()
288288
}
289289
}
290290

291+
public async Task<string> SaveReadingNotesMarkdown(string markdown, string number)
292+
{
293+
var containerClient = blobClient.GetBlobContainerClient("final-markdown");
294+
await containerClient.CreateIfNotExistsAsync();
295+
296+
var fileName = $"readingnotes-{number}.md";
297+
var markdownBlobClient = containerClient.GetBlobClient(fileName);
298+
299+
byte[] markdownBytes = Encoding.UTF8.GetBytes(markdown);
300+
var response = await markdownBlobClient.UploadAsync(new MemoryStream(markdownBytes), overwrite: true);
301+
302+
if (response.GetRawResponse().Status == 201 || response.GetRawResponse().Status == 200)
303+
{
304+
return markdownBlobClient.Uri.ToString();
305+
}
306+
307+
return string.Empty;
308+
}
309+
291310
}

NoteBookmark.Api/IDataStorageService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ public interface IDataStorageService
3232
public Task UpdatePostReadStatus();
3333

3434
public bool DeletePost(string rowKey);
35+
36+
public Task<string> SaveReadingNotesMarkdown(string markdown, string number);
3537
}

NoteBookmark.Api/SummaryEndpoints.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public static void MapSummaryEndpoints(this IEndpointRouteBuilder app)
2020

2121
endpoints.MapPost("/summary", SaveSummary)
2222
.WithDescription("Create or update the summary");
23+
24+
endpoints.MapPost("/{number}/markdown", SaveReadingNotesMarkdown)
25+
.WithDescription("Save reading notes as markdown to blob storage");
2326
}
2427
static List<Summary> GetSummaries(TableServiceClient tblClient, BlobServiceClient blobClient)
2528
{
@@ -54,4 +57,25 @@ static async Task<Results<Ok<ReadingNotes>, NotFound>> GetReadingNotes(string nu
5457
return TypedResults.Ok(readingNotes);
5558
}
5659

60+
static async Task<Results<Ok<string>, BadRequest>> SaveReadingNotesMarkdown(string number, MarkdownRequest request, TableServiceClient tblClient, BlobServiceClient blobClient)
61+
{
62+
try
63+
{
64+
var dataStorageService = new DataStorageService(tblClient, blobClient);
65+
var url = await dataStorageService.SaveReadingNotesMarkdown(request.Markdown, number);
66+
if (string.IsNullOrEmpty(url))
67+
{
68+
return TypedResults.BadRequest();
69+
}
70+
return TypedResults.Ok(url);
71+
}
72+
catch (Exception ex)
73+
{
74+
Console.WriteLine($"An error occurred while saving the markdown: {ex.Message}");
75+
return TypedResults.BadRequest();
76+
}
77+
}
78+
5779
}
80+
81+
public record MarkdownRequest(string Markdown);

NoteBookmark.BlazorApp/Components/Pages/SummaryEditor.razor

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ else{
2121
<FluentTab Label=" Edit Summary" Icon="@(new Icons.Regular.Size20.FormSparkle())" Id="tabEdit">
2222
<EditForm Model="@readingNotes" OnValidSubmit="@HandleValidSubmit" FormName="new_readingNotes">
2323
<FluentStack Orientation="Orientation.Vertical" Width="100%">
24-
<FluentButton Type="ButtonType.Submit" Appearance="Appearance.Accent">Save</FluentButton>
24+
25+
<FluentButton Type="ButtonType.Submit" Appearance="Appearance.Accent">Save as Draft</FluentButton>
26+
2527
<DataAnnotationsValidator />
28+
2629
<div style="width: 80%;">
2730
<FluentTextField Label="Title" @bind-Value="readingNotes!.Title"/>
2831
</div>
@@ -32,6 +35,9 @@ else{
3235
<div style="width: 80%;">
3336
<FluentTextField Label="Published Url" @bind-Value="@readingNotes!.PublishedUrl" style="width: 80%;"/>
3437
</div>
38+
<div style="width: 80%;">
39+
<FluentTextArea Placeholder="Introduction" @bind-Value=@readingNotes.Intro Cols="100" Rows="5"></FluentTextArea>
40+
</div>
3541

3642
<div>
3743
@foreach (var note in readingNotes!.Notes)
@@ -74,19 +80,17 @@ else{
7480
<FluentValidationSummary />
7581
</div>
7682
<FluentButton OnClick="@(async () => await Publish())" Type="ButtonType.Button" Appearance="Appearance.Lightweight">Publish</FluentButton> |
77-
<FluentButton Type="ButtonType.Submit" Appearance="Appearance.Accent">Save</FluentButton>
83+
<FluentButton Type="ButtonType.Submit" Appearance="Appearance.Accent">Save as Draft</FluentButton>
7884
</EditForm>
7985

8086
</FluentTab>
8187

8288
<FluentTab Label=" Markdown" Icon="@(new Icons.Regular.Size20.Markdown())" Id="tabMD">
83-
<FluentLabel Typo="Typography.Body" Color="@Color.Disabled"> Read Only: This Markdown is generated from the form.</FluentLabel>
84-
<FluentTextArea Value="@readingNotesMD" ReadOnly="true" Cols="100" Rows="30"/>
89+
<FluentTextArea @bind-Value="@readingNotesMD" Cols="100" Rows="30"/>
8590
</FluentTab>
8691

8792
<FluentTab Label=" Html" Icon="@(new Icons.Regular.Size20.Code())" Id="tabHTML">
88-
<FluentLabel Typo="Typography.Body" Color="@Color.Disabled"> Read Only: This HTML is generated from the Markdown.</FluentLabel>
89-
<FluentTextArea Value="@readingNotesHTML" ReadOnly="true" Cols="100" Rows="30"/>
93+
<FluentTextArea Value="@readingNotesHTML" Cols="100" Rows="30"/>
9094
</FluentTab>
9195

9296
</FluentTabs>
@@ -196,25 +200,44 @@ else{
196200
private void DeleteReadingNote(string category, string RowKey)
197201
{
198202
readingNotes!.Notes[category].RemoveAll(x => x.RowKey == RowKey);
199-
}
200-
203+
}
204+
201205
private async Task Publish()
202206
{
203-
if(readingNotes is not null)
207+
if (readingNotes is not null)
204208
{
209+
if (string.IsNullOrWhiteSpace(readingNotes.PublishedUrl))
210+
{
211+
toastService.ShowError("Published Url is required to publish.");
212+
return;
213+
}
214+
215+
// Save markdown to blob storage only if readingNotesMD is not null or empty
216+
if (!string.IsNullOrWhiteSpace(readingNotesMD))
217+
{
218+
var markdownSaved = await client.SaveReadingNotesMarkdown(readingNotesMD, readingNotes.Number);
219+
if (!markdownSaved)
220+
{
221+
toastService.ShowError("Failed to save markdown file.");
222+
return;
223+
}
224+
}
225+
205226
await HandleValidSubmit();
206227
var settings = await client.GetSettings();
207-
if(settings is not null)
228+
if (settings is not null)
208229
{
209230
var cnt = Convert.ToInt32(settings!.ReadingNotesCounter);
210231
// Only increment if the current Summary is the most recent one.
211-
if(cnt == Convert.ToInt32(readingNotes!.Number))
232+
if (cnt == Convert.ToInt32(readingNotes!.Number))
212233
{
213234
cnt++;
214235
settings.ReadingNotesCounter = (cnt).ToString();
215236
await client.SaveSettings(settings);
216237
}
217238
}
239+
240+
toastService.ShowSuccess("Reading notes published successfully!");
218241
}
219242
}
220243
}

NoteBookmark.BlazorApp/PostNoteClient.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,11 @@ public async Task<bool> DeletePost(string id)
150150
var response = await httpClient.DeleteAsync($"api/posts/{id}");
151151
return response.IsSuccessStatusCode;
152152
}
153+
154+
public async Task<bool> SaveReadingNotesMarkdown(string markdown, string number)
155+
{
156+
var request = new { Markdown = markdown };
157+
var response = await httpClient.PostAsJsonAsync($"api/summary/{number}/markdown", request);
158+
return response.IsSuccessStatusCode;
159+
}
153160
}

NoteBookmark.Domain/ReadingNotes.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public string ToMarkDown()
7171
md.Append(Title + Environment.NewLine);
7272
md.Append('=', Title.Length);
7373

74+
md.Append(Environment.NewLine + Environment.NewLine + this.Intro ?? "" + Environment.NewLine);
75+
7476
//== All Notes
7577
foreach (var key in this.Notes.Keys)
7678
{

0 commit comments

Comments
 (0)