adjusted file service, added get all notes method

This commit is contained in:
Ryan Peters 2023-05-12 12:13:35 -04:00
parent c9f22e0978
commit 77e6ed78fd
3 changed files with 24 additions and 8 deletions

View File

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

View File

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

View File

@ -6,5 +6,6 @@
}
},
"AllowedHosts": "*",
"ContentFilePath": "content"
"DefaultContentFileName": "content",
"ContentFilePath": "notes"
}