Refactor Api-Client
Add Exceptionhandler, AlertService JSON-Converter AppSettings via DI Reformat Code
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Jugenddienst_Stunden.Interfaces;
|
||||
using Jugenddienst_Stunden.Infrastructure;
|
||||
using Jugenddienst_Stunden.Models;
|
||||
using Jugenddienst_Stunden.Types;
|
||||
|
||||
@@ -8,12 +9,72 @@ namespace Jugenddienst_Stunden.Repositories;
|
||||
/// Standard-Repository, das die bestehende API-/Model-Logik kapselt.
|
||||
/// </summary>
|
||||
internal class HoursRepository : IHoursRepository {
|
||||
public async Task<BaseResponse> LoadBase(string query) => await HoursBase.LoadBase(query);
|
||||
public async Task<Settings> LoadSettings() => await HoursBase.LoadSettings();
|
||||
public async Task<Hours> LoadData() => await HoursBase.LoadData();
|
||||
public async Task<User> LoadUser(string apiKey) => await HoursBase.LoadUser(apiKey);
|
||||
public async Task<List<DayTime>> LoadDay(DateTime date) => await HoursBase.LoadDay(date);
|
||||
public async Task<DayTime> LoadEntry(int id) => await HoursBase.LoadEntry(id);
|
||||
public async Task<DayTime> SaveEntry(DayTime stunde) => await HoursBase.SaveEntry(stunde);
|
||||
public async Task DeleteEntry(DayTime stunde) => await HoursBase.DeleteEntry(stunde);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user