WASM Mode activated
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
using System.Net.Http.Json;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using timetracker.Shared;
|
||||
|
||||
namespace timetracker.Client.Services;
|
||||
|
||||
public class ClientAuthService : IAuthService
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
private readonly AuthenticationStateProvider _authStateProvider;
|
||||
|
||||
public ClientAuthService(HttpClient http, AuthenticationStateProvider authStateProvider)
|
||||
{
|
||||
_http = http;
|
||||
_authStateProvider = authStateProvider;
|
||||
}
|
||||
|
||||
private HostAuthenticationStateProvider HostProvider => (HostAuthenticationStateProvider)_authStateProvider;
|
||||
|
||||
public async Task<User?> LoginAsync(string username, string password)
|
||||
{
|
||||
var response = await _http.PostAsJsonAsync("api/auth/login", new { Username = username, Password = password });
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var userInfo = await response.Content.ReadFromJsonAsync<UserInfo>();
|
||||
if (userInfo != null)
|
||||
{
|
||||
HostProvider.NotifyUserChanged(userInfo);
|
||||
return new User { Id = userInfo.Id, Username = userInfo.Username };
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<(User? User, string? Error)> RegisterAsync(string username, string password)
|
||||
{
|
||||
var response = await _http.PostAsJsonAsync("api/auth/register", new { Username = username, Password = password });
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var userInfo = await response.Content.ReadFromJsonAsync<UserInfo>();
|
||||
if (userInfo != null)
|
||||
{
|
||||
HostProvider.NotifyUserChanged(userInfo);
|
||||
return (new User { Id = userInfo.Id, Username = userInfo.Username }, null);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var error = await response.Content.ReadAsStringAsync();
|
||||
return (null, error);
|
||||
}
|
||||
return (null, "Registrierung fehlgeschlagen.");
|
||||
}
|
||||
|
||||
public async Task<List<User>> GetAllUsersAsync()
|
||||
{
|
||||
return await _http.GetFromJsonAsync<List<User>>("api/users") ?? [];
|
||||
}
|
||||
|
||||
public async Task DeleteUserAsync(int userId)
|
||||
{
|
||||
await _http.DeleteAsync($"api/users/{userId}");
|
||||
}
|
||||
|
||||
public async Task<string?> RenameUserAsync(int userId, string newUsername)
|
||||
{
|
||||
var response = await _http.PutAsJsonAsync($"api/users/{userId}/rename", new { Username = newUsername });
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using System.Net.Http.Json;
|
||||
using timetracker.Shared;
|
||||
|
||||
namespace timetracker.Client.Services;
|
||||
|
||||
public class ClientHolidayService : IHolidayService
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
|
||||
public ClientHolidayService(HttpClient http)
|
||||
{
|
||||
_http = http;
|
||||
}
|
||||
|
||||
public async Task<List<PublicHoliday>> GetHolidaysAsync(int year, string? stateCode = null)
|
||||
{
|
||||
var url = $"api/holidays?year={year}";
|
||||
if (!string.IsNullOrEmpty(stateCode))
|
||||
{
|
||||
url += $"&stateCode={stateCode}";
|
||||
}
|
||||
return await _http.GetFromJsonAsync<List<PublicHoliday>>(url) ?? [];
|
||||
}
|
||||
|
||||
public async Task<(bool Success, string Message)> FetchAndStoreAsync(int year)
|
||||
{
|
||||
var response = await _http.PostAsync($"api/holidays/fetch/{year}", null);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var result = await response.Content.ReadFromJsonAsync<FetchResponse>();
|
||||
if (result != null)
|
||||
{
|
||||
return (result.Success, result.Message);
|
||||
}
|
||||
}
|
||||
return (false, "Fehler beim Abrufen der Feiertage.");
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(int id)
|
||||
{
|
||||
await _http.DeleteAsync($"api/holidays/{id}");
|
||||
}
|
||||
|
||||
private class FetchResponse
|
||||
{
|
||||
public bool Success { get; set; }
|
||||
public string Message { get; set; } = "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using System.Net.Http.Json;
|
||||
using timetracker.Shared;
|
||||
|
||||
namespace timetracker.Client.Services;
|
||||
|
||||
public class ClientTimetrackerService : ITimetrackerService
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
|
||||
public ClientTimetrackerService(HttpClient http)
|
||||
{
|
||||
_http = http;
|
||||
}
|
||||
|
||||
public async Task<List<WorkDay>> GetWeekAsync(int userId, DateOnly monday)
|
||||
{
|
||||
return await _http.GetFromJsonAsync<List<WorkDay>>($"api/tracker/week?userId={userId}&monday={monday:yyyy-MM-dd}") ?? [];
|
||||
}
|
||||
|
||||
public async Task UpsertWorkDayAsync(WorkDay workDay)
|
||||
{
|
||||
await _http.PostAsJsonAsync("api/tracker/workday", workDay);
|
||||
}
|
||||
|
||||
public async Task<AppSettings> GetSettingsAsync(int userId)
|
||||
{
|
||||
return await _http.GetFromJsonAsync<AppSettings>($"api/tracker/settings/{userId}") ?? new AppSettings { UserId = userId };
|
||||
}
|
||||
|
||||
public async Task SaveSettingsAsync(AppSettings settings)
|
||||
{
|
||||
await _http.PostAsJsonAsync("api/tracker/settings", settings);
|
||||
}
|
||||
|
||||
public async Task<List<VacationDay>> GetVacationDaysAsync(int userId, int year)
|
||||
{
|
||||
return await _http.GetFromJsonAsync<List<VacationDay>>($"api/tracker/vacation/{userId}/{year}") ?? [];
|
||||
}
|
||||
|
||||
public async Task AddVacationDayAsync(VacationDay vacationDay)
|
||||
{
|
||||
await _http.PostAsJsonAsync("api/tracker/vacation", vacationDay);
|
||||
}
|
||||
|
||||
public async Task RemoveVacationDayAsync(int userId, int id)
|
||||
{
|
||||
await _http.DeleteAsync($"api/tracker/vacation/{userId}/{id}");
|
||||
}
|
||||
|
||||
public async Task<TimeSpan> GetTotalOvertimeAsync(int userId, AppSettings settings)
|
||||
{
|
||||
var response = await _http.PostAsJsonAsync($"api/tracker/overtime/{userId}", settings);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var hours = await response.Content.ReadFromJsonAsync<double>();
|
||||
return TimeSpan.FromHours(hours);
|
||||
}
|
||||
return TimeSpan.Zero;
|
||||
}
|
||||
|
||||
public async Task<List<WorkDay>> GetMonthAsync(int userId, int year, int month)
|
||||
{
|
||||
return await _http.GetFromJsonAsync<List<WorkDay>>($"api/tracker/month?userId={userId}&year={year}&month={month}") ?? [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
using System.Security.Claims;
|
||||
using System.Net.Http.Json;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using timetracker.Shared;
|
||||
|
||||
namespace timetracker.Client.Services;
|
||||
|
||||
public class HostAuthenticationStateProvider : AuthenticationStateProvider
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
private static readonly ClaimsPrincipal Anonymous = new(new ClaimsIdentity());
|
||||
private ClaimsPrincipal? _currentUser;
|
||||
|
||||
public HostAuthenticationStateProvider(HttpClient http)
|
||||
{
|
||||
_http = http;
|
||||
}
|
||||
|
||||
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
|
||||
{
|
||||
if (_currentUser != null)
|
||||
{
|
||||
return new AuthenticationState(_currentUser);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var response = await _http.GetAsync("api/auth/me");
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var userInfo = await response.Content.ReadFromJsonAsync<UserInfo>();
|
||||
if (userInfo != null)
|
||||
{
|
||||
var claims = new[]
|
||||
{
|
||||
new Claim(ClaimTypes.NameIdentifier, userInfo.Id.ToString()),
|
||||
new Claim(ClaimTypes.Name, userInfo.Username)
|
||||
};
|
||||
var identity = new ClaimsIdentity(claims, "Cookie");
|
||||
_currentUser = new ClaimsPrincipal(identity);
|
||||
return new AuthenticationState(_currentUser);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore error and fall back to anonymous (e.g. server offline or network issues)
|
||||
}
|
||||
|
||||
_currentUser = Anonymous;
|
||||
return new AuthenticationState(_currentUser);
|
||||
}
|
||||
|
||||
public void NotifyUserChanged(UserInfo? userInfo)
|
||||
{
|
||||
if (userInfo != null)
|
||||
{
|
||||
var claims = new[]
|
||||
{
|
||||
new Claim(ClaimTypes.NameIdentifier, userInfo.Id.ToString()),
|
||||
new Claim(ClaimTypes.Name, userInfo.Username)
|
||||
};
|
||||
var identity = new ClaimsIdentity(claims, "Cookie");
|
||||
_currentUser = new ClaimsPrincipal(identity);
|
||||
}
|
||||
else
|
||||
{
|
||||
_currentUser = Anonymous;
|
||||
}
|
||||
|
||||
NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(_currentUser)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
using timetracker.Shared;
|
||||
|
||||
namespace timetracker.Client.Services;
|
||||
|
||||
public class UserNotificationService : IUserNotificationService, IAsyncDisposable
|
||||
{
|
||||
private readonly HubConnection _hubConnection;
|
||||
|
||||
public event Func<Task>? OnUsersChanged;
|
||||
public event Func<int, Task>? OnUserDeleted;
|
||||
|
||||
public UserNotificationService(NavigationManager navigationManager)
|
||||
{
|
||||
_hubConnection = new HubConnectionBuilder()
|
||||
.WithUrl(navigationManager.ToAbsoluteUri("/hubs/notifications"))
|
||||
.WithAutomaticReconnect()
|
||||
.Build();
|
||||
|
||||
_hubConnection.On("UsersChanged", async () =>
|
||||
{
|
||||
if (OnUsersChanged != null)
|
||||
await OnUsersChanged.Invoke();
|
||||
});
|
||||
|
||||
_hubConnection.On<int>("UserDeleted", async (userId) =>
|
||||
{
|
||||
if (OnUserDeleted != null)
|
||||
await OnUserDeleted.Invoke(userId);
|
||||
});
|
||||
|
||||
// Start connection asynchronously
|
||||
_ = StartHubConnectionAsync();
|
||||
}
|
||||
|
||||
private async Task StartHubConnectionAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
await _hubConnection.StartAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"SignalR Connection Error: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_hubConnection != null)
|
||||
{
|
||||
await _hubConnection.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user