Details ausgearbeitet
Linked colors to options menu Options extension Aussehen aufgebohrt: - erlaubt text von status zu ändern, - bessere beschriftung von Farben desing änderung und bedienung - heute status felder anklickbar, - Farbpalette verdunkelt und kontrast reduziert - Kalender Tage anklickbar gemacht - Optionen überarbeitet
This commit is contained in:
@@ -0,0 +1,188 @@
|
||||
using cfs_tracker.Models;
|
||||
|
||||
namespace cfs_tracker.Data;
|
||||
|
||||
public class SymptomRepository
|
||||
{
|
||||
private readonly AppDatabase _database;
|
||||
|
||||
public SymptomRepository(AppDatabase database)
|
||||
{
|
||||
_database = database;
|
||||
}
|
||||
|
||||
public async Task<List<SymptomItem>> GetAllAsync()
|
||||
{
|
||||
await _database.InitAsync();
|
||||
|
||||
return await _database.Connection.Table<SymptomItem>()
|
||||
.OrderBy(x => x.SortOrder)
|
||||
.ThenBy(x => x.Name)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<List<SymptomItem>> GetActiveAsync()
|
||||
{
|
||||
await _database.InitAsync();
|
||||
|
||||
return await _database.Connection.Table<SymptomItem>()
|
||||
.Where(x => x.IsActive)
|
||||
.OrderBy(x => x.SortOrder)
|
||||
.ThenBy(x => x.Name)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task EnsureDefaultsAsync()
|
||||
{
|
||||
await _database.InitAsync();
|
||||
|
||||
if ((await _database.Connection.Table<SymptomItem>().CountAsync()) > 0)
|
||||
return;
|
||||
|
||||
var defaults = new List<SymptomItem>
|
||||
{
|
||||
new() { Name = "Kopfschmerzen", SortOrder = 0, IsActive = true },
|
||||
new() { Name = "Kopfnebel", SortOrder = 1, IsActive = true },
|
||||
new() { Name = "Tinnitus", SortOrder = 2, IsActive = true },
|
||||
new() { Name = "Schwere / Müdigkeit", SortOrder = 3, IsActive = true },
|
||||
new() { Name = "Überforderung", SortOrder = 4, IsActive = true }
|
||||
};
|
||||
|
||||
foreach (var item in defaults)
|
||||
await _database.Connection.InsertAsync(item);
|
||||
}
|
||||
|
||||
public async Task AddAsync(string name)
|
||||
{
|
||||
await _database.InitAsync();
|
||||
|
||||
var maxSort = 0;
|
||||
var all = await _database.Connection.Table<SymptomItem>().ToListAsync();
|
||||
if (all.Count > 0)
|
||||
maxSort = all.Max(x => x.SortOrder) + 1;
|
||||
|
||||
await _database.Connection.InsertAsync(new SymptomItem
|
||||
{
|
||||
Name = name.Trim(),
|
||||
SortOrder = maxSort,
|
||||
IsActive = true
|
||||
});
|
||||
}
|
||||
|
||||
public async Task RenameAsync(int id, string newName)
|
||||
{
|
||||
await _database.InitAsync();
|
||||
|
||||
var item = await _database.Connection.Table<SymptomItem>()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (item is null)
|
||||
return;
|
||||
|
||||
item.Name = newName.Trim();
|
||||
await _database.Connection.UpdateAsync(item);
|
||||
}
|
||||
|
||||
public async Task SetActiveAsync(int id, bool isActive)
|
||||
{
|
||||
await _database.InitAsync();
|
||||
|
||||
var item = await _database.Connection.Table<SymptomItem>()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (item is null)
|
||||
return;
|
||||
|
||||
item.IsActive = isActive;
|
||||
await _database.Connection.UpdateAsync(item);
|
||||
}
|
||||
|
||||
public async Task<bool> HasUsageAsync(int symptomId)
|
||||
{
|
||||
await _database.InitAsync();
|
||||
|
||||
var count = await _database.Connection.Table<HealthEntrySymptom>()
|
||||
.Where(x => x.SymptomItemId == symptomId)
|
||||
.CountAsync();
|
||||
|
||||
return count > 0;
|
||||
}
|
||||
|
||||
public async Task DeleteIfUnusedAsync(int symptomId)
|
||||
{
|
||||
await _database.InitAsync();
|
||||
|
||||
if (await HasUsageAsync(symptomId))
|
||||
return;
|
||||
|
||||
var item = await _database.Connection.Table<SymptomItem>()
|
||||
.FirstOrDefaultAsync(x => x.Id == symptomId);
|
||||
|
||||
if (item is not null)
|
||||
await _database.Connection.DeleteAsync(item);
|
||||
}
|
||||
|
||||
public async Task<List<int>> GetSymptomIdsForEntryAsync(int entryId)
|
||||
{
|
||||
await _database.InitAsync();
|
||||
|
||||
var links = await _database.Connection.Table<HealthEntrySymptom>()
|
||||
.Where(x => x.HealthEntryId == entryId)
|
||||
.ToListAsync();
|
||||
|
||||
return links.Select(x => x.SymptomItemId).ToList();
|
||||
}
|
||||
|
||||
public async Task SetSymptomsForEntryAsync(int entryId, IEnumerable<int> symptomIds)
|
||||
{
|
||||
await _database.InitAsync();
|
||||
|
||||
var existing = await _database.Connection.Table<HealthEntrySymptom>()
|
||||
.Where(x => x.HealthEntryId == entryId)
|
||||
.ToListAsync();
|
||||
|
||||
foreach (var item in existing)
|
||||
await _database.Connection.DeleteAsync(item);
|
||||
|
||||
foreach (var symptomId in symptomIds.Distinct())
|
||||
{
|
||||
await _database.Connection.InsertAsync(new HealthEntrySymptom
|
||||
{
|
||||
HealthEntryId = entryId,
|
||||
SymptomItemId = symptomId
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Dictionary<int, List<string>>> GetSymptomsByEntryIdsAsync(IEnumerable<int> entryIds)
|
||||
{
|
||||
await _database.InitAsync();
|
||||
|
||||
var ids = entryIds.Distinct().ToHashSet();
|
||||
var result = new Dictionary<int, List<string>>();
|
||||
|
||||
if (ids.Count == 0)
|
||||
return result;
|
||||
|
||||
var allLinks = await _database.Connection.Table<HealthEntrySymptom>().ToListAsync();
|
||||
var relevantLinks = allLinks.Where(x => ids.Contains(x.HealthEntryId)).ToList();
|
||||
|
||||
if (relevantLinks.Count == 0)
|
||||
return result;
|
||||
|
||||
var allSymptoms = await _database.Connection.Table<SymptomItem>().ToListAsync();
|
||||
var symptomMap = allSymptoms.ToDictionary(x => x.Id, x => x.Name);
|
||||
|
||||
foreach (var group in relevantLinks.GroupBy(x => x.HealthEntryId))
|
||||
{
|
||||
result[group.Key] = group
|
||||
.Where(x => symptomMap.ContainsKey(x.SymptomItemId))
|
||||
.Select(x => symptomMap[x.SymptomItemId])
|
||||
.Distinct()
|
||||
.OrderBy(x => x)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user