feat(data): add WeekReportRepository with tests
This commit is contained in:
38
src/ClaudeDo.Data/Repositories/WeekReportRepository.cs
Normal file
38
src/ClaudeDo.Data/Repositories/WeekReportRepository.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using ClaudeDo.Data.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ClaudeDo.Data.Repositories;
|
||||
|
||||
public sealed class WeekReportRepository
|
||||
{
|
||||
private readonly ClaudeDoDbContext _context;
|
||||
|
||||
public WeekReportRepository(ClaudeDoDbContext context) => _context = context;
|
||||
|
||||
public async Task<WeekReportEntity?> GetByRangeAsync(
|
||||
DateOnly start, DateOnly end, CancellationToken ct = default) =>
|
||||
await _context.WeekReports.AsNoTracking()
|
||||
.FirstOrDefaultAsync(r => r.StartDate == start && r.EndDate == end, ct);
|
||||
|
||||
public async Task UpsertAsync(DateOnly start, DateOnly end, string markdown, CancellationToken ct = default)
|
||||
{
|
||||
var row = await _context.WeekReports
|
||||
.FirstOrDefaultAsync(r => r.StartDate == start && r.EndDate == end, ct);
|
||||
if (row is null)
|
||||
{
|
||||
_context.WeekReports.Add(new WeekReportEntity
|
||||
{
|
||||
StartDate = start,
|
||||
EndDate = end,
|
||||
Markdown = markdown,
|
||||
GeneratedAt = DateTime.UtcNow,
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
row.Markdown = markdown;
|
||||
row.GeneratedAt = DateTime.UtcNow;
|
||||
}
|
||||
await _context.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user