2024-05-02 21:44:13 +02:00
|
|
|
using Avalonia.Collections;
|
2024-04-05 12:31:34 +02:00
|
|
|
|
|
|
|
|
using InkForge.Data;
|
|
|
|
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
|
|
namespace InkForge.Desktop.Models;
|
|
|
|
|
|
2024-05-02 21:44:13 +02:00
|
|
|
public class NoteStore(IDbContextFactory<NoteDbContext> dbContextFactory)
|
2024-04-05 12:31:34 +02:00
|
|
|
{
|
2024-05-02 21:44:13 +02:00
|
|
|
public AvaloniaDictionary<int, Note> Notes { get; } = [];
|
2024-04-05 12:31:34 +02:00
|
|
|
|
2024-05-02 21:44:13 +02:00
|
|
|
public async ValueTask Load()
|
2024-04-05 12:31:34 +02:00
|
|
|
{
|
2024-05-02 21:44:13 +02:00
|
|
|
await using var dbContext = await dbContextFactory.CreateDbContextAsync().ConfigureAwait(false);
|
|
|
|
|
Notes.Clear();
|
|
|
|
|
await foreach (var note in dbContext.Notes.AsAsyncEnumerable().ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
Notes.Add(note.Id, Map(note));
|
|
|
|
|
}
|
2024-04-05 12:31:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddNote(Note note)
|
|
|
|
|
{
|
2024-05-02 21:44:13 +02:00
|
|
|
using var dbContext = dbContextFactory.CreateDbContext();
|
|
|
|
|
var entity = Map(note);
|
2024-04-05 12:31:34 +02:00
|
|
|
var entry = dbContext.Notes.Add(entity);
|
2024-05-02 21:44:13 +02:00
|
|
|
|
2024-04-05 12:31:34 +02:00
|
|
|
dbContext.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Note? GetById(int id)
|
|
|
|
|
{
|
2024-05-02 21:44:13 +02:00
|
|
|
if (!Notes.TryGetValue(id, out var note))
|
2024-04-05 12:31:34 +02:00
|
|
|
{
|
2024-05-02 21:44:13 +02:00
|
|
|
using var dbContext = dbContextFactory.CreateDbContext();
|
2024-04-05 12:31:34 +02:00
|
|
|
if (dbContext.Notes.Find(id) is not { } dbNote)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-02 21:44:13 +02:00
|
|
|
Notes.Add(id, note = Map(dbNote));
|
2024-04-05 12:31:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return note;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-02 21:44:13 +02:00
|
|
|
private NoteEntity Map(Note note)
|
2024-04-05 12:31:34 +02:00
|
|
|
{
|
|
|
|
|
return new()
|
|
|
|
|
{
|
|
|
|
|
Id = note.Id,
|
|
|
|
|
Value = new()
|
|
|
|
|
{
|
|
|
|
|
Name = note.Name,
|
|
|
|
|
Created = note.CreatedTime,
|
|
|
|
|
Updated = note.UpdatedTime,
|
|
|
|
|
},
|
2024-05-02 21:44:13 +02:00
|
|
|
Parent = note.ParentId switch
|
2024-04-05 12:31:34 +02:00
|
|
|
{
|
2024-05-02 21:44:13 +02:00
|
|
|
{ } parentId => new()
|
2024-04-05 12:31:34 +02:00
|
|
|
{
|
|
|
|
|
Id = parentId
|
|
|
|
|
},
|
2024-05-02 21:44:13 +02:00
|
|
|
_ => null
|
|
|
|
|
},
|
2024-04-05 12:31:34 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-02 21:44:13 +02:00
|
|
|
private Note Map(NoteEntity entity)
|
2024-04-05 12:31:34 +02:00
|
|
|
{
|
2024-05-02 21:44:13 +02:00
|
|
|
return new()
|
|
|
|
|
{
|
|
|
|
|
Id = entity.Id,
|
|
|
|
|
Name = entity.Value.Name,
|
|
|
|
|
CreatedTime = entity.Value.Created,
|
|
|
|
|
UpdatedTime = entity.Value.Updated,
|
|
|
|
|
ParentId = entity.Parent?.Id
|
|
|
|
|
};
|
2024-04-05 12:31:34 +02:00
|
|
|
}
|
|
|
|
|
}
|