File size: 1,444 Bytes
b9c7f0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03ee889
 
 
 
 
 
 
 
 
 
b9c7f0e
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using FlowAPI.Application.Interfaces;
using FlowAPI.Domain.Entities;
using FlowAPI.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;

namespace FlowAPI.Infrastructure.Repositories
{
    public class HabitRecordRepository : GenericRepository<HabitRecord>, IHabitRecordRepository
    {
        public HabitRecordRepository(AppDbContext context) : base(context) { }

        public async Task<IEnumerable<HabitRecord>> GetAllByUserIdAsync(Guid userId)
        {
            return await _dbSet
                .Where(r => r.UserId == userId)
                .OrderByDescending(r => r.CheckedDate)
                .AsNoTracking()
                .ToListAsync();
        }

        public async Task<HabitRecord?> GetTodayRecordAsync(Guid userId, string habitName)
        {
            var today = DateTime.UtcNow.Date;
            return await _dbSet
                .FirstOrDefaultAsync(r =>
                    r.UserId == userId &&
                    r.HabitName == habitName &&
                    r.CheckedDate.Date == today);
        }

        public async Task<HabitRecord?> GetRecordForDateAsync(Guid userId, string habitName, DateTime date)
        {
            var targetDate = date.Date;
            return await _dbSet
                .FirstOrDefaultAsync(r =>
                    r.UserId == userId &&
                    r.HabitName == habitName &&
                    r.CheckedDate.Date == targetDate);
        }
    }
}