Diverse Meldungen
This commit is contained in:
@@ -7,123 +7,147 @@ 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) }) {
|
||||
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");
|
||||
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);
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Stundeneintrag speichern
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="isNewItem"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task SaveItemAsync(string url, string token, DayTime item, bool isNewItem = false) {
|
||||
//using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
|
||||
/// <summary>
|
||||
/// Stundeneintrag speichern
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="isNewItem"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task SaveItemAsync(string url, string token, DayTime item, bool isNewItem = false) {
|
||||
//using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
|
||||
|
||||
//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");
|
||||
}
|
||||
try {
|
||||
HttpClient client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
||||
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
||||
//Uhrzeiten sollten sinnvolle Werte haben
|
||||
if (item.TimeSpanVon == item.TimeSpanBis) {
|
||||
throw new Exception("Beginn und Ende sind gleich");
|
||||
}
|
||||
|
||||
string json = JsonSerializer.Serialize<DayTime>(item);
|
||||
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
if (item.TimeSpanBis < item.TimeSpanVon) {
|
||||
throw new Exception("Ende ist vor Beginn");
|
||||
}
|
||||
|
||||
HttpResponseMessage? response = null;
|
||||
if (isNewItem)
|
||||
response = await client.PostAsync(url, content);
|
||||
else
|
||||
response = await client.PutAsync(url, content);
|
||||
TimeSpan span = TimeSpan.Zero;
|
||||
span += item.TimeSpanBis - item.TimeSpanVon;
|
||||
if (span.Hours > 10) {
|
||||
//Hier vielleicht eine Abfrage, ob mehr als 10 Stunden gesund sind?
|
||||
}
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
Debug.WriteLine(@"\tTodoItem successfully saved.");
|
||||
} catch (Exception ex) {
|
||||
Debug.WriteLine(@"\tERROR {0}", ex.Message);
|
||||
}
|
||||
//}
|
||||
}
|
||||
//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);
|
||||
|
||||
public static async Task DeleteItemAsync(string url, string token) {
|
||||
//using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
|
||||
string json = JsonSerializer.Serialize<DayTime>(item);
|
||||
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
try {
|
||||
HttpClient client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
||||
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
||||
HttpResponseMessage? response = null;
|
||||
if (isNewItem)
|
||||
response = await client.PostAsync(url, content);
|
||||
else
|
||||
response = await client.PutAsync(url, content);
|
||||
|
||||
HttpResponseMessage response = await client.DeleteAsync(url);
|
||||
//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);
|
||||
//}
|
||||
//}
|
||||
}
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
Debug.WriteLine(@"\tTodoItem successfully deleted.");
|
||||
} 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) }) {
|
||||
|
||||
public static async Task<User> AuthUserPass(string user, string pass, string url) {
|
||||
//try {
|
||||
HttpClient client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
||||
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
||||
|
||||
var values = new Dictionary<string, string>
|
||||
{
|
||||
{ "user", user },
|
||||
{ "pass", pass }
|
||||
};
|
||||
HttpResponseMessage response = await client.DeleteAsync(url);
|
||||
|
||||
var content = new FormUrlEncodedContent(values);
|
||||
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);
|
||||
//}
|
||||
//}
|
||||
}
|
||||
|
||||
using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
|
||||
public static async Task<User> AuthUserPass(string user, string pass, string url) {
|
||||
|
||||
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
||||
var values = new Dictionary<string, string>
|
||||
{
|
||||
{ "user", user },
|
||||
{ "pass", pass }
|
||||
};
|
||||
|
||||
// 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
|
||||
var content = new FormUrlEncodedContent(values);
|
||||
|
||||
string responseData = await HttpContent.ReadAsStringAsync();
|
||||
User userData = System.Text.Json.JsonSerializer.Deserialize<User>(responseData) ?? throw new Exception("Fehler beim Deserialisieren der Daten");
|
||||
return userData;
|
||||
}
|
||||
}
|
||||
}
|
||||
using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(15) }) {
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user