Farbpalette vorladen lassen
Bootstraper läd jetzt die farbeinstellung sofort und nicht nachträglich cleanup and rework PDF switch to HTML export fix Colorsystem Farben für knöpfe hinzugefügt als anpassung button color button color primary and secondary change
This commit is contained in:
+112
-39
@@ -5,101 +5,171 @@ namespace cfs_tracker.Data;
|
||||
public class HealthEntryRepository
|
||||
{
|
||||
private readonly AppDatabase _database;
|
||||
private readonly SymptomRepository _symptomRepository;
|
||||
|
||||
public HealthEntryRepository(AppDatabase database) => _database = database;
|
||||
|
||||
public async Task AddEntryAsync(HealthState state, EntrySource source, DateTime? timestamp = null, int? activityId = null)
|
||||
public HealthEntryRepository(AppDatabase database, SymptomRepository symptomRepository)
|
||||
{
|
||||
await _database.InitAsync();
|
||||
var entry = new HealthEntry
|
||||
{
|
||||
Timestamp = timestamp ?? DateTime.Now,
|
||||
State = state,
|
||||
Source = source,
|
||||
ActivityId = activityId,
|
||||
UpdatedAt = DateTime.Now
|
||||
};
|
||||
await _database.Connection.InsertAsync(entry);
|
||||
_database = database;
|
||||
_symptomRepository = symptomRepository;
|
||||
}
|
||||
|
||||
public async Task<int> AddEntryAndReturnIdAsync(HealthState state, EntrySource source, DateTime? timestamp = null, int? activityId = null)
|
||||
public async Task<int> AddQuickEntryAsync(
|
||||
HealthState state,
|
||||
int? activityId = null,
|
||||
IEnumerable<int>? symptomIds = null,
|
||||
DateTime? timestamp = null,
|
||||
EntrySource source = EntrySource.App)
|
||||
{
|
||||
await _database.InitAsync();
|
||||
|
||||
var effectiveTimestamp = timestamp ?? DateTime.Now;
|
||||
var entry = new HealthEntry
|
||||
{
|
||||
Timestamp = timestamp ?? DateTime.Now,
|
||||
EntryDate = effectiveTimestamp.Date,
|
||||
Timestamp = effectiveTimestamp,
|
||||
State = state,
|
||||
Source = source,
|
||||
ActivityId = activityId,
|
||||
DayPart = null,
|
||||
ActivityId = NormalizeActivityId(activityId),
|
||||
CreatedAt = DateTime.Now,
|
||||
UpdatedAt = DateTime.Now
|
||||
};
|
||||
|
||||
await _database.Connection.InsertAsync(entry);
|
||||
await _symptomRepository.SetSymptomsForEntryAsync(entry.Id, symptomIds);
|
||||
return entry.Id;
|
||||
}
|
||||
|
||||
public async Task UpsertBackfillAsync(DateTime date, DayPart part, HealthState state, EntrySource source, int? activityId = null)
|
||||
public async Task<int> UpsertBackfillAsync(
|
||||
DateTime date,
|
||||
DayPart part = DayPart.Morning,
|
||||
HealthState state = HealthState.Gray,
|
||||
int? activityId = null,
|
||||
IEnumerable<int>? symptomIds = null,
|
||||
EntrySource source = EntrySource.Backfill)
|
||||
{
|
||||
await _database.InitAsync();
|
||||
var slotTimestamp = BuildSlotTimestamp(date, part);
|
||||
|
||||
var slotTimestamp = BuildBackfillTimestamp(date, part);
|
||||
var existing = await _database.Connection.Table<HealthEntry>()
|
||||
.FirstOrDefaultAsync(x => x.Timestamp == slotTimestamp);
|
||||
|
||||
if (existing is null)
|
||||
{
|
||||
await _database.Connection.InsertAsync(new HealthEntry
|
||||
var entry = new HealthEntry
|
||||
{
|
||||
EntryDate = date.Date,
|
||||
Timestamp = slotTimestamp,
|
||||
State = state,
|
||||
Source = source,
|
||||
ActivityId = activityId,
|
||||
DayPart = part,
|
||||
ActivityId = NormalizeActivityId(activityId),
|
||||
CreatedAt = DateTime.Now,
|
||||
UpdatedAt = DateTime.Now
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
existing.State = state;
|
||||
existing.Source = source;
|
||||
existing.ActivityId = activityId;
|
||||
existing.UpdatedAt = DateTime.Now;
|
||||
await _database.Connection.UpdateAsync(existing);
|
||||
};
|
||||
|
||||
await _database.Connection.InsertAsync(entry);
|
||||
await _symptomRepository.SetSymptomsForEntryAsync(entry.Id, symptomIds);
|
||||
return entry.Id;
|
||||
}
|
||||
|
||||
existing.EntryDate = date.Date;
|
||||
existing.Timestamp = slotTimestamp;
|
||||
existing.State = state;
|
||||
existing.Source = source;
|
||||
existing.DayPart = part;
|
||||
existing.ActivityId = NormalizeActivityId(activityId);
|
||||
existing.UpdatedAt = DateTime.Now;
|
||||
|
||||
await _database.Connection.UpdateAsync(existing);
|
||||
await _symptomRepository.SetSymptomsForEntryAsync(existing.Id, symptomIds);
|
||||
return existing.Id;
|
||||
}
|
||||
|
||||
public async Task<HealthEntry?> GetBackfillEntryAsync(DateTime date, DayPart part)
|
||||
public async Task UpdateEntryAsync(
|
||||
int entryId,
|
||||
DateTime date,
|
||||
HealthState state,
|
||||
int? activityId = null,
|
||||
IEnumerable<int>? symptomIds = null,
|
||||
DayPart? part = null,
|
||||
EntrySource? source = null)
|
||||
{
|
||||
await _database.InitAsync();
|
||||
var slotTimestamp = BuildSlotTimestamp(date, part);
|
||||
|
||||
var entry = await _database.Connection.Table<HealthEntry>()
|
||||
.FirstOrDefaultAsync(x => x.Id == entryId);
|
||||
|
||||
if (entry is null)
|
||||
return;
|
||||
|
||||
entry.EntryDate = date.Date;
|
||||
entry.State = state;
|
||||
entry.ActivityId = NormalizeActivityId(activityId);
|
||||
entry.Source = source ?? entry.Source;
|
||||
entry.DayPart = part;
|
||||
|
||||
if (part.HasValue)
|
||||
entry.Timestamp = BuildBackfillTimestamp(date, part.Value);
|
||||
else if (date.Date != entry.EntryDate.Date)
|
||||
entry.Timestamp = date.Date + entry.Timestamp.TimeOfDay;
|
||||
|
||||
entry.UpdatedAt = DateTime.Now;
|
||||
|
||||
await _database.Connection.UpdateAsync(entry);
|
||||
await _symptomRepository.SetSymptomsForEntryAsync(entry.Id, symptomIds);
|
||||
}
|
||||
|
||||
public async Task<HealthEntry?> GetByIdAsync(int id)
|
||||
{
|
||||
await _database.InitAsync();
|
||||
|
||||
return await _database.Connection.Table<HealthEntry>()
|
||||
.FirstOrDefaultAsync(x => x.Timestamp == slotTimestamp);
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
}
|
||||
|
||||
public async Task<List<HealthEntry>> GetEntriesForDateAsync(DateTime date)
|
||||
{
|
||||
await _database.InitAsync();
|
||||
|
||||
var start = date.Date;
|
||||
var end = start.AddDays(1);
|
||||
|
||||
var list = await _database.Connection.Table<HealthEntry>()
|
||||
.Where(x => x.Timestamp >= start && x.Timestamp < end)
|
||||
.ToListAsync();
|
||||
return list.OrderBy(x => x.Timestamp).ToList();
|
||||
|
||||
return list.OrderBy(x => x.Timestamp).ThenBy(x => x.Id).ToList();
|
||||
}
|
||||
|
||||
public async Task<List<HealthEntry>> GetEntriesBetweenAsync(DateTime startDate, DateTime endDate)
|
||||
public async Task<List<HealthEntry>> GetEntriesBetweenAsync(DateTime startDateInclusive, DateTime endDateExclusive)
|
||||
{
|
||||
await _database.InitAsync();
|
||||
var start = startDate.Date;
|
||||
var end = endDate.Date;
|
||||
|
||||
var start = startDateInclusive.Date;
|
||||
var end = endDateExclusive.Date;
|
||||
|
||||
var list = await _database.Connection.Table<HealthEntry>()
|
||||
.Where(x => x.Timestamp >= start && x.Timestamp < end)
|
||||
.ToListAsync();
|
||||
return list.OrderBy(x => x.Timestamp).ToList();
|
||||
|
||||
return list.OrderBy(x => x.Timestamp).ThenBy(x => x.Id).ToList();
|
||||
}
|
||||
|
||||
public async Task DeleteEntryAsync(int id)
|
||||
{
|
||||
await _database.InitAsync();
|
||||
var item = await _database.Connection.Table<HealthEntry>().FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
var links = await _database.Connection.Table<HealthEntrySymptom>()
|
||||
.Where(x => x.HealthEntryId == id)
|
||||
.ToListAsync();
|
||||
|
||||
foreach (var link in links)
|
||||
await _database.Connection.DeleteAsync(link);
|
||||
|
||||
var item = await _database.Connection.Table<HealthEntry>()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (item is not null)
|
||||
await _database.Connection.DeleteAsync(item);
|
||||
}
|
||||
@@ -111,10 +181,13 @@ public class HealthEntryRepository
|
||||
await _database.Connection.DeleteAllAsync<HealthEntrySymptom>();
|
||||
}
|
||||
|
||||
private static DateTime BuildSlotTimestamp(DateTime date, DayPart part) => part switch
|
||||
public static DateTime BuildBackfillTimestamp(DateTime date, DayPart part) => part switch
|
||||
{
|
||||
DayPart.Morning => date.Date.AddHours(9),
|
||||
DayPart.Afternoon => date.Date.AddHours(15),
|
||||
_ => date.Date.AddHours(9)
|
||||
};
|
||||
|
||||
private static int? NormalizeActivityId(int? activityId) =>
|
||||
activityId.HasValue && activityId.Value > 0 ? activityId : null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user