Spaces:
Sleeping
Sleeping
Commit ·
03ee889
1
Parent(s): 0e9dbdf
feat(habits): support custom checked date in backend DTO, repository, and service
Browse files
FlowAPI.Application/DTOs/Habit/HabitDtos.cs
CHANGED
|
@@ -4,6 +4,7 @@ namespace FlowAPI.Application.DTOs.Habit
|
|
| 4 |
{
|
| 5 |
public Guid UserId { get; set; }
|
| 6 |
public string HabitName { get; set; } = string.Empty;
|
|
|
|
| 7 |
}
|
| 8 |
|
| 9 |
public class HabitRecordResponseDto
|
|
|
|
| 4 |
{
|
| 5 |
public Guid UserId { get; set; }
|
| 6 |
public string HabitName { get; set; } = string.Empty;
|
| 7 |
+
public DateTime? CheckedDate { get; set; }
|
| 8 |
}
|
| 9 |
|
| 10 |
public class HabitRecordResponseDto
|
FlowAPI.Application/Interfaces/IHabitRecordRepository.cs
CHANGED
|
@@ -6,5 +6,6 @@ namespace FlowAPI.Application.Interfaces
|
|
| 6 |
{
|
| 7 |
Task<IEnumerable<HabitRecord>> GetAllByUserIdAsync(Guid userId);
|
| 8 |
Task<HabitRecord?> GetTodayRecordAsync(Guid userId, string habitName);
|
|
|
|
| 9 |
}
|
| 10 |
}
|
|
|
|
| 6 |
{
|
| 7 |
Task<IEnumerable<HabitRecord>> GetAllByUserIdAsync(Guid userId);
|
| 8 |
Task<HabitRecord?> GetTodayRecordAsync(Guid userId, string habitName);
|
| 9 |
+
Task<HabitRecord?> GetRecordForDateAsync(Guid userId, string habitName, DateTime date);
|
| 10 |
}
|
| 11 |
}
|
FlowAPI.Application/Services/HabitService.cs
CHANGED
|
@@ -23,7 +23,8 @@ namespace FlowAPI.Application.Services
|
|
| 23 |
|
| 24 |
public async Task<HabitRecordResponseDto> RecordHabitAsync(CreateHabitRecordDto dto)
|
| 25 |
{
|
| 26 |
-
var
|
|
|
|
| 27 |
if (existing is not null) return MapToDto(existing);
|
| 28 |
|
| 29 |
var record = new HabitRecord
|
|
@@ -31,7 +32,7 @@ namespace FlowAPI.Application.Services
|
|
| 31 |
Id = Guid.NewGuid(),
|
| 32 |
UserId = dto.UserId,
|
| 33 |
HabitName = dto.HabitName,
|
| 34 |
-
CheckedDate =
|
| 35 |
};
|
| 36 |
|
| 37 |
var created = await _repo.CreateAsync(record);
|
|
|
|
| 23 |
|
| 24 |
public async Task<HabitRecordResponseDto> RecordHabitAsync(CreateHabitRecordDto dto)
|
| 25 |
{
|
| 26 |
+
var targetDate = dto.CheckedDate ?? DateTime.UtcNow;
|
| 27 |
+
var existing = await _repo.GetRecordForDateAsync(dto.UserId, dto.HabitName, targetDate.Date);
|
| 28 |
if (existing is not null) return MapToDto(existing);
|
| 29 |
|
| 30 |
var record = new HabitRecord
|
|
|
|
| 32 |
Id = Guid.NewGuid(),
|
| 33 |
UserId = dto.UserId,
|
| 34 |
HabitName = dto.HabitName,
|
| 35 |
+
CheckedDate = targetDate
|
| 36 |
};
|
| 37 |
|
| 38 |
var created = await _repo.CreateAsync(record);
|
FlowAPI.Infrastructure/Repositories/HabitRecordRepository.cs
CHANGED
|
@@ -27,5 +27,15 @@ namespace FlowAPI.Infrastructure.Repositories
|
|
| 27 |
r.HabitName == habitName &&
|
| 28 |
r.CheckedDate.Date == today);
|
| 29 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
}
|
| 31 |
}
|
|
|
|
| 27 |
r.HabitName == habitName &&
|
| 28 |
r.CheckedDate.Date == today);
|
| 29 |
}
|
| 30 |
+
|
| 31 |
+
public async Task<HabitRecord?> GetRecordForDateAsync(Guid userId, string habitName, DateTime date)
|
| 32 |
+
{
|
| 33 |
+
var targetDate = date.Date;
|
| 34 |
+
return await _dbSet
|
| 35 |
+
.FirstOrDefaultAsync(r =>
|
| 36 |
+
r.UserId == userId &&
|
| 37 |
+
r.HabitName == habitName &&
|
| 38 |
+
r.CheckedDate.Date == targetDate);
|
| 39 |
+
}
|
| 40 |
}
|
| 41 |
}
|