This commit is contained in:
2024-10-13 16:34:46 +02:00
parent b6755ae77f
commit d7d7db8dd4
12 changed files with 140 additions and 85 deletions

View File

@@ -8,7 +8,7 @@ using static System.Runtime.InteropServices.JavaScript.JSType;
namespace Jugenddienst_Stunden.Models;
class Auth {
public static async Task<string> GetApiDataWithAuthAsync(string url, string token) {
public static async Task<string?> GetApiDataWithAuthAsync(string url, string token) {
// Erstellen eines HttpClient-Objekts
using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
@@ -62,7 +62,7 @@ class Auth {
string json = JsonSerializer.Serialize<DayTime>(item);
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = null;
HttpResponseMessage? response = null;
if (isNewItem)
response = await client.PostAsync(url, content);
else

View File

@@ -6,21 +6,21 @@ using System.Text;
namespace Jugenddienst_Stunden.Models;
public class Operator : ObservableObject {
public string id;
public string name;
public string surname;
public string email;
public string password;
public string lang;
public string admin;
public string aktiv;
public string department;
public string department_name;
public string num;
public string year;
public string? id;
public string? name;
public string? surname;
public string? email;
public string? password;
public string? lang;
public string? admin;
public string? aktiv;
public string? department;
public string? department_name;
public string? num;
public string? year;
public event EventHandler<string> AlertEvent;
public event EventHandler<string>? AlertEvent;
public static async Task<Operator> LoadData(string apiKey) {

View File

@@ -18,11 +18,13 @@ internal class Stunde : ObservableObject {
public static async Task<Hours> LoadData() {
Preferences.Default.Set("apiKey", "NXw5NDdCcEdLMVNDZTRENmphWG02MjlyeFFDenN8aHR0cDovL2hvdXJzLmRhdW5pLm1pbmUubnU6ODEvYXBwYXBp");
Preferences.Default.Set("name", "Johannes");
Preferences.Default.Set("surname", "Fink");
Preferences.Default.Set("EmployeeId", 5);
Preferences.Default.Set("apiUrl", "http://hours.dauni.mine.nu:81/appapi");
////Preferences.Default.Set("apiKey", "NXw5NDdCcEdLMVNDZTRENmphWG02MjlyeFFDenN8aHR0cDovL2hvdXJzLmRhdW5pLm1pbmUubnU6ODEvYXBwYXBp");
//Preferences.Default.Set("apiKey", "NXw5NDdCcEdLMVNDZTRENmphWG02MjlyeFFDenN8aHR0cHM6Ly9zdHVuZGVuLmpkLWxhbmEtdGlzZW5zLml0L2FwcGFwaQ==");//Online
//Preferences.Default.Set("name", "Johannes");
//Preferences.Default.Set("surname", "Fink");
//Preferences.Default.Set("EmployeeId", 5);
////Preferences.Default.Set("apiUrl", "http://hours.dauni.mine.nu:81/appapi");
//Preferences.Default.Set("apiUrl", "https://stunden.jd-lana-tisens.it/appapi");
apiKey = Preferences.Default.Get("apiKey", "NXw5NDdCcEdLMVNDZTRENmphWG02MjlyeFFDenN8aHR0cDovL2hvdXJzLmRhdW5pLm1pbmUubnU6ODEvYXBwYXBp");
EmployeeId = Preferences.Default.Get("employeeId", 5);
@@ -38,14 +40,15 @@ internal class Stunde : ObservableObject {
Hours hours = new Hours();
if (Connectivity.Current.NetworkAccess == NetworkAccess.None) {
await App.Current.MainPage.DisplayAlert("Keine Internetverbindung",
"Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.",
"OK");
throw new Exception("Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.");
//await App.Current.MainPage.DisplayAlert("Keine Internetverbindung",
// "Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.",
// "OK");
} else {
var tokendata = new TokenData(apiKey);
//string data = await Auth.GetApiDataWithAuthAsync(requestUrl, apiKey);
string data = await Auth.GetApiDataWithAuthAsync(tokendata.url + "?hours", tokendata.apiKey);
string? data = await Auth.GetApiDataWithAuthAsync(tokendata.url + "?hours", tokendata.apiKey);
if (data == "null") {
throw new Exception("Keine Daten erhalten");
@@ -54,7 +57,10 @@ internal class Stunde : ObservableObject {
throw new Exception("Problem mit Token");
}
hours = JsonConvert.DeserializeObject<Hours>(data);
if (data == null) {
throw new Exception("Keine Daten erhalten");
}
hours = JsonConvert.DeserializeObject<Hours>(data) ?? throw new Exception("Fehler beim Deserialisieren der Daten");
}
return hours;
@@ -70,13 +76,14 @@ internal class Stunde : ObservableObject {
Hours hours = new Hours();
if (Connectivity.Current.NetworkAccess == NetworkAccess.None) {
await App.Current.MainPage.DisplayAlert("Keine Internetverbindung",
"Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.",
"OK");
throw new Exception("Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.");
//await App.Current.MainPage.DisplayAlert("Keine Internetverbindung",
// "Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.",
// "OK");
} else {
var tokendata = new TokenData(apiKey);
string data = await Auth.GetApiDataWithAuthAsync(tokendata.url + "?basic", tokendata.apiKey);
string? data = await Auth.GetApiDataWithAuthAsync(tokendata.url + "?basic", tokendata.apiKey);
if (data == "null") {
throw new Exception("Keine Daten erhalten");
@@ -84,8 +91,10 @@ internal class Stunde : ObservableObject {
if (data == "\"Lalala\"") {
throw new Exception("Problem mit Token");
}
hours = JsonConvert.DeserializeObject<Hours>(data);
if (data == null) {
throw new Exception("Keine Daten erhalten");
}
hours = JsonConvert.DeserializeObject<Hours>(data) ?? throw new Exception("Fehler beim Deserialisieren der Daten");
}
return hours;
@@ -105,14 +114,15 @@ internal class Stunde : ObservableObject {
List<DayTime> daytimes = new List<DayTime>();
if (Connectivity.Current.NetworkAccess == NetworkAccess.None) {
await App.Current.MainPage.DisplayAlert("Keine Internetverbindung",
"Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.",
"OK");
throw new Exception("Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.");
//await App.Current.MainPage.DisplayAlert("Keine Internetverbindung",
// "Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.",
// "OK");
} else {
var tokendata = new TokenData(apiKey);
//string data = await Auth.GetApiDataWithAuthAsync(requestUrl, apiKey);
string data = await Auth.GetApiDataWithAuthAsync(tokendata.url + "?date=" + date.ToString("yyyy-MM-dd"), tokendata.apiKey);
string? data = await Auth.GetApiDataWithAuthAsync(tokendata.url + "?date=" + date.ToString("yyyy-MM-dd"), tokendata.apiKey);
if (data == "null") {
throw new Exception("Keine Daten für " + date.ToString("ddd. dd. MMM") + " erhalten");
@@ -120,9 +130,11 @@ internal class Stunde : ObservableObject {
if (data == "\"Lalala\"") {
throw new Exception("Problem mit Token");
}
if (data == null) {
throw new Exception("Keine Daten erhalten");
}
//daytimes = System.Text.Json.JsonSerializer.Deserialize<List<DayTime>>(data);
daytimes = JsonConvert.DeserializeObject<List<DayTime>>(data);
daytimes = JsonConvert.DeserializeObject<List<DayTime>>(data) ?? throw new Exception("Fehler beim Deserialisieren der Daten");
}
@@ -138,10 +150,14 @@ internal class Stunde : ObservableObject {
public static async Task<DayTime> LoadEntry(int id) {
var tokendata = new TokenData(apiKey);
var data = await Auth.GetApiDataWithAuthAsync(tokendata.url + "?id=" + id, 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);
DayTime hours = JsonConvert.DeserializeObject<DayTime>(data) ?? throw new Exception("Fehler beim Deserialisieren der Daten");
hours.TimeSpanVon = hours.begin.ToTimeSpan();
hours.TimeSpanBis = hours.end.ToTimeSpan();