adjusted file service, added get all notes method
This commit is contained in:
parent
c9f22e0978
commit
77e6ed78fd
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
{
|
||||
public interface INoteService
|
||||
{
|
||||
ICollection<string> GetNoteNames();
|
||||
string Get(string? noteName);
|
||||
void Save(string content, string? noteName);
|
||||
}
|
||||
|
@ -6,5 +6,6 @@
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"ContentFilePath": "content"
|
||||
"DefaultContentFileName": "content",
|
||||
"ContentFilePath": "notes"
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user