From f610664230365a0b05b761010846e895594b3ed7 Mon Sep 17 00:00:00 2001 From: Ryan Peters Date: Tue, 7 Feb 2023 12:21:22 -0500 Subject: [PATCH 01/19] early work on supporting multiple files --- Controllers/HomeController.cs | 5 +++-- NoteHub.cs | 4 ++-- Services/FileNoteService.cs | 37 +++++++++++++++++++++++------------ Services/INoteService.cs | 4 ++-- appsettings.json | 2 +- wwwroot/js/site.js | 6 ++++-- 6 files changed, 36 insertions(+), 22 deletions(-) diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index c280efe..be8ae83 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -12,9 +12,10 @@ public class HomeController : Controller this.noteService = noteService; } - public IActionResult Index() + [Route("{noteName?}")] + public IActionResult Index(string? noteName) { - var content = noteService.Get(); + var content = noteService.Get(noteName); return View((object)content); } diff --git a/NoteHub.cs b/NoteHub.cs index 881105b..057f101 100644 --- a/NoteHub.cs +++ b/NoteHub.cs @@ -12,9 +12,9 @@ namespace BinaryDad.Notes this.noteService = noteService; } - public async Task SaveNote(string content) + public async Task SaveNote(string content, string? noteName) { - noteService.Save(content); + noteService.Save(content, noteName); await Clients.Others.SendAsync("updateNote", content); } diff --git a/Services/FileNoteService.cs b/Services/FileNoteService.cs index 82137bb..edef53d 100644 --- a/Services/FileNoteService.cs +++ b/Services/FileNoteService.cs @@ -2,27 +2,38 @@ { public class FileNoteService : INoteService { - private readonly string? filePath; + private readonly string? defaultFileName; public FileNoteService(IConfiguration configuration) { - filePath = configuration["ContentFilePath"]; + defaultFileName = configuration["ContentFilePath"]; + + CheckFile(defaultFileName); + } + + public string Get(string? noteName) + { + CheckFile(noteName); + + return File.ReadAllText(GetFileName(noteName)); + } + + public void Save(string content, string? noteName) + { + File.WriteAllText(GetFileName(noteName), content); + } + + private void CheckFile(string noteName) + { + var fileName = GetFileName(noteName); // ensure initialized - if (!File.Exists(filePath)) + if (!File.Exists(fileName)) { - Save("Hi! Feel free to start typing. Everything will be saved soon after you are done typing."); + Save("Hi! Feel free to start typing. Everything will be saved soon after you are done typing.", fileName); } } - public string Get() - { - return File.ReadAllText(filePath); - } - - public void Save(string content) - { - File.WriteAllText(filePath, content); - } + private string GetFileName(string? noteName) => string.IsNullOrWhiteSpace(noteName) ? defaultFileName : noteName; } } diff --git a/Services/INoteService.cs b/Services/INoteService.cs index 4aac958..2451e08 100644 --- a/Services/INoteService.cs +++ b/Services/INoteService.cs @@ -2,7 +2,7 @@ { public interface INoteService { - string Get(); - void Save(string content); + string Get(string? noteName); + void Save(string content, string? noteName); } } diff --git a/appsettings.json b/appsettings.json index 46d38c3..ad4f7fe 100644 --- a/appsettings.json +++ b/appsettings.json @@ -6,5 +6,5 @@ } }, "AllowedHosts": "*", - "ContentFilePath": "content.txt" + "ContentFilePath": "content" } diff --git a/wwwroot/js/site.js b/wwwroot/js/site.js index 6aa913e..4ff5e9e 100644 --- a/wwwroot/js/site.js +++ b/wwwroot/js/site.js @@ -1,4 +1,6 @@ -let connection = new signalR.HubConnectionBuilder() +const noteName = document.location.pathname.substring(1); + +let connection = new signalR.HubConnectionBuilder() .withAutomaticReconnect() .withUrl("/noteHub") .build(); @@ -29,7 +31,7 @@ function saveContent($textarea) { var content = $textarea.val(); - connection.invoke('SaveNote', content).then(function () { + connection.invoke('SaveNote', content, noteName).then(function () { showToast('#saved-indicator'); }).catch(function (err) { console.error(err.toString()); From b89a557af1b92cb41ee3a2bc7d6aa2ed1f95b2c6 Mon Sep 17 00:00:00 2001 From: Ryan Peters Date: Thu, 9 Mar 2023 22:06:21 -0500 Subject: [PATCH 02/19] working! --- NoteContext.cs | 7 +++++++ NoteHub.cs | 18 +++++++++++++++++- wwwroot/js/site.js | 2 +- 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 NoteContext.cs diff --git a/NoteContext.cs b/NoteContext.cs new file mode 100644 index 0000000..eaed12f --- /dev/null +++ b/NoteContext.cs @@ -0,0 +1,7 @@ +namespace BinaryDad.Notes +{ + public static class NoteContext + { + public static IDictionary ClientNotes { get; } = new Dictionary(); + } +} \ No newline at end of file diff --git a/NoteHub.cs b/NoteHub.cs index 057f101..791d375 100644 --- a/NoteHub.cs +++ b/NoteHub.cs @@ -12,11 +12,27 @@ namespace BinaryDad.Notes this.noteService = noteService; } + public override Task OnConnectedAsync() + { + var noteName = Context.GetHttpContext().Request.Query["noteName"]; + + NoteContext.ClientNotes[Context.ConnectionId] = noteName; + + return base.OnConnectedAsync(); + } + public async Task SaveNote(string content, string? noteName) { noteService.Save(content, noteName); - await Clients.Others.SendAsync("updateNote", content); + var clientConnections = NoteContext.ClientNotes + .Where(c => c.Value == noteName && c.Key != Context.ConnectionId) + .Select(c => c.Key) + .ToList(); + + await Clients + .Clients(clientConnections) + .SendAsync("updateNote", content); } } } diff --git a/wwwroot/js/site.js b/wwwroot/js/site.js index 4ff5e9e..267fcf0 100644 --- a/wwwroot/js/site.js +++ b/wwwroot/js/site.js @@ -2,7 +2,7 @@ let connection = new signalR.HubConnectionBuilder() .withAutomaticReconnect() - .withUrl("/noteHub") + .withUrl(`/noteHub?noteName=${noteName}`) .build(); function start() { From c9f22e09782516eb17679e248b40f9a8fea5d394 Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 10 Mar 2023 03:14:28 +0000 Subject: [PATCH 03/19] add comments --- NoteHub.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NoteHub.cs b/NoteHub.cs index 791d375..20ad638 100644 --- a/NoteHub.cs +++ b/NoteHub.cs @@ -25,11 +25,13 @@ namespace BinaryDad.Notes { noteService.Save(content, noteName); + // find all other connections except for the current one var clientConnections = NoteContext.ClientNotes .Where(c => c.Value == noteName && c.Key != Context.ConnectionId) .Select(c => c.Key) .ToList(); + // update note for all other clients await Clients .Clients(clientConnections) .SendAsync("updateNote", content); From 77e6ed78fd16e5f343a6c45dd75cc9095e2122ae Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 12 May 2023 12:13:35 -0400 Subject: [PATCH 04/19] adjusted file service, added get all notes method --- Services/FileNoteService.cs | 28 +++++++++++++++++++++------- Services/INoteService.cs | 1 + appsettings.json | 3 ++- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/Services/FileNoteService.cs b/Services/FileNoteService.cs index edef53d..3e6c92e 100644 --- a/Services/FileNoteService.cs +++ b/Services/FileNoteService.cs @@ -3,10 +3,12 @@ public class FileNoteService : INoteService { private readonly string? defaultFileName; + private readonly string? filePath; public FileNoteService(IConfiguration configuration) { - defaultFileName = configuration["ContentFilePath"]; + defaultFileName = configuration["DefaultContentFileName"].Trim().ToLower(); + filePath = configuration["ContentFilePath"].Trim(); CheckFile(defaultFileName); } @@ -15,25 +17,37 @@ { CheckFile(noteName); - return File.ReadAllText(GetFileName(noteName)); + return File.ReadAllText(GetFilePath(noteName)); + } + + public ICollection GetNoteNames() + { + return Directory.GetFiles(filePath); } public void Save(string content, string? noteName) { - File.WriteAllText(GetFileName(noteName), content); + File.WriteAllText(GetFilePath(noteName), content); } private void CheckFile(string noteName) { - var fileName = GetFileName(noteName); + var filePath = GetFilePath(noteName); // ensure initialized - if (!File.Exists(fileName)) + if (!File.Exists(filePath)) { - Save("Hi! Feel free to start typing. Everything will be saved soon after you are done typing.", fileName); + Save("Hi! Feel free to start typing. Everything will be saved soon after you are done typing.", noteName); } } - private string GetFileName(string? noteName) => string.IsNullOrWhiteSpace(noteName) ? defaultFileName : noteName; + private string GetFilePath(string? noteName) + { + noteName = string.IsNullOrWhiteSpace(noteName) ? defaultFileName : noteName; + + noteName = noteName.Trim().ToLower(); + + return Path.Combine(filePath, noteName); + } } } diff --git a/Services/INoteService.cs b/Services/INoteService.cs index 2451e08..e61dbde 100644 --- a/Services/INoteService.cs +++ b/Services/INoteService.cs @@ -2,6 +2,7 @@ { public interface INoteService { + ICollection GetNoteNames(); string Get(string? noteName); void Save(string content, string? noteName); } diff --git a/appsettings.json b/appsettings.json index ad4f7fe..e7daff3 100644 --- a/appsettings.json +++ b/appsettings.json @@ -6,5 +6,6 @@ } }, "AllowedHosts": "*", - "ContentFilePath": "content" + "DefaultContentFileName": "content", + "ContentFilePath": "notes" } From 16f4dc9a769121b491239c6204b42f623cb4c099 Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 12 May 2023 12:38:13 -0400 Subject: [PATCH 05/19] working with links --- .vscode/launch.json | 2 +- Controllers/HomeController.cs | 11 ++++++++--- Models/ContentModel.cs | 7 +++++++ Models/ErrorViewModel.cs | 8 -------- NoteHub.cs | 2 +- Notes/content | 1 + Notes/homelab-stuff | 1 + Notes/my-notes | 1 + Notes/sdfsfd | 1 + Properties/launchSettings.json | 23 ---------------------- Services/FileNoteService.cs | 10 ++++++---- Services/INoteService.cs | 4 ++-- Views/Home/Index.cshtml | 11 +++++++++-- Views/Shared/Error.cshtml | 25 ------------------------ wwwroot/css/site.css | 35 +++++++++++++++++++++++++--------- 15 files changed, 64 insertions(+), 78 deletions(-) create mode 100644 Models/ContentModel.cs delete mode 100644 Models/ErrorViewModel.cs create mode 100644 Notes/content create mode 100644 Notes/homelab-stuff create mode 100644 Notes/my-notes create mode 100644 Notes/sdfsfd delete mode 100644 Views/Shared/Error.cshtml diff --git a/.vscode/launch.json b/.vscode/launch.json index 1768fa5..47886ae 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -17,7 +17,7 @@ // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser "serverReadyAction": { "action": "openExternally", - "pattern": "\\bNow listening on:\\s+(https?://\\S+)" + "pattern": "\\bNow listening on:\\s+(http?://\\S+)" }, "env": { "ASPNETCORE_ENVIRONMENT": "Development" diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index be8ae83..8841348 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -1,4 +1,5 @@ -using BinaryDad.Notes.Services; +using BinaryDad.Notes.Models; +using BinaryDad.Notes.Services; using Microsoft.AspNetCore.Mvc; namespace BinaryDad.Notes.Controllers; @@ -15,8 +16,12 @@ public class HomeController : Controller [Route("{noteName?}")] public IActionResult Index(string? noteName) { - var content = noteService.Get(noteName); + var model = new ContentModel + { + Text = noteService.GetText(noteName), + NoteNames = noteService.GetNoteNames() + }; - return View((object)content); + return View(model); } } diff --git a/Models/ContentModel.cs b/Models/ContentModel.cs new file mode 100644 index 0000000..26f2e64 --- /dev/null +++ b/Models/ContentModel.cs @@ -0,0 +1,7 @@ +namespace BinaryDad.Notes.Models; + +public class ContentModel +{ + public ICollection NoteNames { get; set; } + public string Text { get; set; } +} \ No newline at end of file diff --git a/Models/ErrorViewModel.cs b/Models/ErrorViewModel.cs deleted file mode 100644 index 8e3c889..0000000 --- a/Models/ErrorViewModel.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace BinaryDad.Notes.Models; - -public class ErrorViewModel -{ - public string? RequestId { get; set; } - - public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); -} diff --git a/NoteHub.cs b/NoteHub.cs index 20ad638..52836ef 100644 --- a/NoteHub.cs +++ b/NoteHub.cs @@ -23,7 +23,7 @@ namespace BinaryDad.Notes public async Task SaveNote(string content, string? noteName) { - noteService.Save(content, noteName); + noteService.SaveText(content, noteName); // find all other connections except for the current one var clientConnections = NoteContext.ClientNotes diff --git a/Notes/content b/Notes/content new file mode 100644 index 0000000..521df3e --- /dev/null +++ b/Notes/content @@ -0,0 +1 @@ +default page \ No newline at end of file diff --git a/Notes/homelab-stuff b/Notes/homelab-stuff new file mode 100644 index 0000000..2fe782f --- /dev/null +++ b/Notes/homelab-stuff @@ -0,0 +1 @@ +some homelab stuff \ No newline at end of file diff --git a/Notes/my-notes b/Notes/my-notes new file mode 100644 index 0000000..ff4a3a5 --- /dev/null +++ b/Notes/my-notes @@ -0,0 +1 @@ +sdsada \ No newline at end of file diff --git a/Notes/sdfsfd b/Notes/sdfsfd new file mode 100644 index 0000000..6197c15 --- /dev/null +++ b/Notes/sdfsfd @@ -0,0 +1 @@ +this is on weird page \ No newline at end of file diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json index b02486a..67f6526 100644 --- a/Properties/launchSettings.json +++ b/Properties/launchSettings.json @@ -8,29 +8,6 @@ }, "dotnetRunMessages": true, "applicationUrl": "http://localhost:5015" - }, - "https": { - "commandName": "Project", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - }, - "dotnetRunMessages": true, - "applicationUrl": "https://localhost:7042;http://localhost:5015" - }, - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "Docker": { - "commandName": "Docker", - "launchBrowser": true, - "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", - "publishAllPorts": true, - "useSSL": true } }, "iisSettings": { diff --git a/Services/FileNoteService.cs b/Services/FileNoteService.cs index 3e6c92e..6d1eed5 100644 --- a/Services/FileNoteService.cs +++ b/Services/FileNoteService.cs @@ -13,7 +13,7 @@ CheckFile(defaultFileName); } - public string Get(string? noteName) + public string GetText(string? noteName) { CheckFile(noteName); @@ -22,10 +22,12 @@ public ICollection GetNoteNames() { - return Directory.GetFiles(filePath); + return Directory.GetFiles(filePath) + .Select(f => Path.GetFileName(f)) + .ToList(); } - public void Save(string content, string? noteName) + public void SaveText(string content, string? noteName) { File.WriteAllText(GetFilePath(noteName), content); } @@ -37,7 +39,7 @@ // ensure initialized if (!File.Exists(filePath)) { - Save("Hi! Feel free to start typing. Everything will be saved soon after you are done typing.", noteName); + SaveText("Hi! Feel free to start typing. Everything will be saved soon after you are done typing.", noteName); } } diff --git a/Services/INoteService.cs b/Services/INoteService.cs index e61dbde..a5467b2 100644 --- a/Services/INoteService.cs +++ b/Services/INoteService.cs @@ -3,7 +3,7 @@ public interface INoteService { ICollection GetNoteNames(); - string Get(string? noteName); - void Save(string content, string? noteName); + string GetText(string? noteName); + void SaveText(string content, string? noteName); } } diff --git a/Views/Home/Index.cshtml b/Views/Home/Index.cshtml index 18616fa..8dee1b3 100644 --- a/Views/Home/Index.cshtml +++ b/Views/Home/Index.cshtml @@ -1,6 +1,13 @@ -@model string +@model ContentModel - + + +
+ @foreach (var note in Model.NoteNames) + { + @note + } +
Saved
Updated
\ No newline at end of file diff --git a/Views/Shared/Error.cshtml b/Views/Shared/Error.cshtml deleted file mode 100644 index a1e0478..0000000 --- a/Views/Shared/Error.cshtml +++ /dev/null @@ -1,25 +0,0 @@ -@model ErrorViewModel -@{ - ViewData["Title"] = "Error"; -} - -

Error.

-

An error occurred while processing your request.

- -@if (Model.ShowRequestId) -{ -

- Request ID: @Model.RequestId -

-} - -

Development Mode

-

- Swapping to Development environment will display more detailed information about the error that occurred. -

-

- The Development environment shouldn't be enabled for deployed applications. - It can result in displaying sensitive information from exceptions to end users. - For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development - and restarting the app. -

diff --git a/wwwroot/css/site.css b/wwwroot/css/site.css index ce64259..40ba5c2 100644 --- a/wwwroot/css/site.css +++ b/wwwroot/css/site.css @@ -20,6 +20,23 @@ body { min-height: 100%; } +div.note-names { + position: fixed; + bottom: 0; + left: 0; + font-size: 11px; +} + +div.note-names a { + color: #666; + padding: 0 5px; + text-decoration: none; +} + +div.note-names a:not(:last-of-type) { + border-right: 1px solid #666; +} + textarea { width: 100%; height: 100%; @@ -52,14 +69,14 @@ textarea { border-radius: 5px 5px 0 0; } - .toast.show { - bottom: 0; - } +.toast.show { + bottom: 0; +} - .toast#saved-indicator { - background-color: green; - } +.toast#saved-indicator { + background-color: green; +} - .toast#update-indicator { - background-color: orangered; - } +.toast#update-indicator { + background-color: orangered; +} \ No newline at end of file From 07d93d9c285e3587146b17b6777815a24d6d48ee Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 12 May 2023 12:47:22 -0400 Subject: [PATCH 06/19] remove notes --- Notes/content | 1 - Notes/homelab-stuff | 1 - Notes/my-notes | 1 - Notes/sdfsfd | 1 - 4 files changed, 4 deletions(-) delete mode 100644 Notes/content delete mode 100644 Notes/homelab-stuff delete mode 100644 Notes/my-notes delete mode 100644 Notes/sdfsfd diff --git a/Notes/content b/Notes/content deleted file mode 100644 index 521df3e..0000000 --- a/Notes/content +++ /dev/null @@ -1 +0,0 @@ -default page \ No newline at end of file diff --git a/Notes/homelab-stuff b/Notes/homelab-stuff deleted file mode 100644 index 2fe782f..0000000 --- a/Notes/homelab-stuff +++ /dev/null @@ -1 +0,0 @@ -some homelab stuff \ No newline at end of file diff --git a/Notes/my-notes b/Notes/my-notes deleted file mode 100644 index ff4a3a5..0000000 --- a/Notes/my-notes +++ /dev/null @@ -1 +0,0 @@ -sdsada \ No newline at end of file diff --git a/Notes/sdfsfd b/Notes/sdfsfd deleted file mode 100644 index 6197c15..0000000 --- a/Notes/sdfsfd +++ /dev/null @@ -1 +0,0 @@ -this is on weird page \ No newline at end of file From b60a246c344644ced925c9cf0ed56ff0641ad169 Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 12 May 2023 12:47:50 -0400 Subject: [PATCH 07/19] style change --- wwwroot/css/site.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wwwroot/css/site.css b/wwwroot/css/site.css index 40ba5c2..2b038f7 100644 --- a/wwwroot/css/site.css +++ b/wwwroot/css/site.css @@ -22,7 +22,7 @@ body { div.note-names { position: fixed; - bottom: 0; + bottom: 5px; left: 0; font-size: 11px; } From faaa0dba8f210b0825a896376454262736c2e5a3 Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 12 May 2023 12:47:54 -0400 Subject: [PATCH 08/19] add delete endpoint --- Controllers/HomeController.cs | 8 ++++++++ Services/FileNoteService.cs | 7 +++++++ Services/INoteService.cs | 1 + 3 files changed, 16 insertions(+) diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index 8841348..02fc0c4 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -24,4 +24,12 @@ public class HomeController : Controller return View(model); } + + [Route("{noteName}/delete")] + public IActionResult Delete(string noteName) + { + noteService.DeleteNote(noteName); + + return Redirect("/"); + } } diff --git a/Services/FileNoteService.cs b/Services/FileNoteService.cs index 6d1eed5..93c7297 100644 --- a/Services/FileNoteService.cs +++ b/Services/FileNoteService.cs @@ -32,6 +32,13 @@ File.WriteAllText(GetFilePath(noteName), content); } + public void DeleteNote(string noteName) + { + var filePath = GetFilePath(noteName); + + File.Delete(filePath); + } + private void CheckFile(string noteName) { var filePath = GetFilePath(noteName); diff --git a/Services/INoteService.cs b/Services/INoteService.cs index a5467b2..7d9ff99 100644 --- a/Services/INoteService.cs +++ b/Services/INoteService.cs @@ -5,5 +5,6 @@ ICollection GetNoteNames(); string GetText(string? noteName); void SaveText(string content, string? noteName); + void DeleteNote(string noteName); } } From 114e1499f896248f9951dd469d19f4d793c99edc Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 12 May 2023 12:55:05 -0400 Subject: [PATCH 09/19] fix some nullable issues --- Models/ContentModel.cs | 4 ++-- Services/FileNoteService.cs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Models/ContentModel.cs b/Models/ContentModel.cs index 26f2e64..e91d81f 100644 --- a/Models/ContentModel.cs +++ b/Models/ContentModel.cs @@ -2,6 +2,6 @@ namespace BinaryDad.Notes.Models; public class ContentModel { - public ICollection NoteNames { get; set; } - public string Text { get; set; } + public ICollection NoteNames { get; set; } = new List(); + public string Text { get; set; } = string.Empty; } \ No newline at end of file diff --git a/Services/FileNoteService.cs b/Services/FileNoteService.cs index 93c7297..9decc84 100644 --- a/Services/FileNoteService.cs +++ b/Services/FileNoteService.cs @@ -2,8 +2,8 @@ { public class FileNoteService : INoteService { - private readonly string? defaultFileName; - private readonly string? filePath; + private readonly string defaultFileName; + private readonly string filePath; public FileNoteService(IConfiguration configuration) { @@ -39,7 +39,7 @@ File.Delete(filePath); } - private void CheckFile(string noteName) + private void CheckFile(string? noteName) { var filePath = GetFilePath(noteName); From 400f20e9163f2f228826610107fb77d569e19ead Mon Sep 17 00:00:00 2001 From: Ryan Peters Date: Wed, 17 May 2023 21:44:41 -0400 Subject: [PATCH 10/19] working nicely --- Controllers/NoteController.cs | 5 +++-- Models/ContentModel.cs | 1 + Services/FileNoteService.cs | 16 +++++--------- Services/INoteService.cs | 4 ++-- Views/Note/Index.cshtml | 6 +++-- appsettings.json | 5 +++-- notes/birds | 1 + notes/default | 1 + notes/sdfsds | 1 + wwwroot/css/site.css | 41 ++++++++++++++++++++--------------- 10 files changed, 44 insertions(+), 37 deletions(-) create mode 100644 notes/birds create mode 100644 notes/default create mode 100644 notes/sdfsds diff --git a/Controllers/NoteController.cs b/Controllers/NoteController.cs index 7b835d3..9f9fe79 100644 --- a/Controllers/NoteController.cs +++ b/Controllers/NoteController.cs @@ -15,11 +15,12 @@ public class NoteController : Controller this.noteService = noteService; } - [Route("{noteName?}")] - public IActionResult Index(string? noteName) + [Route("{noteName=default}")] + public IActionResult Index(string noteName) { var model = new ContentModel { + CurrentNote = noteName, Text = noteService.GetText(noteName), NoteNames = noteService.GetNoteNames() }; diff --git a/Models/ContentModel.cs b/Models/ContentModel.cs index e91d81f..3631056 100644 --- a/Models/ContentModel.cs +++ b/Models/ContentModel.cs @@ -2,6 +2,7 @@ namespace BinaryDad.Notes.Models; public class ContentModel { + public string CurrentNote { get; set; } = string.Empty; public ICollection NoteNames { get; set; } = new List(); public string Text { get; set; } = string.Empty; } \ No newline at end of file diff --git a/Services/FileNoteService.cs b/Services/FileNoteService.cs index 9decc84..0b9f035 100644 --- a/Services/FileNoteService.cs +++ b/Services/FileNoteService.cs @@ -2,18 +2,14 @@ { public class FileNoteService : INoteService { - private readonly string defaultFileName; private readonly string filePath; public FileNoteService(IConfiguration configuration) { - defaultFileName = configuration["DefaultContentFileName"].Trim().ToLower(); - filePath = configuration["ContentFilePath"].Trim(); - - CheckFile(defaultFileName); + filePath = configuration["FileNoteService:ContentFilePath"].Trim(); } - public string GetText(string? noteName) + public string GetText(string noteName) { CheckFile(noteName); @@ -27,7 +23,7 @@ .ToList(); } - public void SaveText(string content, string? noteName) + public void SaveText(string content, string noteName) { File.WriteAllText(GetFilePath(noteName), content); } @@ -39,7 +35,7 @@ File.Delete(filePath); } - private void CheckFile(string? noteName) + private void CheckFile(string noteName) { var filePath = GetFilePath(noteName); @@ -50,10 +46,8 @@ } } - private string GetFilePath(string? noteName) + private string GetFilePath(string noteName) { - noteName = string.IsNullOrWhiteSpace(noteName) ? defaultFileName : noteName; - noteName = noteName.Trim().ToLower(); return Path.Combine(filePath, noteName); diff --git a/Services/INoteService.cs b/Services/INoteService.cs index 7d9ff99..9ba6fc7 100644 --- a/Services/INoteService.cs +++ b/Services/INoteService.cs @@ -3,8 +3,8 @@ public interface INoteService { ICollection GetNoteNames(); - string GetText(string? noteName); - void SaveText(string content, string? noteName); + string GetText(string noteName); + void SaveText(string content, string noteName); void DeleteNote(string noteName); } } diff --git a/Views/Note/Index.cshtml b/Views/Note/Index.cshtml index 8dee1b3..f643373 100644 --- a/Views/Note/Index.cshtml +++ b/Views/Note/Index.cshtml @@ -3,9 +3,11 @@
- @foreach (var note in Model.NoteNames) + @foreach (var note in Model.NoteNames.Order()) { - @note + var css = note.Equals(Model.CurrentNote, StringComparison.OrdinalIgnoreCase) ? "current" : null; + + @note }
diff --git a/appsettings.json b/appsettings.json index e7daff3..17bfc41 100644 --- a/appsettings.json +++ b/appsettings.json @@ -6,6 +6,7 @@ } }, "AllowedHosts": "*", - "DefaultContentFileName": "content", - "ContentFilePath": "notes" + "FileNoteService": { + "ContentFilePath": "notes" + } } diff --git a/notes/birds b/notes/birds new file mode 100644 index 0000000..9fd7e12 --- /dev/null +++ b/notes/birds @@ -0,0 +1 @@ +some bird stufffg hfghf \ No newline at end of file diff --git a/notes/default b/notes/default new file mode 100644 index 0000000..3e82765 --- /dev/null +++ b/notes/default @@ -0,0 +1 @@ +default tesxtg dg \ No newline at end of file diff --git a/notes/sdfsds b/notes/sdfsds new file mode 100644 index 0000000..07c01f0 --- /dev/null +++ b/notes/sdfsds @@ -0,0 +1 @@ +Hi! Feel free to start typing. Every \ No newline at end of file diff --git a/wwwroot/css/site.css b/wwwroot/css/site.css index 89c8b0a..f815c5e 100644 --- a/wwwroot/css/site.css +++ b/wwwroot/css/site.css @@ -24,18 +24,23 @@ div.note-names { position: fixed; bottom: 5px; left: 0; - font-size: 11px; + font-size: 14px; + opacity: 0.5; } -div.note-names a { - color: #666; - padding: 0 5px; - text-decoration: none; -} + div.note-names a { + color: #666; + padding-left: 10px; + text-decoration: none; + } -div.note-names a:not(:last-of-type) { - border-right: 1px solid #666; -} + div.note-names a.current { + font-weight: bold; + } + + div .note-names a:not(:last-of-type) { + border-right: 1px solid #666; + } textarea { width: 100%; @@ -69,17 +74,17 @@ textarea { border-radius: 5px 5px 0 0; } -.toast.show { - bottom: 0; -} + .toast.show { + bottom: 0; + } -.toast#saved-indicator { - background-color: green; -} + .toast#saved-indicator { + background-color: green; + } -.toast#update-indicator { - background-color: orangered; -} + .toast#update-indicator { + background-color: orangered; + } form input[type=password] { display: block; From fced31f42f4e8b39372f5e7865ed4f1e22ce342d Mon Sep 17 00:00:00 2001 From: Ryan Peters Date: Wed, 17 May 2023 21:48:39 -0400 Subject: [PATCH 11/19] remove old notes --- notes/birds | 1 - notes/default | 1 - notes/sdfsds | 1 - 3 files changed, 3 deletions(-) delete mode 100644 notes/birds delete mode 100644 notes/default delete mode 100644 notes/sdfsds diff --git a/notes/birds b/notes/birds deleted file mode 100644 index 9fd7e12..0000000 --- a/notes/birds +++ /dev/null @@ -1 +0,0 @@ -some bird stufffg hfghf \ No newline at end of file diff --git a/notes/default b/notes/default deleted file mode 100644 index 3e82765..0000000 --- a/notes/default +++ /dev/null @@ -1 +0,0 @@ -default tesxtg dg \ No newline at end of file diff --git a/notes/sdfsds b/notes/sdfsds deleted file mode 100644 index 07c01f0..0000000 --- a/notes/sdfsds +++ /dev/null @@ -1 +0,0 @@ -Hi! Feel free to start typing. Every \ No newline at end of file From 1b423bdfffea5230774573e19be6e461be61de54 Mon Sep 17 00:00:00 2001 From: Ryan Peters Date: Wed, 17 May 2023 21:48:50 -0400 Subject: [PATCH 12/19] ignore notes folder --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index b1a10ab..8e17bd7 100644 --- a/.gitignore +++ b/.gitignore @@ -362,3 +362,5 @@ MigrationBackup/ # Fody - auto-generated XML schema FodyWeavers.xsd /content.txt + +/Notes \ No newline at end of file From e89f9f74fe2921fa508afe7dfaf658489487fbaf Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 28 Jul 2023 20:19:59 +0000 Subject: [PATCH 13/19] get note name from api --- Controllers/ApiController.cs | 2 +- wwwroot/js/site.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Controllers/ApiController.cs b/Controllers/ApiController.cs index 2112ea0..54a5c03 100644 --- a/Controllers/ApiController.cs +++ b/Controllers/ApiController.cs @@ -12,6 +12,6 @@ namespace BinaryDad.Notes.Controllers this.noteService = noteService; } - public string Note() => noteService.Get(); + public string Note(string noteName) => noteService.GetText(noteName); } } diff --git a/wwwroot/js/site.js b/wwwroot/js/site.js index 62482a4..18182aa 100644 --- a/wwwroot/js/site.js +++ b/wwwroot/js/site.js @@ -54,10 +54,11 @@ $(function () { $textarea.val(content); showToast('#update-indicator'); }); + // update content after reconnected connection.onreconnected(function() { - $.get('api/note', function(content) { + $.get(`api/note?noteName=${noteName}`, function(content) { $textarea.val(content); showToast('#update-indicator'); console.log('Refreshed after disconnect'); From 41de9e60c4998778434fe5f20075640605766830 Mon Sep 17 00:00:00 2001 From: Ryan Peters Date: Fri, 4 Aug 2023 18:26:22 -0400 Subject: [PATCH 14/19] still figuring out how to handle default note name... --- Controllers/ApiController.cs | 2 ++ Controllers/NoteController.cs | 9 +++++++-- Services/FileNoteService.cs | 9 ++++++++- Views/Note/Index.cshtml | 9 ++++++++- wwwroot/js/site.js | 4 ++-- 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Controllers/ApiController.cs b/Controllers/ApiController.cs index 54a5c03..fcabb81 100644 --- a/Controllers/ApiController.cs +++ b/Controllers/ApiController.cs @@ -3,6 +3,7 @@ using BinaryDad.Notes.Services; namespace BinaryDad.Notes.Controllers { + [ApiController] public class ApiController : ControllerBase { private readonly INoteService noteService; @@ -12,6 +13,7 @@ namespace BinaryDad.Notes.Controllers this.noteService = noteService; } + [Route("note/{noteName}")] public string Note(string noteName) => noteService.GetText(noteName); } } diff --git a/Controllers/NoteController.cs b/Controllers/NoteController.cs index 9f9fe79..839646d 100644 --- a/Controllers/NoteController.cs +++ b/Controllers/NoteController.cs @@ -1,4 +1,4 @@ -using BinaryDad.Notes.Models; +using BinaryDad.Notes.Models; using BinaryDad.Notes.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -15,9 +15,14 @@ public class NoteController : Controller this.noteService = noteService; } - [Route("{noteName=default}")] + [Route("{noteName?}")] public IActionResult Index(string noteName) { + if (string.IsNullOrWhiteSpace(noteName)) + { + noteName = "default"; + } + var model = new ContentModel { CurrentNote = noteName, diff --git a/Services/FileNoteService.cs b/Services/FileNoteService.cs index 0b9f035..4c5fed6 100644 --- a/Services/FileNoteService.cs +++ b/Services/FileNoteService.cs @@ -4,9 +4,16 @@ { private readonly string filePath; + private static readonly string defaultFilePath = "notes"; + public FileNoteService(IConfiguration configuration) { - filePath = configuration["FileNoteService:ContentFilePath"].Trim(); + filePath = configuration["FileNoteService:ContentFilePath"]; + + if (string.IsNullOrWhiteSpace(filePath)) + { + filePath = defaultFilePath; + } } public string GetText(string noteName) diff --git a/Views/Note/Index.cshtml b/Views/Note/Index.cshtml index f643373..f2a71f9 100644 --- a/Views/Note/Index.cshtml +++ b/Views/Note/Index.cshtml @@ -12,4 +12,11 @@
Saved
-
Updated
\ No newline at end of file +
Updated
+ +@section Scripts +{ + +} diff --git a/wwwroot/js/site.js b/wwwroot/js/site.js index 18182aa..ddeaa87 100644 --- a/wwwroot/js/site.js +++ b/wwwroot/js/site.js @@ -1,4 +1,4 @@ -const noteName = document.location.pathname.substring(1); +let noteName = '';//document.location.pathname.substring(1); let connection = new signalR.HubConnectionBuilder() .withAutomaticReconnect() @@ -58,7 +58,7 @@ $(function () { // update content after reconnected connection.onreconnected(function() { - $.get(`api/note?noteName=${noteName}`, function(content) { + $.get(`api/note/${noteName}`, function(content) { $textarea.val(content); showToast('#update-indicator'); console.log('Refreshed after disconnect'); From 4269188c8c08d76523d7b78781a48cb4d34456b3 Mon Sep 17 00:00:00 2001 From: Ryan Peters Date: Fri, 5 Jan 2024 13:46:01 -0500 Subject: [PATCH 15/19] some fixes --- Views/Note/Index.cshtml | 2 +- Views/Shared/_Layout.cshtml | 3 ++- wwwroot/js/site.js | 4 +--- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Views/Note/Index.cshtml b/Views/Note/Index.cshtml index f2a71f9..bda3c3f 100644 --- a/Views/Note/Index.cshtml +++ b/Views/Note/Index.cshtml @@ -17,6 +17,6 @@ @section Scripts { } diff --git a/Views/Shared/_Layout.cshtml b/Views/Shared/_Layout.cshtml index 40d1bb9..cd24bb5 100644 --- a/Views/Shared/_Layout.cshtml +++ b/Views/Shared/_Layout.cshtml @@ -11,11 +11,12 @@ @RenderBody() + @RenderSection("scripts", false) + - @RenderSection("scripts", false) diff --git a/wwwroot/js/site.js b/wwwroot/js/site.js index ddeaa87..0cc05d1 100644 --- a/wwwroot/js/site.js +++ b/wwwroot/js/site.js @@ -1,6 +1,4 @@ -let noteName = '';//document.location.pathname.substring(1); - -let connection = new signalR.HubConnectionBuilder() +let connection = new signalR.HubConnectionBuilder() .withAutomaticReconnect() .withUrl(`/noteHub?noteName=${noteName}`) .build(); From ac13770ce0cc85749715aca4b90ed526609fb94d Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 6 Jan 2024 21:00:49 -0500 Subject: [PATCH 16/19] change how default note name is set --- Controllers/NoteController.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Controllers/NoteController.cs b/Controllers/NoteController.cs index 839646d..a379372 100644 --- a/Controllers/NoteController.cs +++ b/Controllers/NoteController.cs @@ -15,14 +15,9 @@ public class NoteController : Controller this.noteService = noteService; } - [Route("{noteName?}")] + [Route("{noteName=default}")] public IActionResult Index(string noteName) { - if (string.IsNullOrWhiteSpace(noteName)) - { - noteName = "default"; - } - var model = new ContentModel { CurrentNote = noteName, From 8b04e4efd298901cf75f3bcf72bf2e41dc227e31 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 6 Jan 2024 21:01:17 -0500 Subject: [PATCH 17/19] change folder path naming --- Services/FileNoteService.cs | 16 +++++++++------- appsettings.json | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Services/FileNoteService.cs b/Services/FileNoteService.cs index 4c5fed6..cc8ffb2 100644 --- a/Services/FileNoteService.cs +++ b/Services/FileNoteService.cs @@ -2,17 +2,17 @@ { public class FileNoteService : INoteService { - private readonly string filePath; + private readonly string folderPath; - private static readonly string defaultFilePath = "notes"; + private static readonly string defaultFolderPath = "notes"; public FileNoteService(IConfiguration configuration) { - filePath = configuration["FileNoteService:ContentFilePath"]; + folderPath = configuration["FileNoteService:ContentFolder"]; - if (string.IsNullOrWhiteSpace(filePath)) + if (string.IsNullOrWhiteSpace(folderPath)) { - filePath = defaultFilePath; + folderPath = defaultFolderPath; } } @@ -25,7 +25,7 @@ public ICollection GetNoteNames() { - return Directory.GetFiles(filePath) + return Directory.GetFiles(folderPath) .Select(f => Path.GetFileName(f)) .ToList(); } @@ -49,6 +49,8 @@ // ensure initialized if (!File.Exists(filePath)) { + Directory.CreateDirectory(folderPath); + SaveText("Hi! Feel free to start typing. Everything will be saved soon after you are done typing.", noteName); } } @@ -57,7 +59,7 @@ { noteName = noteName.Trim().ToLower(); - return Path.Combine(filePath, noteName); + return Path.Combine(folderPath, noteName); } } } diff --git a/appsettings.json b/appsettings.json index 17bfc41..abd5538 100644 --- a/appsettings.json +++ b/appsettings.json @@ -7,6 +7,6 @@ }, "AllowedHosts": "*", "FileNoteService": { - "ContentFilePath": "notes" + "ContentFolder": "notes" } } From c38e4cd8bf28b4eda8cbed2a42dbff8943c1945c Mon Sep 17 00:00:00 2001 From: Ryan Peters Date: Sat, 6 Jan 2024 21:15:50 -0500 Subject: [PATCH 18/19] fix merge issue with scripts --- Views/Note/Index.cshtml | 3 +++ Views/Shared/_Layout.cshtml | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Views/Note/Index.cshtml b/Views/Note/Index.cshtml index bda3c3f..a1f0bbb 100644 --- a/Views/Note/Index.cshtml +++ b/Views/Note/Index.cshtml @@ -19,4 +19,7 @@ + + + } diff --git a/Views/Shared/_Layout.cshtml b/Views/Shared/_Layout.cshtml index 02823e7..2a4e856 100644 --- a/Views/Shared/_Layout.cshtml +++ b/Views/Shared/_Layout.cshtml @@ -11,10 +11,10 @@ @RenderBody() - @RenderSection("scripts", false) - + @RenderSection("scripts", false) + From 9309b7012e9b40a73a8d8d807479262a3ec3ae1f Mon Sep 17 00:00:00 2001 From: Ryan Peters Date: Mon, 8 Jan 2024 17:17:38 -0500 Subject: [PATCH 19/19] make a notes folder in dockerfile --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index 163814c..cb5813c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,6 +6,7 @@ RUN dotnet publish "BinaryDad.Notes.csproj" -c Release -o /app/publish /p:UseApp FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base WORKDIR /app +RUN mkdir notes COPY --from=build /app/publish . EXPOSE 8080