You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.6 KiB

1 year ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NotesNet8.Models
{
internal class Note
{
public string Filename { get; set; }
public string Text { get; set; }
public DateTime Date { get; set; }
1 year ago
public Note()
{
Filename = $"{Path.GetRandomFileName()}.notes.txt";
Date = DateTime.Now;
Text = "";
}
public static Note Load(string filename)
{
filename = Path.Combine(FileSystem.AppDataDirectory, filename);
if (!File.Exists(filename))
throw new FileNotFoundException("Unable to find file on local storage.", filename);
return new Note()
{
Filename = Path.GetFileName(filename),
Text = File.ReadAllText(filename),
Date = File.GetLastWriteTime(filename)
};
}
public static IEnumerable<Note> LoadAll()
{
string appDataPath = FileSystem.AppDataDirectory;
return Directory
.EnumerateFiles(appDataPath, "*.notes.txt")
.Select(filename => Note.Load(Path.GetFileName(filename)))
.OrderByDescending(note => note.Date);
}
public void Save()
{
File.WriteAllText(Path.Combine(FileSystem.AppDataDirectory, Filename), Text);
}
public void Delete()
{
File.Delete(Path.Combine(FileSystem.AppDataDirectory, Filename));
}
1 year ago
}
}