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 +++++++++++++++++------ testFrontend/SafeNSound.Sdk/SafeNSoundClient.cs | 63 +++++++++++----- 2 files changed, 115 insertions(+), 40 deletions(-) (limited to 'testFrontend') 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(); } 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() - { + + public async Task WhoAmI() { var res = await HttpClient.GetAsync("/auth/whoami"); res.EnsureSuccessStatusCode(); return (await res.Content.ReadFromJsonAsync())!; } - + #region Alarm public async Task GetAlarm(string userId = "@me") { @@ -29,25 +25,21 @@ public class SafeNSoundClient(SafeNSoundConfiguration config, string accessToken res.EnsureSuccessStatusCode(); return (await res.Content.ReadFromJsonAsync())!; } - + 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> 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> GetDevices() { + var res = await HttpClient.GetAsync("/auth/devices"); + res.EnsureSuccessStatusCode(); + return (await res.Content.ReadFromJsonAsync>())!; + } + + public async Task GetDevice(string deviceId) { + var res = await HttpClient.GetAsync($"/auth/devices/{deviceId}"); + res.EnsureSuccessStatusCode(); + return (await res.Content.ReadFromJsonAsync())!; + } + + 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 -- cgit 1.5.1