early work on supporting multiple files

This commit is contained in:
Ryan Peters 2023-02-07 12:21:22 -05:00
parent ed90291b93
commit f610664230
6 changed files with 36 additions and 22 deletions

View File

@ -12,9 +12,10 @@ public class HomeController : Controller
this.noteService = noteService; 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); return View((object)content);
} }

View File

@ -12,9 +12,9 @@ namespace BinaryDad.Notes
this.noteService = noteService; 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); await Clients.Others.SendAsync("updateNote", content);
} }

View File

@ -2,27 +2,38 @@
{ {
public class FileNoteService : INoteService public class FileNoteService : INoteService
{ {
private readonly string? filePath; private readonly string? defaultFileName;
public FileNoteService(IConfiguration configuration) 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 // 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() private string GetFileName(string? noteName) => string.IsNullOrWhiteSpace(noteName) ? defaultFileName : noteName;
{
return File.ReadAllText(filePath);
}
public void Save(string content)
{
File.WriteAllText(filePath, content);
}
} }
} }

View File

@ -2,7 +2,7 @@
{ {
public interface INoteService public interface INoteService
{ {
string Get(); string Get(string? noteName);
void Save(string content); void Save(string content, string? noteName);
} }
} }

View File

@ -6,5 +6,5 @@
} }
}, },
"AllowedHosts": "*", "AllowedHosts": "*",
"ContentFilePath": "content.txt" "ContentFilePath": "content"
} }

View File

@ -1,4 +1,6 @@
let connection = new signalR.HubConnectionBuilder() const noteName = document.location.pathname.substring(1);
let connection = new signalR.HubConnectionBuilder()
.withAutomaticReconnect() .withAutomaticReconnect()
.withUrl("/noteHub") .withUrl("/noteHub")
.build(); .build();
@ -29,7 +31,7 @@ function saveContent($textarea) {
var content = $textarea.val(); var content = $textarea.val();
connection.invoke('SaveNote', content).then(function () { connection.invoke('SaveNote', content, noteName).then(function () {
showToast('#saved-indicator'); showToast('#saved-indicator');
}).catch(function (err) { }).catch(function (err) {
console.error(err.toString()); console.error(err.toString());