Refactor: Remove GlobalVar and replace with IAppSettings; restructure affected infrastructure, services, and view models for dependency injection.
This commit is contained in:
@@ -100,82 +100,7 @@ internal static class BaseFunc {
|
||||
new() { 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 - außer bei Freistellungen, da wäre eigentlich null
|
||||
if (item.TimeSpanVon == item.TimeSpanBis && item.FreistellungAktiv == null) {
|
||||
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 && GlobalVar.Settings.GemeindeAktivSet) {
|
||||
throw new Exception("Gemeinde nicht gewählt");
|
||||
}
|
||||
|
||||
//Projekt ist ein Pflichtfeld
|
||||
if (item.ProjektAktiv == null && GlobalVar.Settings.ProjektAktivSet) {
|
||||
throw new Exception("Projekt nicht gewählt");
|
||||
}
|
||||
|
||||
//Keine Beschreibung
|
||||
if (string.IsNullOrEmpty(item.Description) && item.FreistellungAktiv == null) {
|
||||
throw new Exception("Keine Beschreibung");
|
||||
}
|
||||
|
||||
//Keine Beschreibung
|
||||
if (string.IsNullOrEmpty(item.Description)) {
|
||||
item.Description = item.FreistellungAktiv.Name;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stundeneintrag löschen
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
using Jugenddienst_Stunden.Types;
|
||||
|
||||
namespace Jugenddienst_Stunden.Models;
|
||||
|
||||
internal static class GlobalVar {
|
||||
public static string ApiKey {
|
||||
get => Preferences.Default.Get("apiKey", "");
|
||||
set => Preferences.Default.Set("apiKey", value);
|
||||
}
|
||||
|
||||
public static int EmployeeId {
|
||||
get => Preferences.Default.Get("EmployeeId", 0);
|
||||
set => Preferences.Default.Set("EmployeeId", value);
|
||||
}
|
||||
|
||||
public static string Name {
|
||||
get => Preferences.Default.Get("name", "Nicht");
|
||||
set => Preferences.Default.Set("name", value);
|
||||
}
|
||||
|
||||
public static string Surname {
|
||||
get => Preferences.Default.Get("surname", "Eingeloggt");
|
||||
set => Preferences.Default.Set("surname", value);
|
||||
}
|
||||
|
||||
public static string ApiUrl {
|
||||
get => Preferences.Default.Get("apiUrl", "");
|
||||
set => Preferences.Default.Set("apiUrl", value);
|
||||
}
|
||||
|
||||
public static Settings Settings { get; set; }
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
using Jugenddienst_Stunden.Types;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Jugenddienst_Stunden.Models;
|
||||
|
||||
internal static class HoursBase {
|
||||
/// <summary>
|
||||
/// Laden ... what can be: "settings", "hours", date="YYYY-MM-DD", id=<int/>
|
||||
/// </summary>
|
||||
/// <returns>Entire response</returns>
|
||||
internal static async Task<BaseResponse> LoadBase(string what) {
|
||||
string data = await BaseFunc.GetApiDataWithAuthAsync(GlobalVar.ApiUrl + "?" + what, GlobalVar.ApiKey);
|
||||
BaseResponse res = JsonConvert.DeserializeObject<BaseResponse>(data) ??
|
||||
throw new Exception("Fehler beim Deserialisieren der Daten");
|
||||
return res;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Einstellungen laden
|
||||
/// </summary>
|
||||
/// <returns>Settings only</returns>
|
||||
internal static async Task<Settings> LoadSettings() {
|
||||
string data = await BaseFunc.GetApiDataWithAuthAsync(GlobalVar.ApiUrl + "?settings", GlobalVar.ApiKey);
|
||||
BaseResponse res = JsonConvert.DeserializeObject<BaseResponse>(data) ??
|
||||
throw new Exception("Fehler beim Deserialisieren der Daten");
|
||||
return res.settings;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Daten laden
|
||||
/// </summary>
|
||||
/// <returns>Hours only</returns>
|
||||
internal static async Task<Hours> LoadData() {
|
||||
string data = await BaseFunc.GetApiDataWithAuthAsync(GlobalVar.ApiUrl + "?hours", GlobalVar.ApiKey);
|
||||
BaseResponse res = JsonConvert.DeserializeObject<BaseResponse>(data) ??
|
||||
throw new Exception("Fehler beim Deserialisieren der Daten");
|
||||
return res.hour;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Benutzerdaten laden
|
||||
/// </summary>
|
||||
/// <returns>User-Object</returns>
|
||||
public static async Task<User> LoadUser(string apiKey) {
|
||||
string data = await BaseFunc.GetApiDataWithAuthAsync(GlobalVar.ApiUrl, apiKey);
|
||||
BaseResponse res = JsonConvert.DeserializeObject<BaseResponse>(data) ??
|
||||
throw new Exception("Fehler beim Deserialisieren der Daten");
|
||||
return res.user;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Zeiten eines Tages holen
|
||||
/// </summary>
|
||||
internal static async Task<List<DayTime>> LoadDay(DateTime date) {
|
||||
string data = await BaseFunc.GetApiDataWithAuthAsync(GlobalVar.ApiUrl + "?date=" + date.ToString("yyyy-MM-dd"),
|
||||
GlobalVar.ApiKey);
|
||||
BaseResponse res = JsonConvert.DeserializeObject<BaseResponse>(data) ??
|
||||
throw new Exception("Fehler beim Deserialisieren der Daten");
|
||||
return res.daytimes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Einzelnen Stundeneintrag holen
|
||||
/// </summary>
|
||||
internal static async Task<DayTime> LoadEntry(int id) {
|
||||
string data = await BaseFunc.GetApiDataWithAuthAsync(GlobalVar.ApiUrl + "?id=" + id, GlobalVar.ApiKey);
|
||||
BaseResponse res = JsonConvert.DeserializeObject<BaseResponse>(data) ??
|
||||
throw new Exception("Fehler beim Deserialisieren der Daten");
|
||||
res.daytime.TimeSpanVon = res.daytime.Begin.ToTimeSpan();
|
||||
res.daytime.TimeSpanBis = res.daytime.End.ToTimeSpan();
|
||||
return res.daytime;
|
||||
}
|
||||
|
||||
/// <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(GlobalVar.ApiUrl, GlobalVar.ApiKey, stunde, isNew);
|
||||
|
||||
return stunde;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Eintrag löschen
|
||||
/// </summary>
|
||||
internal static async Task DeleteEntry(DayTime stunde) {
|
||||
await BaseFunc.DeleteItemAsync(GlobalVar.ApiUrl + "/entry/" + stunde.Id, GlobalVar.ApiKey);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user