212 lines
6.4 KiB
C#
212 lines
6.4 KiB
C#
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using Newtonsoft.Json;
|
|
using Jugenddienst_Stunden.Types;
|
|
using System.Collections.ObjectModel;
|
|
using Jugenddienst_Stunden.Exceptions;
|
|
|
|
namespace Jugenddienst_Stunden.Models;
|
|
|
|
internal class Stunde : ObservableObject {
|
|
|
|
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", "");
|
|
|
|
|
|
|
|
public static async Task<Hours> LoadData() {
|
|
|
|
if (string.IsNullOrEmpty(apiKey)) {
|
|
throw new Exception("Kein APIKEY, bitte zuerst Login durchführen");
|
|
}
|
|
|
|
Hours hours = new Hours();
|
|
|
|
if (Connectivity.Current.NetworkAccess == NetworkAccess.None) {
|
|
throw new Exception("Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.");
|
|
} else {
|
|
var tokendata = new TokenData(apiKey);
|
|
|
|
//string data = await Auth.GetApiDataWithAuthAsync(requestUrl, apiKey);
|
|
string? data = await Auth.GetApiDataWithAuthAsync(tokendata.url + "?hours", tokendata.apiKey);
|
|
|
|
if (data == "null") {
|
|
throw new Exception("Keine Daten erhalten");
|
|
}
|
|
if (data == "\"Lalala\"") {
|
|
throw new Exception("Problem mit Token");
|
|
}
|
|
|
|
if (data == null) {
|
|
throw new Exception("Keine Daten erhalten");
|
|
}
|
|
hours = JsonConvert.DeserializeObject<Hours>(data) ?? throw new Exception("Fehler beim Deserialisieren der Daten");
|
|
|
|
}
|
|
return hours;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Einstellungen
|
|
/// </summary>
|
|
public static async Task<Settings> LoadSettings()
|
|
{
|
|
|
|
Settings settings;
|
|
|
|
if (Connectivity.Current.NetworkAccess == NetworkAccess.None)
|
|
{
|
|
throw new Exception("Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.");
|
|
}
|
|
else
|
|
{
|
|
var tokendata = new TokenData(apiKey);
|
|
|
|
string? data = await Auth.GetApiDataWithAuthAsync(tokendata.url + "?settings", tokendata.apiKey);
|
|
|
|
if (data == "null")
|
|
{
|
|
throw new Exception("Keine Daten erhalten");
|
|
}
|
|
if (data == "\"Lalala\"")
|
|
{
|
|
throw new Exception("Problem mit Token");
|
|
}
|
|
if (data == null)
|
|
{
|
|
throw new Exception("Keine Daten erhalten");
|
|
}
|
|
settings = JsonConvert.DeserializeObject<Settings>(data) ?? throw new Exception("Fehler beim Deserialisieren der Daten");
|
|
|
|
}
|
|
return settings;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Basisdaten: Mitarbeiterdaten, Projekte, Gemeinden, Freistellungen.
|
|
/// </summary>
|
|
public static async Task<Hours> LoadBasicData() {
|
|
|
|
Hours hours = new Hours();
|
|
|
|
if (Connectivity.Current.NetworkAccess == NetworkAccess.None) {
|
|
throw new Exception("Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.");
|
|
} else {
|
|
var tokendata = new TokenData(apiKey);
|
|
|
|
string? data = await Auth.GetApiDataWithAuthAsync(tokendata.url + "?basic", tokendata.apiKey);
|
|
|
|
if (data == "null") {
|
|
throw new Exception("Keine Daten erhalten");
|
|
}
|
|
if (data == "\"Lalala\"") {
|
|
throw new Exception("Problem mit Token");
|
|
}
|
|
if (data == null) {
|
|
throw new Exception("Keine Daten erhalten");
|
|
}
|
|
hours = JsonConvert.DeserializeObject<Hours>(data) ?? throw new Exception("Fehler beim Deserialisieren der Daten");
|
|
|
|
}
|
|
return hours;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Zeiten eines Tages holen
|
|
/// </summary>
|
|
public static async Task<ObservableCollection<DayTime>> LoadDay(DateTime date) {
|
|
if (string.IsNullOrEmpty(apiKey)) {
|
|
throw new Exception("Kein APIKEY, bitte zuerst Login durchführen");
|
|
}
|
|
|
|
if (Connectivity.Current.NetworkAccess == NetworkAccess.None) {
|
|
throw new Exception("Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.");
|
|
}
|
|
|
|
var tokendata = new TokenData(apiKey);
|
|
string? data = await Auth.GetApiDataWithAuthAsync(tokendata.url + "?date=" + date.ToString("yyyy-MM-dd"), tokendata.apiKey);
|
|
|
|
if (data == "null") {
|
|
throw new NoDataException("Keine Daten für " + date.ToString("ddd. dd. MMM") + " erhalten");
|
|
}
|
|
if (data == "\"Lalala\"") {
|
|
throw new Exception("Problem mit Token");
|
|
}
|
|
if (data == null) {
|
|
throw new NoDataException("Keine Daten erhalten");
|
|
}
|
|
|
|
|
|
ObservableCollection<DayTime> daytimes = System.Text.Json.JsonSerializer.Deserialize<ObservableCollection<DayTime>>(data) ?? throw new Exception("Fehler beim Deserialisieren der Daten");
|
|
//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>
|
|
public static async Task<DayTime> LoadEntry(int id) {
|
|
|
|
var tokendata = new TokenData(apiKey);
|
|
string? data = await Auth.GetApiDataWithAuthAsync(tokendata.url + "?id=" + id, tokendata.apiKey);
|
|
|
|
if (data == null) {
|
|
throw new Exception("Keine Daten erhalten");
|
|
}
|
|
|
|
//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;
|
|
}
|
|
|
|
|
|
public static Stunde Load(string filename) {
|
|
filename = System.IO.Path.Combine(FileSystem.AppDataDirectory, filename);
|
|
|
|
if (!File.Exists(filename))
|
|
throw new FileNotFoundException("Unable to find file on local storage.", filename);
|
|
|
|
return
|
|
new() {
|
|
//Filename = Path.GetFileName(filename),
|
|
//Text = File.ReadAllText(filename),
|
|
Date = File.GetLastWriteTime(filename)
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Eintrag speichern
|
|
/// </summary>
|
|
public static async Task<DayTime> SaveEntry(DayTime stunde) { //, string begin, string end, string freistellung, string bemerkung) {
|
|
|
|
var tokendata = new TokenData(apiKey);
|
|
bool isNew = false;
|
|
if (stunde.id == null)
|
|
isNew = true;
|
|
await Auth.SaveItemAsync(tokendata.url, tokendata.apiKey, stunde, isNew);
|
|
|
|
return stunde;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Eintrag löschen
|
|
/// </summary>
|
|
public static async Task DeleteEntry(DayTime stunde) {
|
|
var tokendata = new TokenData(apiKey);
|
|
await Auth.DeleteItemAsync(tokendata.url + "/entry/" + stunde.id, tokendata.apiKey);
|
|
}
|
|
|
|
|
|
}
|