Initial
This commit is contained in:
33
src/ClaudeMailbox/Data/Repositories/MailboxRepository.cs
Normal file
33
src/ClaudeMailbox/Data/Repositories/MailboxRepository.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using ClaudeMailbox.Data.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ClaudeMailbox.Data.Repositories;
|
||||
|
||||
public sealed class MailboxRepository
|
||||
{
|
||||
private readonly MailboxDbContext _db;
|
||||
|
||||
public MailboxRepository(MailboxDbContext db) => _db = db;
|
||||
|
||||
public async Task<Mailbox> UpsertAsync(string name, CancellationToken ct = default)
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
var row = await _db.Mailboxes.FirstOrDefaultAsync(m => m.Name == name, ct);
|
||||
if (row is null)
|
||||
{
|
||||
row = new Mailbox { Name = name, CreatedAt = now, LastSeenAt = now };
|
||||
_db.Mailboxes.Add(row);
|
||||
}
|
||||
else
|
||||
{
|
||||
row.LastSeenAt = now;
|
||||
}
|
||||
await _db.SaveChangesAsync(ct);
|
||||
return row;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<Mailbox>> ListAsync(CancellationToken ct = default)
|
||||
{
|
||||
return await _db.Mailboxes.AsNoTracking().OrderBy(m => m.Name).ToListAsync(ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user