44 lines
1.5 KiB
C#
44 lines
1.5 KiB
C#
namespace TaxBaik.Application.Services;
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using TaxBaik.Domain.Entities;
|
|
|
|
public record AdminDashboardSummary(
|
|
int ThisMonthInquiries,
|
|
int NewInquiries,
|
|
int TotalPosts,
|
|
int PublishedPosts,
|
|
IReadOnlyList<Inquiry> RecentInquiries);
|
|
|
|
public class AdminDashboardService(
|
|
InquiryService inquiryService,
|
|
BlogService blogService,
|
|
IMemoryCache memoryCache)
|
|
{
|
|
private static readonly TimeSpan CacheDuration = TimeSpan.FromSeconds(30);
|
|
public const string CacheKey = "admin-dashboard-summary";
|
|
|
|
public async Task<AdminDashboardSummary> GetSummaryAsync(CancellationToken ct = default)
|
|
{
|
|
if (memoryCache.TryGetValue(CacheKey, out AdminDashboardSummary? cached) && cached != null)
|
|
return cached;
|
|
|
|
var recentTask = inquiryService.GetPagedAsync(1, 5, ct: ct);
|
|
var thisMonthTask = inquiryService.CountThisMonthAsync(ct);
|
|
var newTask = inquiryService.CountByStatusAsync("new", ct);
|
|
var statsTask = blogService.GetStatsAsync(ct);
|
|
|
|
var (recentInquiries, _) = await recentTask;
|
|
var stats = await statsTask;
|
|
var summary = new AdminDashboardSummary(
|
|
ThisMonthInquiries: await thisMonthTask,
|
|
NewInquiries: await newTask,
|
|
TotalPosts: stats.TotalPosts,
|
|
PublishedPosts: stats.PublishedPosts,
|
|
RecentInquiries: recentInquiries.OrderByDescending(x => x.CreatedAt).Take(5).ToList());
|
|
|
|
memoryCache.Set(CacheKey, summary, CacheDuration);
|
|
return summary;
|
|
}
|
|
}
|