early work on supporting multiple files
This commit is contained in:
parent
ed90291b93
commit
f610664230
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
{
|
||||
public interface INoteService
|
||||
{
|
||||
string Get();
|
||||
void Save(string content);
|
||||
string Get(string? noteName);
|
||||
void Save(string content, string? noteName);
|
||||
}
|
||||
}
|
||||
|
@ -6,5 +6,5 @@
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"ContentFilePath": "content.txt"
|
||||
"ContentFilePath": "content"
|
||||
}
|
||||
|
@ -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());
|
||||
|
Loading…
Reference in New Issue
Block a user