summary refs log tree commit diff
path: root/testFrontend
diff options
context:
space:
mode:
Diffstat (limited to 'testFrontend')
-rw-r--r--testFrontend/SafeNSound.Frontend/Pages/Auth.razor92
-rw-r--r--testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs63
2 files changed, 115 insertions, 40 deletions
diff --git a/testFrontend/SafeNSound.Frontend/Pages/Auth.razor b/testFrontend/SafeNSound.Frontend/Pages/Auth.razor

index 5540f02..20625f7 100644 --- a/testFrontend/SafeNSound.Frontend/Pages/Auth.razor +++ b/testFrontend/SafeNSound.Frontend/Pages/Auth.razor
@@ -3,13 +3,13 @@ <h1>Auth</h1> <u>User:</u><br/> <span>Username (L?, R): </span> -<FancyTextBox @bind-Value="@Username"/><br/> +<FancyTextBox @bind-Value="@AuthData.Username"/><br/> <span>Email (L? R): </span> -<FancyTextBox @bind-Value="@Email"/><br/> +<FancyTextBox @bind-Value="@AuthData.Email"/><br/> <span>Password (L, R): </span> -<FancyTextBox @bind-Value="@Password" IsPassword="true"/><br/> +<FancyTextBox @bind-Value="@AuthData.Password" IsPassword="true"/><br/> <span>Type (R): </span> -<FancyTextBox @bind-Value="@UserType"/><span> (one of user|monitor|admin)</span><br/> +<FancyTextBox @bind-Value="@AuthData.UserType"/><span> (one of user|monitor|admin)</span><br/> <LinkButton OnClick="@Randomise">Randomise</LinkButton> <LinkButton OnClick="@Register">Register</LinkButton> <LinkButton OnClick="@Login">Login</LinkButton> @@ -24,6 +24,20 @@ <LinkButton OnClick="@GetAssignedUsers">Get</LinkButton> <LinkButton OnClick="@AddAssignedUser">Add</LinkButton> <LinkButton OnClick="@RemoveAssignedUser">Remove</LinkButton> +<br/><br/> + +<u>Devices:</u><br/> +@if (CurrentDevice is not null) { + <span>Device ID: @CurrentDevice.Id</span><br/> + <span>Log in date: @CurrentDevice.CreatedAt</span><br/> + <span>Last seen: @CurrentDevice.LastSeen</span><br/> + <span>Device name: </span> + <FancyTextBox @bind-Value="@CurrentDevice.Name"/><br/> + <LinkButton OnClick="@GetDevice">Get</LinkButton> + <LinkButton OnClick="@UpdateDevice">Update</LinkButton> + <LinkButton OnClick="@DeleteDevice">Delete</LinkButton> + <LinkButton OnClick="@GetAllDevices">Get all</LinkButton> +} @if (Exception != null) { <div class="alert alert-danger"> @@ -44,10 +58,15 @@ } @code { - private string Username { get; set; } = string.Empty; - private string Email { get; set; } = string.Empty; - private string Password { get; set; } = string.Empty; - private string UserType { get; set; } = string.Empty; + + private RegisterDto AuthData { get; set; } = new() { + Username = string.Empty, + UserType = string.Empty, + Email = String.Empty, + Password = string.Empty + }; + + private DeviceDto? CurrentDevice { get; set; } private string TargetUserId { get; set; } = string.Empty; @@ -55,10 +74,10 @@ private object? Result { get; set; } private async Task Randomise() { - Username = Guid.NewGuid().ToString(); - Email = Guid.NewGuid() + "@example.com"; - Password = Guid.NewGuid().ToString(); - UserType = Random.Shared.GetItems(["user", "monitor", "admin"], 1)[0]; + AuthData.Username = Guid.NewGuid().ToString(); + AuthData.Email = Guid.NewGuid() + "@example.com"; + AuthData.Password = Guid.NewGuid().ToString(); + AuthData.UserType = Random.Shared.GetItems(["user", "monitor", "admin"], 1)[0]; StateHasChanged(); } @@ -67,10 +86,10 @@ Exception = null; try { await Authentication.Register(new() { - Username = Username, - Password = Password, - Email = Email, - UserType = UserType + Username = AuthData.Username, + Password = AuthData.Password, + Email = AuthData.Email, + UserType = AuthData.UserType }); } catch (Exception ex) { @@ -86,11 +105,14 @@ try { AuthResult result; Result = result = await Authentication.Login(new() { - Username = Username, - Password = Password, - Email = Email + Username = AuthData.Username, + Password = AuthData.Password, + Email = AuthData.Email }); App.Client = new SafeNSoundClient(Config, result.AccessToken); + CurrentDevice = await App.Client.GetDevice( + (await App.Client.WhoAmI()).DeviceId + ); } catch (Exception ex) { Exception = ex; @@ -104,9 +126,9 @@ Exception = null; try { await Authentication.Delete(new() { - Username = Username, - Password = Password, - Email = Email + Username = AuthData.Username, + Password = AuthData.Password, + Email = AuthData.Email }); } catch (Exception ex) { @@ -125,6 +147,7 @@ catch (Exception ex) { Exception = ex; } + StateHasChanged(); } @@ -137,6 +160,7 @@ catch (Exception ex) { Exception = ex; } + StateHasChanged(); } @@ -150,6 +174,7 @@ catch (Exception ex) { Exception = ex; } + StateHasChanged(); } @@ -163,6 +188,7 @@ catch (Exception ex) { Exception = ex; } + StateHasChanged(); } @@ -185,6 +211,28 @@ catch (Exception ex) { Exception = ex; } + + StateHasChanged(); + } + + private async Task GetDevice() { + Result = CurrentDevice = await App.Client!.GetDevice(CurrentDevice!.Id!); + StateHasChanged(); + } + + private async Task UpdateDevice() { + await App.Client!.UpdateDevice(CurrentDevice!.Id!, new() { + Name = CurrentDevice.Name + }); + await GetDevice(); + } + + private async Task DeleteDevice() { + await App.Client!.DeleteDevice(CurrentDevice!.Id!); + } + + private async Task GetAllDevices() { + Result = await App.Client!.GetDevices(); StateHasChanged(); } diff --git a/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs b/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs
index 8291178..9ea8073 100644 --- a/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs +++ b/testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs
@@ -4,24 +4,20 @@ using System.Text.Json.Serialization; namespace SafeNSound.Sdk; -public class SafeNSoundClient(SafeNSoundConfiguration config, string accessToken) -{ - public WrappedHttpClient HttpClient { get; } = new() - { +public class SafeNSoundClient(SafeNSoundConfiguration config, string accessToken) { + public WrappedHttpClient HttpClient { get; } = new() { BaseAddress = new Uri(config.BaseUri), - DefaultRequestHeaders = - { + DefaultRequestHeaders = { Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken) } }; - - public async Task<WhoAmI> WhoAmI() - { + + public async Task<WhoAmI> WhoAmI() { var res = await HttpClient.GetAsync("/auth/whoami"); res.EnsureSuccessStatusCode(); return (await res.Content.ReadFromJsonAsync<WhoAmI>())!; } - + #region Alarm public async Task<AlarmDto> GetAlarm(string userId = "@me") { @@ -29,25 +25,21 @@ public class SafeNSoundClient(SafeNSoundConfiguration config, string accessToken res.EnsureSuccessStatusCode(); return (await res.Content.ReadFromJsonAsync<AlarmDto>())!; } - + public async Task SetAlarm(AlarmDto alarm, string userId = "@me") { var res = await HttpClient.PutAsJsonAsync("/alarm/@me", alarm); res.EnsureSuccessStatusCode(); } - + public async Task DeleteAlarm(string userId = "@me") { var res = await HttpClient.DeleteAsync($"/alarm/{userId}"); res.EnsureSuccessStatusCode(); } - - #endregion #region Budget - - #endregion public async Task<Dictionary<string, AlarmDto>> GetAllAlarms() { @@ -89,13 +81,48 @@ public class SafeNSoundClient(SafeNSoundConfiguration config, string accessToken var res = await HttpClient.PostAsync("/admin/monitorAllUsers", null); res.EnsureSuccessStatusCode(); } -} + public async Task<List<DeviceDto>> GetDevices() { + var res = await HttpClient.GetAsync("/auth/devices"); + res.EnsureSuccessStatusCode(); + return (await res.Content.ReadFromJsonAsync<List<DeviceDto>>())!; + } + + public async Task<DeviceDto> GetDevice(string deviceId) { + var res = await HttpClient.GetAsync($"/auth/devices/{deviceId}"); + res.EnsureSuccessStatusCode(); + return (await res.Content.ReadFromJsonAsync<DeviceDto>())!; + } + + public async Task DeleteDevice(string deviceId) { + var res = await HttpClient.DeleteAsync($"/auth/devices/{deviceId}"); + res.EnsureSuccessStatusCode(); + } + + public async Task UpdateDevice(string deviceId, DeviceDto device) { + var res = await HttpClient.PatchAsJsonAsync($"/auth/devices/{deviceId}", device); + res.EnsureSuccessStatusCode(); + } +} public class AlarmDto { [JsonPropertyName("reason")] public required string Reason { get; set; } - + [JsonPropertyName("createdAt")] public DateTime CreatedAt { get; set; } +} + +public class DeviceDto { + [JsonPropertyName("_id")] + public string? Id { get; set; } + + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("createdAt")] + public DateTime CreatedAt { get; set; } + + [JsonPropertyName("lastSeen")] + public DateTime LastSeen { get; set; } } \ No newline at end of file