From f610664230365a0b05b761010846e895594b3ed7 Mon Sep 17 00:00:00 2001 From: Ryan Peters Date: Tue, 7 Feb 2023 12:21:22 -0500 Subject: [PATCH] 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());