This commit is contained in:
2025-03-18 00:26:18 +01:00
parent 093679c9bb
commit 6b4bffe5ec

View File

@@ -1,49 +1,49 @@
namespace Jugenddienst_Stunden.Models; namespace Jugenddienst_Stunden.Models;
internal class Note { internal class Note {
public string Filename { get; set; } public string Filename { get; set; }
public string Text { get; set; } public string Text { get; set; }
public DateTime Date { get; set; } public DateTime Date { get; set; }
public void Save() => public void Save() =>
File.WriteAllText(System.IO.Path.Combine(FileSystem.AppDataDirectory, Filename), Text); File.WriteAllText(System.IO.Path.Combine(FileSystem.AppDataDirectory, Filename), Text);
public void Delete() => public void Delete() =>
File.Delete(System.IO.Path.Combine(FileSystem.AppDataDirectory, Filename)); File.Delete(System.IO.Path.Combine(FileSystem.AppDataDirectory, Filename));
public static Note Load(string filename) { public static Note Load(string filename) {
filename = System.IO.Path.Combine(FileSystem.AppDataDirectory, filename); filename = System.IO.Path.Combine(FileSystem.AppDataDirectory, filename);
if (!File.Exists(filename)) if (!File.Exists(filename))
throw new FileNotFoundException("Unable to find file on local storage.", filename); throw new FileNotFoundException("Unable to find file on local storage.", filename);
return return
new() { new() {
Filename = Path.GetFileName(filename), Filename = Path.GetFileName(filename),
Text = File.ReadAllText(filename), Text = File.ReadAllText(filename),
Date = File.GetLastWriteTime(filename) Date = File.GetLastWriteTime(filename)
}; };
} }
public static IEnumerable<Note> LoadAll() { public static IEnumerable<Note> LoadAll() {
// Get the folder where the notes are stored. // Get the folder where the notes are stored.
string appDataPath = FileSystem.AppDataDirectory; string appDataPath = FileSystem.AppDataDirectory;
// Use Linq extensions to load the *.notes.txt files. // Use Linq extensions to load the *.notes.txt files.
return Directory return Directory
// Select the file names from the directory // Select the file names from the directory
.EnumerateFiles(appDataPath, "*.notes.txt") .EnumerateFiles(appDataPath, "*.notes.txt")
// Each file name is used to load a note // Each file name is used to load a note
.Select(filename => Note.Load(Path.GetFileName(filename))) .Select(filename => Note.Load(Path.GetFileName(filename)))
// With the final collection of notes, order them by date // With the final collection of notes, order them by date
.OrderByDescending(note => note.Date); .OrderByDescending(note => note.Date);
} }
public Note() { public Note() {
Filename = $"{Path.GetRandomFileName()}.notes.txt"; Filename = $"{Path.GetRandomFileName()}.notes.txt";
Date = DateTime.Now; Date = DateTime.Now;
Text = ""; Text = "";
} }
} }