Settings so halb und a bissi aufgeräumt ...
This commit is contained in:
@@ -1,148 +0,0 @@
|
||||
using Jugenddienst_Stunden.Types;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
|
||||
namespace Jugenddienst_Stunden.Models;
|
||||
|
||||
class Auth {
|
||||
public static async Task<string?> GetApiDataWithAuthAsync(string url, string token) {
|
||||
// Erstellen eines HttpClient-Objekts
|
||||
using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
|
||||
|
||||
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
||||
|
||||
// Hinzufügen des Bearer-Tokens zum Authorization-Header
|
||||
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
||||
|
||||
|
||||
// Senden der Anfrage und Abrufen der Antwort
|
||||
using (HttpResponseMessage HttpResponseMessage = await client.GetAsync(url).ConfigureAwait(false)) {
|
||||
// Überprüfen, ob die Anfrage erfolgreich war
|
||||
if (HttpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK) {
|
||||
using (HttpContent HttpContent = HttpResponseMessage.Content) {
|
||||
// Lesen und Rückgabe der Antwort als String
|
||||
string responseData = await HttpContent.ReadAsStringAsync();
|
||||
return responseData;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Stundeneintrag speichern
|
||||
/// </summary>
|
||||
public static async Task SaveItemAsync(string url, string token, DayTime item, bool isNewItem = false) {
|
||||
//using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
|
||||
|
||||
//Uhrzeiten sollten sinnvolle Werte haben
|
||||
if (item.TimeSpanVon == item.TimeSpanBis) {
|
||||
throw new Exception("Beginn und Ende sind gleich");
|
||||
}
|
||||
|
||||
if (item.TimeSpanBis < item.TimeSpanVon) {
|
||||
throw new Exception("Ende ist vor Beginn");
|
||||
}
|
||||
|
||||
TimeSpan span = TimeSpan.Zero;
|
||||
span += item.TimeSpanBis - item.TimeSpanVon;
|
||||
if (span.Hours > 10) {
|
||||
//Hier vielleicht eine Abfrage, ob mehr als 10 Stunden gesund sind?
|
||||
}
|
||||
|
||||
//Gemeinde ist ein Pflichtfeld
|
||||
if (item.GemeindeAktiv == null) {
|
||||
throw new Exception("Gemeinde nicht gewählt");
|
||||
}
|
||||
//Projekt ist ein Pflichtfeld
|
||||
if (item.ProjektAktiv == null) {
|
||||
throw new Exception("Projekt nicht gewählt");
|
||||
}
|
||||
//Keine Beschreibung
|
||||
if (string.IsNullOrEmpty(item.description)) {
|
||||
throw new Exception("Keine Beschreibung");
|
||||
}
|
||||
//try {
|
||||
HttpClient client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
||||
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
||||
|
||||
string json = JsonSerializer.Serialize<DayTime>(item);
|
||||
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
HttpResponseMessage? response = null;
|
||||
if (isNewItem)
|
||||
response = await client.PostAsync(url, content);
|
||||
else
|
||||
response = await client.PutAsync(url, content);
|
||||
|
||||
//if (response.IsSuccessStatusCode)
|
||||
// Debug.WriteLine(@"\tTodoItem successfully saved.");
|
||||
if (!response.IsSuccessStatusCode) {
|
||||
throw new Exception("Fehler beim Speichern " + response.Content);
|
||||
}
|
||||
//} catch (Exception ex) {
|
||||
// Debug.WriteLine(@"\tERROR {0}", ex.Message);
|
||||
//}
|
||||
//}
|
||||
}
|
||||
|
||||
public static async Task DeleteItemAsync(string url, string token) {
|
||||
//using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
|
||||
|
||||
//try {
|
||||
HttpClient client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
||||
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
||||
|
||||
HttpResponseMessage response = await client.DeleteAsync(url);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
throw new Exception("Fehler beim Löschen " + response.Content);
|
||||
//if (response.IsSuccessStatusCode)
|
||||
// Debug.WriteLine(@"\tTodoItem successfully deleted.");
|
||||
//} catch (Exception ex) {
|
||||
// Debug.WriteLine(@"\tERROR {0}", ex.Message);
|
||||
//}
|
||||
//}
|
||||
}
|
||||
|
||||
public static async Task<User> AuthUserPass(string user, string pass, string url) {
|
||||
|
||||
var values = new Dictionary<string, string>
|
||||
{
|
||||
{ "user", user },
|
||||
{ "pass", pass }
|
||||
};
|
||||
|
||||
var content = new FormUrlEncodedContent(values);
|
||||
|
||||
using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
|
||||
|
||||
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
||||
|
||||
// Senden der Anfrage und Abrufen der Antwort
|
||||
using (HttpResponseMessage HttpResponseMessage = await client.PostAsync(url, content).ConfigureAwait(false)) {
|
||||
// Überprüfen, ob die Anfrage erfolgreich war
|
||||
if (HttpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK) {
|
||||
using (HttpContent HttpContent = HttpResponseMessage.Content) {
|
||||
// Lesen und Rückgabe der Antwort als String
|
||||
|
||||
string responseData = await HttpContent.ReadAsStringAsync();
|
||||
User userData = System.Text.Json.JsonSerializer.Deserialize<User>(responseData) ?? throw new Exception("Fehler beim Deserialisieren der Daten");
|
||||
return userData;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
180
Jugenddienst Stunden/Models/BaseFunc.cs
Normal file
180
Jugenddienst Stunden/Models/BaseFunc.cs
Normal file
@@ -0,0 +1,180 @@
|
||||
using Jugenddienst_Stunden.Types;
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
|
||||
namespace Jugenddienst_Stunden.Models;
|
||||
|
||||
internal static class BaseFunc {
|
||||
|
||||
|
||||
internal static async Task<string> GetApiDataWithAuthAsync(string url, string token) {
|
||||
|
||||
if (Connectivity.Current.NetworkAccess == NetworkAccess.None)
|
||||
throw new Exception("Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.");
|
||||
|
||||
if (string.IsNullOrEmpty(token))
|
||||
throw new Exception("Kein APIKEY, bitte zuerst Login durchführen");
|
||||
|
||||
// Erstellen eines HttpClient-Objekts
|
||||
using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
|
||||
|
||||
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
||||
|
||||
// Hinzufügen des Bearer-Tokens zum Authorization-Header
|
||||
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
||||
|
||||
// Senden der Anfrage und Abrufen der Antwort
|
||||
using (HttpResponseMessage HttpResponseMessage = await client.GetAsync(url).ConfigureAwait(false)) {
|
||||
var byteArray = await HttpResponseMessage.Content.ReadAsByteArrayAsync();
|
||||
string responseData = Encoding.UTF8.GetString(byteArray);
|
||||
using (HttpContent HttpContent = HttpResponseMessage.Content) {
|
||||
//responseData = await HttpContent.ReadAsStringAsync();
|
||||
}
|
||||
if (HttpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK) {
|
||||
return responseData;
|
||||
} else {
|
||||
var options = new JsonDocumentOptions {
|
||||
AllowTrailingCommas = true
|
||||
};
|
||||
using (JsonDocument doc = JsonDocument.Parse(responseData, options)) {
|
||||
JsonElement root = doc.RootElement;
|
||||
string message = root.GetProperty("message").GetString() ?? throw new Exception("Fehler: 'message' ist null.");
|
||||
|
||||
throw new Exception(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
internal static async Task<User> AuthUserPass(string user, string pass, string url) {
|
||||
|
||||
var values = new Dictionary<string, string>
|
||||
{
|
||||
{ "user", user },
|
||||
{ "pass", pass }
|
||||
};
|
||||
|
||||
var content = new FormUrlEncodedContent(values);
|
||||
|
||||
using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
|
||||
|
||||
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
||||
|
||||
// Senden der Anfrage und Abrufen der Antwort
|
||||
using (HttpResponseMessage HttpResponseMessage = await client.PostAsync(url, content).ConfigureAwait(false)) {
|
||||
if (!HttpResponseMessage.IsSuccessStatusCode)
|
||||
throw new Exception("Fehler beim Einloggen " + HttpResponseMessage.Content);
|
||||
|
||||
// Überprüfen, ob die Anfrage erfolgreich war
|
||||
if (HttpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK) {
|
||||
using (HttpContent HttpContent = HttpResponseMessage.Content) {
|
||||
// Lesen und Rückgabe der Antwort als String
|
||||
|
||||
string responseData = await HttpContent.ReadAsStringAsync();
|
||||
User userData = System.Text.Json.JsonSerializer.Deserialize<User>(responseData) ?? throw new Exception("Fehler beim Deserialisieren der Daten");
|
||||
return userData;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
internal static HoursBase 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>
|
||||
/// Stundeneintrag speichern
|
||||
/// </summary>
|
||||
internal static async Task SaveItemAsync(string url, string token, DayTime item, bool isNewItem = false) {
|
||||
|
||||
|
||||
//Uhrzeiten sollten sinnvolle Werte haben
|
||||
if (item.TimeSpanVon == item.TimeSpanBis) {
|
||||
throw new Exception("Beginn und Ende sind gleich");
|
||||
}
|
||||
|
||||
if (item.TimeSpanBis < item.TimeSpanVon) {
|
||||
throw new Exception("Ende ist vor Beginn");
|
||||
}
|
||||
|
||||
TimeSpan span = TimeSpan.Zero;
|
||||
span += item.TimeSpanBis - item.TimeSpanVon;
|
||||
if (span.Hours > 10) {
|
||||
//Hier vielleicht eine Abfrage, ob mehr als 10 Stunden gesund sind?
|
||||
//Das müsste aber das ViewModel machen
|
||||
}
|
||||
|
||||
//Gemeinde ist ein Pflichtfeld
|
||||
if (item.GemeindeAktiv == null) {
|
||||
throw new Exception("Gemeinde nicht gewählt");
|
||||
}
|
||||
//Projekt ist ein Pflichtfeld
|
||||
if (item.ProjektAktiv == null) {
|
||||
throw new Exception("Projekt nicht gewählt");
|
||||
}
|
||||
//Keine Beschreibung
|
||||
if (string.IsNullOrEmpty(item.Description)) {
|
||||
throw new Exception("Keine Beschreibung");
|
||||
}
|
||||
using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
|
||||
//HttpClient client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
||||
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
||||
|
||||
//string json = JsonSerializer.Serialize<DayTime>(item);
|
||||
string json = JsonConvert.SerializeObject(item);
|
||||
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
HttpResponseMessage? response = null;
|
||||
if (isNewItem)
|
||||
response = await client.PostAsync(url, content);
|
||||
else
|
||||
response = await client.PutAsync(url, content);
|
||||
|
||||
if (!response.IsSuccessStatusCode) {
|
||||
throw new Exception("Fehler beim Speichern " + response.Content);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal static async Task DeleteItemAsync(string url, string token) {
|
||||
using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
|
||||
|
||||
//HttpClient client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
||||
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
||||
|
||||
HttpResponseMessage response = await client.DeleteAsync(url);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
throw new Exception("Fehler beim Löschen " + response.Content);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
125
Jugenddienst Stunden/Models/HoursBase.cs
Normal file
125
Jugenddienst Stunden/Models/HoursBase.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
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 = new TokenData(apiKey);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Einstellungen
|
||||
/// </summary>
|
||||
internal async Task<Settings> LoadSettings() {
|
||||
|
||||
string data = await BaseFunc.GetApiDataWithAuthAsync(tokendata.Url + "?settings", tokendata.ApiKey);
|
||||
|
||||
Settings _settings = JsonConvert.DeserializeObject<Settings>(data) ?? throw new Exception("Fehler beim Deserialisieren der Daten");
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Jugenddienst_Stunden.Models;
|
||||
namespace Jugenddienst_Stunden.Models;
|
||||
internal class Note {
|
||||
public string Filename { get; set; }
|
||||
public string Text { get; set; }
|
||||
|
||||
@@ -1,52 +1,23 @@
|
||||
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using Jugenddienst_Stunden.ViewModels;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using Newtonsoft.Json;
|
||||
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;
|
||||
namespace Jugenddienst_Stunden.Models;
|
||||
internal class Operator : HoursBase {
|
||||
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) {
|
||||
|
||||
Operator OperatorVar = new Operator();
|
||||
|
||||
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("Keine Internetverbindung");
|
||||
} else {
|
||||
var tokendata = new TokenData(apiKey);
|
||||
|
||||
//try {
|
||||
string data = await Auth.GetApiDataWithAuthAsync(tokendata.url, tokendata.apiKey);
|
||||
if (data == "\"Lalala\"") {
|
||||
throw new Exception("Problem mit Token");
|
||||
}
|
||||
if (data != "null") {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,211 +0,0 @@
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -3,17 +3,17 @@
|
||||
|
||||
namespace Jugenddienst_Stunden.Models;
|
||||
|
||||
class TokenData {
|
||||
public string token { get; set; }
|
||||
public string apiKey { get; set; }
|
||||
public string url { get; set; }
|
||||
public string operator_id { get; set; }
|
||||
internal class TokenData {
|
||||
public string Token { get; set; }
|
||||
public string ApiKey { get; set; }
|
||||
public string Url { get; set; }
|
||||
public string Operator_id { get; set; }
|
||||
|
||||
public TokenData(string ak) {
|
||||
string dat = Encoding.UTF8.GetString(Convert.FromBase64String(ak));
|
||||
token = dat.Split('|')[1]; ;
|
||||
url = dat.Split('|')[2]; ;
|
||||
operator_id = dat.Split('|')[0]; ;
|
||||
apiKey = ak;
|
||||
Token = dat.Split('|')[1]; ;
|
||||
Url = dat.Split('|')[2]; ;
|
||||
Operator_id = dat.Split('|')[0]; ;
|
||||
ApiKey = ak;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user