Add Exceptionhandler, AlertService JSON-Converter AppSettings via DI Reformat Code
80 lines
2.7 KiB
C#
80 lines
2.7 KiB
C#
using Jugenddienst_Stunden.Interfaces;
|
|
using Jugenddienst_Stunden.Infrastructure;
|
|
using Jugenddienst_Stunden.Models;
|
|
using Jugenddienst_Stunden.Types;
|
|
|
|
namespace Jugenddienst_Stunden.Repositories;
|
|
|
|
/// <summary>
|
|
/// Standard-Repository, das die bestehende API-/Model-Logik kapselt.
|
|
/// </summary>
|
|
internal class HoursRepository : IHoursRepository {
|
|
private readonly IApiClient _api;
|
|
|
|
public HoursRepository(IApiClient api) {
|
|
_api = api;
|
|
}
|
|
|
|
public async Task<BaseResponse> LoadBase(string query) {
|
|
// Der bestehende Code übergab eine Query ohne führendes '?'
|
|
var dict = QueryToDictionary(query);
|
|
var res= await _api.GetAsync<BaseResponse>("", dict).ConfigureAwait(false);
|
|
return res;
|
|
}
|
|
|
|
public async Task<Settings> LoadSettings() {
|
|
var res = await _api.GetAsync<BaseResponse>("", new Dictionary<string, string?> { ["settings"] = "1" })
|
|
.ConfigureAwait(false);
|
|
return res.settings;
|
|
}
|
|
|
|
public async Task<Hours> LoadData() {
|
|
var res = await _api.GetAsync<BaseResponse>("", new Dictionary<string, string?> { ["hours"] = "1" })
|
|
.ConfigureAwait(false);
|
|
return res.hour;
|
|
}
|
|
|
|
public Task<User> LoadUser(string apiKey) {
|
|
// Für die erste Iteration bleibt das Token global; der Endpoint ohne Query liefert user
|
|
return _api.GetAsync<BaseResponse>("", null).ContinueWith(t => t.Result.user);
|
|
}
|
|
|
|
public async Task<List<DayTime>> LoadDay(DateTime date) {
|
|
var res = await _api
|
|
.GetAsync<BaseResponse>("", new Dictionary<string, string?> { ["date"] = date.ToString("yyyy-MM-dd") })
|
|
.ConfigureAwait(false);
|
|
return res.daytimes ?? new List<DayTime>();
|
|
}
|
|
|
|
public async Task<DayTime> LoadEntry(int id) {
|
|
var res = await _api.GetAsync<BaseResponse>("", new Dictionary<string, string?> { ["id"] = id.ToString() })
|
|
.ConfigureAwait(false);
|
|
res.daytime.TimeSpanVon = res.daytime.Begin.ToTimeSpan();
|
|
res.daytime.TimeSpanBis = res.daytime.End.ToTimeSpan();
|
|
return res.daytime;
|
|
}
|
|
|
|
public async Task<DayTime> SaveEntry(DayTime stunde) {
|
|
bool isNew = stunde.Id is null;
|
|
var method = isNew ? HttpMethod.Post : HttpMethod.Put;
|
|
await _api.SendAsync<object>(method, "", stunde).ConfigureAwait(false);
|
|
return stunde;
|
|
}
|
|
|
|
public Task DeleteEntry(DayTime stunde)
|
|
=> _api.DeleteAsync($"/entry/{stunde.Id}");
|
|
|
|
private static Dictionary<string, string?> QueryToDictionary(string query) {
|
|
var dict = new Dictionary<string, string?>();
|
|
if (string.IsNullOrWhiteSpace(query)) return dict;
|
|
var q = query.TrimStart('?');
|
|
foreach (var part in q.Split('&', StringSplitOptions.RemoveEmptyEntries)) {
|
|
var kv = part.Split('=', 2);
|
|
var key = Uri.UnescapeDataString(kv[0]);
|
|
var val = kv.Length > 1 ? Uri.UnescapeDataString(kv[1]) : null;
|
|
dict[key] = val;
|
|
}
|
|
|
|
return dict;
|
|
}
|
|
} |