Login auch im Testmodus Bei falschem Token nur eine Meldung Exception bei falschem Token während Loadsettings abfangen
126 lines
4.2 KiB
C#
126 lines
4.2 KiB
C#
using Jugenddienst_Stunden.Types;
|
|
using Newtonsoft.Json;
|
|
using System.Collections.ObjectModel;
|
|
|
|
namespace Jugenddienst_Stunden.Models;
|
|
|
|
internal class HoursBase {
|
|
|
|
public DateTime Date { get; set; }
|
|
|
|
public static string apiKey = Preferences.Default.Get("apiKey", "");
|
|
public static int EmployeeId = Preferences.Default.Get("employeeId", 0);
|
|
public static string name = Preferences.Default.Get("name", "");
|
|
public static string surname = Preferences.Default.Get("surname", "");
|
|
public static string apiUrl = Preferences.Default.Get("apiUrl", "");
|
|
|
|
internal static TokenData tokendata;
|
|
|
|
|
|
/// <summary>
|
|
/// Einstellungen
|
|
/// </summary>
|
|
internal async Task<Settings> LoadSettings() {
|
|
|
|
string data = await BaseFunc.GetApiDataWithAuthAsync(tokendata.Url + "?settings", tokendata.ApiKey);
|
|
|
|
Settings _settings = JsonConvert.DeserializeObject<Settings>(data);
|
|
|
|
return _settings;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Daten laden
|
|
/// </summary>
|
|
internal async Task<Hours> LoadData() {
|
|
|
|
Hours hours = new Hours();
|
|
|
|
var tokendata = new TokenData(apiKey);
|
|
|
|
string data = await BaseFunc.GetApiDataWithAuthAsync(tokendata.Url + "?hours", tokendata.ApiKey);
|
|
|
|
hours = JsonConvert.DeserializeObject<Hours>(data) ?? throw new Exception("Fehler beim Deserialisieren der Daten");
|
|
|
|
return hours;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Basisdaten: Mitarbeiterdaten, Projekte, Gemeinden, Freistellungen.
|
|
/// </summary>
|
|
internal static async Task<Hours> LoadBasicData() {
|
|
|
|
Hours hours = new Hours();
|
|
|
|
string data = await BaseFunc.GetApiDataWithAuthAsync(tokendata.Url + "?basic", tokendata.ApiKey);
|
|
|
|
hours = JsonConvert.DeserializeObject<Hours>(data) ?? throw new Exception("Fehler beim Deserialisieren der Daten");
|
|
|
|
return hours;
|
|
}
|
|
|
|
public static async Task<Operator> LoadOperator(string apiKey) {
|
|
|
|
Operator OperatorVar = new Operator();
|
|
|
|
string data = await BaseFunc.GetApiDataWithAuthAsync(tokendata.Url, tokendata.ApiKey);
|
|
|
|
OperatorVar = JsonConvert.DeserializeObject<Operator>(data);
|
|
Preferences.Default.Set("name", OperatorVar.name);
|
|
Preferences.Default.Set("surname", OperatorVar.surname);
|
|
Preferences.Default.Set("apiUrl", tokendata.Url);
|
|
|
|
return OperatorVar;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Zeiten eines Tages holen
|
|
/// </summary>
|
|
internal async Task<List<DayTime>> LoadDay(DateTime date) {
|
|
|
|
string data = await BaseFunc.GetApiDataWithAuthAsync(tokendata.Url + "?date=" + date.ToString("yyyy-MM-dd"), tokendata.ApiKey);
|
|
|
|
//List<DayTime> daytimes = System.Text.Json.JsonSerializer.Deserialize<List<DayTime>>(data) ?? throw new Exception("Fehler beim Deserialisieren der Daten");
|
|
List<DayTime> daytimes = JsonConvert.DeserializeObject<List<DayTime>>(data);
|
|
//List<DayTime> daytimes = JsonConvert.DeserializeObject<List<DayTime>>(data) ?? throw new Exception("Fehler beim Deserialisieren der Daten");
|
|
|
|
return daytimes;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Einzelnen Stundeneintrag holen
|
|
/// </summary>
|
|
internal static async Task<DayTime> LoadEntry(int id) {
|
|
|
|
string data = await BaseFunc.GetApiDataWithAuthAsync(tokendata.Url + "?id=" + id, tokendata.ApiKey);
|
|
|
|
//DayTime hours = Hours.daytime.Find(x => x.id == id);
|
|
DayTime hours = JsonConvert.DeserializeObject<DayTime>(data) ?? throw new Exception("Fehler beim Deserialisieren der Daten");
|
|
hours.TimeSpanVon = hours.Begin.ToTimeSpan();
|
|
hours.TimeSpanBis = hours.End.ToTimeSpan();
|
|
|
|
return hours;
|
|
}
|
|
|
|
/// <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(tokendata.Url, tokendata.ApiKey, stunde, isNew);
|
|
|
|
return stunde;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Eintrag löschen
|
|
/// </summary>
|
|
internal static async Task DeleteEntry(DayTime stunde) {
|
|
await BaseFunc.DeleteItemAsync(tokendata.Url + "/entry/" + stunde.Id, tokendata.ApiKey);
|
|
}
|
|
}
|