d11f0e46c0
clean up and fixes - Heute gefixt - Code Cleanup NotificationScheduleItemViewModel -fixed NotificationScheduleItemViewModel for Windows compat push implementation Push fixes Push benachrichtungen eingebaut - Status bei push - Push auch in background - Toggle für symtome angepasst push system change exporter update Optionen fix - Optionen aufgeteilt auf 3 tabs - Anpassungen haben jetzt Farbslider Optimierung fix
229 lines
6.8 KiB
C#
229 lines
6.8 KiB
C#
using cfs_tracker.Models;
|
|
|
|
namespace cfs_tracker.Data;
|
|
|
|
public class SymptomRepository
|
|
{
|
|
private readonly AppDatabase _database;
|
|
|
|
public SymptomRepository(AppDatabase database)
|
|
{
|
|
_database = database;
|
|
}
|
|
|
|
public async Task EnsureDefaultsAsync()
|
|
{
|
|
await _database.InitAsync();
|
|
|
|
if (await _database.Connection.Table<SymptomItem>().CountAsync() > 0)
|
|
return;
|
|
|
|
var defaults = new[]
|
|
{
|
|
"Fatigue",
|
|
"PEM",
|
|
"Kopfschmerzen",
|
|
"Schwindel",
|
|
"Kognitive Probleme",
|
|
"Schlafprobleme",
|
|
"Reizempfindlichkeit",
|
|
"Herzrasen",
|
|
"Schmerzen",
|
|
"Atemprobleme"
|
|
};
|
|
|
|
for (var i = 0; i < defaults.Length; i++)
|
|
{
|
|
await _database.Connection.InsertAsync(new SymptomItem
|
|
{
|
|
Name = defaults[i],
|
|
SortOrder = i,
|
|
IsActive = true
|
|
});
|
|
}
|
|
}
|
|
|
|
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<SymptomItem?> GetByIdAsync(int id)
|
|
{
|
|
await _database.InitAsync();
|
|
return await _database.Connection.Table<SymptomItem>()
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
}
|
|
|
|
public async Task<Dictionary<int, string>> GetNameMapAsync()
|
|
{
|
|
var items = await GetAllAsync();
|
|
return items.ToDictionary(x => x.Id, x => x.Name);
|
|
}
|
|
|
|
public async Task<int> AddAsync(string name)
|
|
{
|
|
await _database.InitAsync();
|
|
var cleanName = NormalizeName(name);
|
|
|
|
var existing = await _database.Connection.Table<SymptomItem>().ToListAsync();
|
|
if (existing.Any(x => string.Equals(x.Name, cleanName, StringComparison.OrdinalIgnoreCase)))
|
|
throw new InvalidOperationException($"Das Symptom '{cleanName}' existiert bereits.");
|
|
|
|
var nextSort = existing.Count == 0 ? 0 : existing.Max(x => x.SortOrder) + 1;
|
|
var item = new SymptomItem
|
|
{
|
|
Name = cleanName,
|
|
SortOrder = nextSort,
|
|
IsActive = true
|
|
};
|
|
|
|
await _database.Connection.InsertAsync(item);
|
|
return item.Id;
|
|
}
|
|
|
|
public async Task RenameAsync(int id, string newName)
|
|
{
|
|
await _database.InitAsync();
|
|
var cleanName = NormalizeName(newName);
|
|
|
|
var all = await _database.Connection.Table<SymptomItem>().ToListAsync();
|
|
if (all.Any(x => x.Id != id && string.Equals(x.Name, cleanName, StringComparison.OrdinalIgnoreCase)))
|
|
throw new InvalidOperationException($"Das Symptom '{cleanName}' existiert bereits.");
|
|
|
|
var item = all.FirstOrDefault(x => x.Id == id);
|
|
if (item is null)
|
|
return;
|
|
|
|
item.Name = cleanName;
|
|
await _database.Connection.UpdateAsync(item);
|
|
}
|
|
|
|
public async Task SetActiveAsync(int id, bool isActive)
|
|
{
|
|
await _database.InitAsync();
|
|
var item = await GetByIdAsync(id);
|
|
if (item is null)
|
|
return;
|
|
|
|
item.IsActive = isActive;
|
|
await _database.Connection.UpdateAsync(item);
|
|
}
|
|
|
|
public async Task<bool> HasUsageAsync(int symptomId)
|
|
{
|
|
await _database.InitAsync();
|
|
return await _database.Connection.Table<HealthEntrySymptom>()
|
|
.Where(x => x.SymptomItemId == symptomId)
|
|
.CountAsync() > 0;
|
|
}
|
|
|
|
public async Task DeleteOrDeactivateAsync(int symptomId)
|
|
{
|
|
await _database.InitAsync();
|
|
var item = await GetByIdAsync(symptomId);
|
|
if (item is null)
|
|
return;
|
|
|
|
if (await HasUsageAsync(symptomId))
|
|
{
|
|
item.IsActive = false;
|
|
await _database.Connection.UpdateAsync(item);
|
|
return;
|
|
}
|
|
|
|
await _database.Connection.DeleteAsync(item);
|
|
}
|
|
|
|
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);
|
|
|
|
var ids = symptomIds?
|
|
.Distinct()
|
|
.Where(x => x > 0)
|
|
.ToList() ?? new List<int>();
|
|
|
|
foreach (var symptomId in ids)
|
|
{
|
|
await _database.Connection.InsertAsync(new HealthEntrySymptom
|
|
{
|
|
HealthEntryId = entryId,
|
|
SymptomItemId = symptomId
|
|
});
|
|
}
|
|
}
|
|
|
|
public async Task<List<int>> GetSymptomIdsForEntryAsync(int entryId)
|
|
{
|
|
await _database.InitAsync();
|
|
|
|
return await _database.Connection.QueryScalarsAsync<int>(
|
|
$"SELECT {nameof(HealthEntrySymptom.SymptomItemId)} " +
|
|
$"FROM {nameof(HealthEntrySymptom)} " +
|
|
$"WHERE {nameof(HealthEntrySymptom.HealthEntryId)} = ?",
|
|
entryId);
|
|
}
|
|
|
|
public async Task<Dictionary<int, List<string>>> GetSymptomsByEntryIdsAsync(IEnumerable<int> entryIds)
|
|
{
|
|
await _database.InitAsync();
|
|
|
|
var result = new Dictionary<int, List<string>>();
|
|
var idSet = entryIds.Distinct().ToHashSet();
|
|
|
|
if (idSet.Count == 0)
|
|
return result;
|
|
|
|
var links = await _database.Connection.Table<HealthEntrySymptom>().ToListAsync();
|
|
var relevantLinks = links.Where(x => idSet.Contains(x.HealthEntryId)).ToList();
|
|
if (relevantLinks.Count == 0)
|
|
return result;
|
|
|
|
var symptoms = await _database.Connection.Table<SymptomItem>().ToListAsync();
|
|
var symptomMap = symptoms.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;
|
|
}
|
|
|
|
private static string NormalizeName(string? name)
|
|
{
|
|
var cleanName = (name ?? string.Empty).Trim();
|
|
if (string.IsNullOrWhiteSpace(cleanName))
|
|
throw new ArgumentException("Der Symptomname darf nicht leer sein.", nameof(name));
|
|
|
|
return cleanName;
|
|
}
|
|
}
|