early work on supporting multiple files
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user