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;
}
public IActionResult Index()
[Route("{noteName?}")]
public IActionResult Index(string? noteName)
{
var content = noteService.Get();
var content = noteService.Get(noteName);
return View((object)content);
}

View File

@ -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);
}

View File

@ -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;
}
}

View File

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

View File

@ -6,5 +6,5 @@
}
},
"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()
.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());