Add Save function, Visuals, Stuff

This commit is contained in:
2024-09-25 15:32:58 +02:00
parent a066b9caba
commit 3f2191673e
7 changed files with 250 additions and 235 deletions

View File

@@ -4,6 +4,7 @@ using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Jugenddienst_Stunden.Types;
using System.Text.Json;
using ZXing.QrCode.Internal;
namespace Jugenddienst_Stunden.Models {
@@ -12,6 +13,7 @@ namespace Jugenddienst_Stunden.Models {
public Hours hours;
public static async Task<string> GetApiDataWithAuthAsync(string url, string token) {
// Erstellen eines HttpClient-Objekts
using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
@@ -26,44 +28,48 @@ namespace Jugenddienst_Stunden.Models {
//system.net.webexception nachricht = socket closed
//Grund: Falscher DNS-Server liefert falsche Server-IP
// 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;
}
}
}
// Senden der Anfrage und Abrufen der Antwort
//HttpResponseMessage response = await client.GetAsync(url);
// Überprüfen, ob die Anfrage erfolgreich war
//response.EnsureSuccessStatusCode();
// Lesen und Rückgabe der Antwort als String
//string responseData = await response.Content.ReadAsStringAsync();
//return responseData;
}
return null;
}
public static async Task Main(string[] args) {
string apiUrl = args[0]; // Ersetzen Sie dies durch die tatsächliche URL der API
string bearerToken = args[1]; // Ersetzen Sie hier durch Ihr tatsächliches Token
Preferences.Default.Set("apiUrl", apiUrl);
public static async Task SaveItemAsync(string url, string token, DayTime item, bool isNewItem = false) {
//using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
string data = await GetApiDataWithAuthAsync(apiUrl, bearerToken);
try {
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
if (data != null) {
//Console.WriteLine("API-Daten:");
//Console.WriteLine(data);
//JOperator operator = Newtonsoft.Json.JsonConvert.DeserializeObject<JOperator>(data);
//var zeug = Newtonsoft.Json.JsonConvert.DeserializeObject(data);
Hours hours = Newtonsoft.Json.JsonConvert.DeserializeObject<Hours>(data);
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.");
} catch (Exception ex) {
Debug.WriteLine(@"\tERROR {0}", ex.Message);
}
//}
}
}
}

View File

@@ -16,25 +16,16 @@ namespace Jugenddienst_Stunden.Models {
public string aktiv;
public string department;
public string department_name;
//public List<DateOnly>? timetable_begin;
/// <summary>
/// TimeTable Num
/// </summary>
public string num;
//public List<TimeOnly>? saved_overtime;
//public List<TimeOnly>? saved_holiday;
//public TimeOnly overtime;
//public TimeOnly holiday;
public string year;
//public List<int>? timetable;
public event EventHandler<string> AlertEvent;
public static async Task<Operator> LoadData(string apiKey) {
//var apiKey = Preferences.Default.Get("apiKey", "");
Operator OperatorVar = new Operator();
//Operator operator = 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.",
@@ -48,12 +39,11 @@ namespace Jugenddienst_Stunden.Models {
if (data == "\"Lalala\"") {
throw new Exception("Problem mit Token");
}
if (data != null) {
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);
}
}

View File

@@ -33,7 +33,8 @@ namespace Jugenddienst_Stunden.Models {
//private static readonly string requestUrl = $"{BaseAddress}/appapi?hours";
//public static string apiKey = "Mnx6amdpUkFCdWMvc0lmZ3dWeXRVSWx3VGRDRDh8aHR0cDovL2hvdXJzLmRhdW5pLm1pbmUubnU6ODEvYXBwYXBp"; //Daniel Pichler
//public static string apiKey = "M3xraUdoSktrdGowaUZoaStJbDJLWmIyTFhYeDh8aHR0cDovL2hvdXJzLmRhdW5pLm1pbmUubnU6ODEvYXBwYXBp"; //Daniel Pichler
public static async Task<Hours> LoadData() {
@@ -41,6 +42,8 @@ namespace Jugenddienst_Stunden.Models {
//Temporär fix für virtuelle App
//apiKey = "NXw5eFF0RVQ5TFBMTXFVeFIzQURtd01idHFUb2d8aHR0cDovL2hvdXJzLmRhdW5pLm1pbmUubnU6ODEvYXBwYXBp"; //Johannes Fink
//apiKey = "M3xraUdoSktrdGowaUZoaStJbDJLWmIyTFhYeDh8aHR0cDovL2hvdXJzLmRhdW5pLm1pbmUubnU6ODEvYXBwYXBp"; //Lea Mair
//apiKey = "Mnx6amdpUkFCdWMvc0lmZ3dWeXRVSWx3VGRDRDh8aHR0cDovL2hvdXJzLmRhdW5pLm1pbmUubnU6ODEvYXBwYXBp"; //Daniel Pichler
Hours hours = new Hours();
@@ -55,7 +58,7 @@ namespace Jugenddienst_Stunden.Models {
//string data = await Auth.GetApiDataWithAuthAsync(requestUrl, apiKey);
string data = await Auth.GetApiDataWithAuthAsync(tokendata.url + "?hours", tokendata.apiKey);
if (data == null) {
if (data == "null") {
throw new Exception("Keine Daten erhalten");
}
if (data == "\"Lalala\"") {
@@ -83,8 +86,8 @@ namespace Jugenddienst_Stunden.Models {
//string data = await Auth.GetApiDataWithAuthAsync(requestUrl, apiKey);
string data = await Auth.GetApiDataWithAuthAsync(tokendata.url + "?date=" + date.ToString("yyyy-MM-dd"), tokendata.apiKey);
if (data == null) {
throw new Exception("Keine Daten erhalten");
if (data == "null") {
throw new Exception("Keine Daten für " + date.ToString("ddd. dd. MMM") + " erhalten");
}
if (data == "\"Lalala\"") {
throw new Exception("Problem mit Token");
@@ -128,5 +131,12 @@ namespace Jugenddienst_Stunden.Models {
};
}
public static async Task<DayTime> SaveEntry(DayTime stunde) { //, string begin, string end, string freistellung, string bemerkung) {
var tokendata = new TokenData(apiKey);
await Auth.SaveItemAsync(tokendata.url, tokendata.apiKey, stunde,false);
return null;
}
}
}