From a104cdfc8a1a362134b1fc7ff8f5a5a780465771 Mon Sep 17 00:00:00 2001 From: Rory& Date: Tue, 3 Jun 2025 16:20:05 +0200 Subject: CRUD devices --- testFrontend/SafeNSound.Frontend/Pages/Auth.razor | 92 +++++++++++++++++------ 1 file changed, 70 insertions(+), 22 deletions(-) (limited to 'testFrontend/SafeNSound.Frontend') 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 @@

Auth

User:
Username (L?, R): -
+
Email (L? R): -
+
Password (L, R): -
+
Type (R): - (one of user|monitor|admin)
+ (one of user|monitor|admin)
Randomise Register Login @@ -24,6 +24,20 @@ Get Add Remove +

+ +Devices:
+@if (CurrentDevice is not null) { + Device ID: @CurrentDevice.Id
+ Log in date: @CurrentDevice.CreatedAt
+ Last seen: @CurrentDevice.LastSeen
+ Device name: +
+ Get + Update + Delete + Get all +} @if (Exception != null) {
@@ -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(); } -- cgit 1.5.1