| | | 1 | | using LifeChart.Domain.Entries; |
| | | 2 | | using LifeChart.Infrastructure.Persistence; |
| | | 3 | | using Microsoft.EntityFrameworkCore; |
| | | 4 | | |
| | | 5 | | namespace LifeChart.Infrastructure.Repositories; |
| | | 6 | | |
| | | 7 | | public class DailyEntryRepository : IDailyEntryRepository |
| | | 8 | | { |
| | | 9 | | private readonly LifeChartDbContext _context; |
| | | 10 | | |
| | 0 | 11 | | public DailyEntryRepository(LifeChartDbContext context) |
| | 0 | 12 | | => _context = context; |
| | | 13 | | |
| | | 14 | | public async Task<DailyEntry?> GetByDateAsync(DateOnly date) |
| | 0 | 15 | | => await _context.DailyEntries.FirstOrDefaultAsync(e => e.Date == date); |
| | | 16 | | |
| | | 17 | | public async Task<IEnumerable<DailyEntry>> GetRangeAsync(DateOnly from, DateOnly to) |
| | 0 | 18 | | => await _context.DailyEntries |
| | 0 | 19 | | .Where(e => e.Date >= from && e.Date <= to) |
| | 0 | 20 | | .OrderBy(e => e.Date) |
| | 0 | 21 | | .ToListAsync(); |
| | | 22 | | |
| | | 23 | | public async Task<bool> ExistsForTodayAsync() |
| | 0 | 24 | | => await _context.DailyEntries |
| | 0 | 25 | | .AnyAsync(e => e.Date == DateOnly.FromDateTime(DateTime.Today)); |
| | | 26 | | |
| | | 27 | | public async Task SaveAsync(DailyEntry entry) |
| | | 28 | | { |
| | 0 | 29 | | var existing = await GetByDateAsync(entry.Date); |
| | 0 | 30 | | if (existing is null) |
| | 0 | 31 | | _context.DailyEntries.Add(entry); |
| | | 32 | | else |
| | 0 | 33 | | _context.Entry(existing).CurrentValues.SetValues(entry); |
| | | 34 | | |
| | 0 | 35 | | await _context.SaveChangesAsync(); |
| | 0 | 36 | | } |
| | | 37 | | } |