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" }