Add Exceptionhandler, AlertService JSON-Converter AppSettings via DI Reformat Code
93 lines
3.4 KiB
C#
93 lines
3.4 KiB
C#
using Jugenddienst_Stunden.Types;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Jugenddienst_Stunden.Models;
|
|
|
|
internal static class HoursBase {
|
|
/// <summary>
|
|
/// Laden ... what can be: "settings", "hours", date="YYYY-MM-DD", id=<int/>
|
|
/// </summary>
|
|
/// <returns>Entire response</returns>
|
|
internal static async Task<BaseResponse> LoadBase(string what) {
|
|
string data = await BaseFunc.GetApiDataWithAuthAsync(GlobalVar.ApiUrl + "?" + what, GlobalVar.ApiKey);
|
|
BaseResponse res = JsonConvert.DeserializeObject<BaseResponse>(data) ??
|
|
throw new Exception("Fehler beim Deserialisieren der Daten");
|
|
return res;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Einstellungen laden
|
|
/// </summary>
|
|
/// <returns>Settings only</returns>
|
|
internal static async Task<Settings> LoadSettings() {
|
|
string data = await BaseFunc.GetApiDataWithAuthAsync(GlobalVar.ApiUrl + "?settings", GlobalVar.ApiKey);
|
|
BaseResponse res = JsonConvert.DeserializeObject<BaseResponse>(data) ??
|
|
throw new Exception("Fehler beim Deserialisieren der Daten");
|
|
return res.settings;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Daten laden
|
|
/// </summary>
|
|
/// <returns>Hours only</returns>
|
|
internal static async Task<Hours> LoadData() {
|
|
string data = await BaseFunc.GetApiDataWithAuthAsync(GlobalVar.ApiUrl + "?hours", GlobalVar.ApiKey);
|
|
BaseResponse res = JsonConvert.DeserializeObject<BaseResponse>(data) ??
|
|
throw new Exception("Fehler beim Deserialisieren der Daten");
|
|
return res.hour;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Benutzerdaten laden
|
|
/// </summary>
|
|
/// <returns>User-Object</returns>
|
|
public static async Task<User> LoadUser(string apiKey) {
|
|
string data = await BaseFunc.GetApiDataWithAuthAsync(GlobalVar.ApiUrl, apiKey);
|
|
BaseResponse res = JsonConvert.DeserializeObject<BaseResponse>(data) ??
|
|
throw new Exception("Fehler beim Deserialisieren der Daten");
|
|
return res.user;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Zeiten eines Tages holen
|
|
/// </summary>
|
|
internal static async Task<List<DayTime>> LoadDay(DateTime date) {
|
|
string data = await BaseFunc.GetApiDataWithAuthAsync(GlobalVar.ApiUrl + "?date=" + date.ToString("yyyy-MM-dd"),
|
|
GlobalVar.ApiKey);
|
|
BaseResponse res = JsonConvert.DeserializeObject<BaseResponse>(data) ??
|
|
throw new Exception("Fehler beim Deserialisieren der Daten");
|
|
return res.daytimes;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Einzelnen Stundeneintrag holen
|
|
/// </summary>
|
|
internal static async Task<DayTime> LoadEntry(int id) {
|
|
string data = await BaseFunc.GetApiDataWithAuthAsync(GlobalVar.ApiUrl + "?id=" + id, GlobalVar.ApiKey);
|
|
BaseResponse res = JsonConvert.DeserializeObject<BaseResponse>(data) ??
|
|
throw new Exception("Fehler beim Deserialisieren der Daten");
|
|
res.daytime.TimeSpanVon = res.daytime.Begin.ToTimeSpan();
|
|
res.daytime.TimeSpanBis = res.daytime.End.ToTimeSpan();
|
|
return res.daytime;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Eintrag speichern
|
|
/// </summary>
|
|
internal static async Task<DayTime> SaveEntry(DayTime stunde) {
|
|
//, string begin, string end, string freistellung, string bemerkung) {
|
|
bool isNew = false;
|
|
if (stunde.Id == null)
|
|
isNew = true;
|
|
await BaseFunc.SaveItemAsync(GlobalVar.ApiUrl, GlobalVar.ApiKey, stunde, isNew);
|
|
|
|
return stunde;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Eintrag löschen
|
|
/// </summary>
|
|
internal static async Task DeleteEntry(DayTime stunde) {
|
|
await BaseFunc.DeleteItemAsync(GlobalVar.ApiUrl + "/entry/" + stunde.Id, GlobalVar.ApiKey);
|
|
}
|
|
} |