Contactbeheer: berichten, schakelaar en tweelaagse poort voor het formulier
Beheerroutes zonder /admin-segment op contact-messages (lijst met spam apart en ongelezen-teller zonder spam, detail, status, verwijderen, opnieuw versturen met eigen limiet) en contact-settings voor de CMS-schakelaar, allemaal AdminOnly. Het formulier is alleen open als de hostvlag EN de CMS-schakelaar (standaard uit) aan staan; de hostvlag wordt eerst gecontroleerd zodat de database niet wordt aangeraakt. Bij uitgeschakelde host antwoorden de beheerroutes 404. OpenAPI-document bijgewerkt. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -13,6 +13,21 @@ public interface IContactRepository
|
||||
Task SaveChangesAsync(CancellationToken cancellationToken);
|
||||
Task<int> DeleteExpiredMessagesAsync(DateTimeOffset now, CancellationToken cancellationToken);
|
||||
Task<int> DeleteIdempotencyOlderThanAsync(DateTimeOffset cutoff, CancellationToken cancellationToken);
|
||||
|
||||
// ---- management (admin) ----
|
||||
|
||||
/// <summary>One page of messages of the given statuses, newest first, plus the total count of that selection.</summary>
|
||||
Task<(IReadOnlyList<ContactMessage> Items, int TotalCount)> ListAsync(
|
||||
string siteId, IReadOnlyCollection<ContactMessageStatus> statuses, int skip, int take, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>Messages with status New. Spam is never counted.</summary>
|
||||
Task<int> CountUnreadAsync(string siteId, CancellationToken cancellationToken);
|
||||
|
||||
Task<ContactMessage?> GetAsync(string siteId, Guid id, CancellationToken cancellationToken);
|
||||
void Remove(ContactMessage message);
|
||||
|
||||
Task<ContactSettings?> GetSettingsAsync(CancellationToken cancellationToken);
|
||||
void Add(ContactSettings settings);
|
||||
}
|
||||
|
||||
public class ContactRepository(ContactDbContext context) : IContactRepository
|
||||
@@ -33,6 +48,52 @@ public class ContactRepository(ContactDbContext context) : IContactRepository
|
||||
|
||||
public Task SaveChangesAsync(CancellationToken cancellationToken) => context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
public async Task<(IReadOnlyList<ContactMessage> Items, int TotalCount)> ListAsync(
|
||||
string siteId, IReadOnlyCollection<ContactMessageStatus> statuses, int skip, int take, CancellationToken cancellationToken)
|
||||
{
|
||||
// One flag per status instead of 'statuses.Contains(m.Status)': the status is stored as a
|
||||
// string through a value converter, and a parameterized collection loses that mapping in
|
||||
// the SQL translation.
|
||||
var wantNew = statuses.Contains(ContactMessageStatus.New);
|
||||
var wantRead = statuses.Contains(ContactMessageStatus.Read);
|
||||
var wantArchived = statuses.Contains(ContactMessageStatus.Archived);
|
||||
var wantSpam = statuses.Contains(ContactMessageStatus.Spam);
|
||||
var wantConverted = statuses.Contains(ContactMessageStatus.Converted);
|
||||
|
||||
var query = context.ContactMessages
|
||||
.AsNoTracking()
|
||||
.Where(m => m.SiteId == siteId
|
||||
&& ((wantNew && m.Status == ContactMessageStatus.New)
|
||||
|| (wantRead && m.Status == ContactMessageStatus.Read)
|
||||
|| (wantArchived && m.Status == ContactMessageStatus.Archived)
|
||||
|| (wantSpam && m.Status == ContactMessageStatus.Spam)
|
||||
|| (wantConverted && m.Status == ContactMessageStatus.Converted)));
|
||||
|
||||
var total = await query.CountAsync(cancellationToken);
|
||||
var items = await query
|
||||
.OrderByDescending(m => m.ReceivedAt)
|
||||
.ThenBy(m => m.Id)
|
||||
.Skip(skip)
|
||||
.Take(take)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return (items, total);
|
||||
}
|
||||
|
||||
public Task<int> CountUnreadAsync(string siteId, CancellationToken cancellationToken)
|
||||
=> context.ContactMessages.CountAsync(
|
||||
m => m.SiteId == siteId && m.Status == ContactMessageStatus.New, cancellationToken);
|
||||
|
||||
public Task<ContactMessage?> GetAsync(string siteId, Guid id, CancellationToken cancellationToken)
|
||||
=> context.ContactMessages.FirstOrDefaultAsync(m => m.SiteId == siteId && m.Id == id, cancellationToken);
|
||||
|
||||
public void Remove(ContactMessage message) => context.ContactMessages.Remove(message);
|
||||
|
||||
public Task<ContactSettings?> GetSettingsAsync(CancellationToken cancellationToken)
|
||||
=> context.ContactSettings.FirstOrDefaultAsync(s => s.Id == ContactSettings.SingletonId, cancellationToken);
|
||||
|
||||
public void Add(ContactSettings settings) => context.ContactSettings.Add(settings);
|
||||
|
||||
public async Task<int> DeleteExpiredMessagesAsync(DateTimeOffset now, CancellationToken cancellationToken)
|
||||
{
|
||||
var total = 0;
|
||||
|
||||
Reference in New Issue
Block a user