2023-01-05 15:32:21 +00:00
|
|
|
|
namespace BinaryDad.Notes.Services
|
|
|
|
|
{
|
|
|
|
|
public class FileNoteService : INoteService
|
|
|
|
|
{
|
2023-02-07 17:21:22 +00:00
|
|
|
|
private readonly string? defaultFileName;
|
2023-01-05 15:32:21 +00:00
|
|
|
|
|
|
|
|
|
public FileNoteService(IConfiguration configuration)
|
|
|
|
|
{
|
2023-02-07 17:21:22 +00:00
|
|
|
|
defaultFileName = configuration["ContentFilePath"];
|
2023-01-05 15:32:21 +00:00
|
|
|
|
|
2023-02-07 17:21:22 +00:00
|
|
|
|
CheckFile(defaultFileName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Get(string? noteName)
|
|
|
|
|
{
|
|
|
|
|
CheckFile(noteName);
|
|
|
|
|
|
|
|
|
|
return File.ReadAllText(GetFileName(noteName));
|
2023-01-05 15:32:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-07 17:21:22 +00:00
|
|
|
|
public void Save(string content, string? noteName)
|
2023-01-05 15:32:21 +00:00
|
|
|
|
{
|
2023-02-07 17:21:22 +00:00
|
|
|
|
File.WriteAllText(GetFileName(noteName), content);
|
2023-01-05 15:32:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-07 17:21:22 +00:00
|
|
|
|
private void CheckFile(string noteName)
|
2023-01-05 15:32:21 +00:00
|
|
|
|
{
|
2023-02-07 17:21:22 +00:00
|
|
|
|
var fileName = GetFileName(noteName);
|
|
|
|
|
|
|
|
|
|
// ensure initialized
|
|
|
|
|
if (!File.Exists(fileName))
|
|
|
|
|
{
|
|
|
|
|
Save("Hi! Feel free to start typing. Everything will be saved soon after you are done typing.", fileName);
|
|
|
|
|
}
|
2023-01-05 15:32:21 +00:00
|
|
|
|
}
|
2023-02-07 17:21:22 +00:00
|
|
|
|
|
|
|
|
|
private string GetFileName(string? noteName) => string.IsNullOrWhiteSpace(noteName) ? defaultFileName : noteName;
|
2023-01-05 15:32:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|