diff --git a/Models/Account.cs b/Models/Account.cs index 47b163e..4c02b81 100644 --- a/Models/Account.cs +++ b/Models/Account.cs @@ -6,4 +6,9 @@ public class User public required string Email { get; set; } public uint Id { get; set; } public bool IsAdmin { get; set; } + + public override string ToString() + { + return $"{nameof(Name)}: {Name}, {nameof(Email)}: {Email}, {nameof(Id)}: {Id}, {nameof(IsAdmin)}: {IsAdmin}"; + } } \ No newline at end of file diff --git a/Pages/UserListPanel.razor b/Pages/UserListPanel.razor index b03faff..51834a3 100644 --- a/Pages/UserListPanel.razor +++ b/Pages/UserListPanel.razor @@ -7,22 +7,22 @@

User Panel

- + - - + @@ -33,15 +33,7 @@ -
- - Add Account -
-
- - Remove Selection -
- + + + +
+ + Add Account +
+
+ + Remove Selection +
+ + Accounts + + +
+ diff --git a/Pages/UserListPanel.razor.cs b/Pages/UserListPanel.razor.cs index 7d4904d..c9ebb9b 100644 --- a/Pages/UserListPanel.razor.cs +++ b/Pages/UserListPanel.razor.cs @@ -8,11 +8,15 @@ namespace AdminPanel.Pages { public partial class UserListPanel { - [Inject] public ISnackbar Snackbar { get; private init; } - [Inject] public IUsersService Service { get; private init; } + [Inject] private ISnackbar Snackbar { get; init; } + [Inject] private IUsersService Service { get; init; } + [Inject] private ILogger Logger { get; init; } private MudDataGrid Grid { get; set; } + + private string? SearchString { get; set; } + private HashSet SelectedItems { get; set; } = new(); @@ -26,7 +30,8 @@ namespace AdminPanel.Pages private async Task> LoadUsersFromServer(GridState state) { - var (count, users) = await Service.ListUsers((uint)(state.Page * state.PageSize), (uint)state.PageSize); + Logger.LogDebug($"Loading users from server, state = {state} searchString = {SearchString}"); + var (count, users) = await Service.ListUsers((uint)(state.Page * state.PageSize), (uint)state.PageSize, SearchString); return new GridData { TotalItems = (int)count, @@ -37,7 +42,7 @@ namespace AdminPanel.Pages private async void OnAccountUpdated(User user) { - Console.WriteLine(user.IsAdmin); + Logger.LogDebug($"Account updated : {user}"); try { await Service.UpdateUser(user); @@ -56,8 +61,9 @@ namespace AdminPanel.Pages try { - await Service.AddUser(FormAccountName!, FormAccountEmail!, FormAccountPassword!, + var user = await Service.AddUser(FormAccountName!, FormAccountEmail!, FormAccountPassword!, FormAccountIsAdmin); + Logger.LogDebug($"Added user : {user}"); await Grid.ReloadServerData(); } catch (Exception) @@ -68,11 +74,11 @@ namespace AdminPanel.Pages private async void RemoveSelection(MouseEventArgs e) { - var items = SelectedItems.ToList().ConvertAll(u => u.Id); - Console.WriteLine(items.Count); + var users = SelectedItems.ToList().ConvertAll(u => u.Id); + Logger.LogDebug($"Removing users : {users}"); try { - await Service.RemoveUsers(items); + await Service.RemoveUsers(users); await Grid.ReloadServerData(); } catch (Exception) @@ -83,7 +89,16 @@ namespace AdminPanel.Pages private Func VerifyLength(uint min, uint max) { - return s => (s.Length >= min && s.Length <= max) ? null : $"length is incorrect (must be between {min} and {max})"; + return s => s.Length >= min && s.Length <= max ? null : $"length is incorrect (must be between {min} and {max})"; + } + + private void ValidateSearch(KeyboardEventArgs e) + { + if (e.Key == "Enter") + { + Grid.ReloadServerData(); + Logger.LogDebug($"Searching for {SearchString}"); + } } } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index e633b92..b8036fc 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,5 @@ using AdminPanel; +using AdminPanel.Pages; using AdminPanel.Services; using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; @@ -11,10 +12,11 @@ builder.RootComponents.Add("head::after"); builder.Services .AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); +builder.Logging.SetMinimumLevel(LogLevel.Debug); + var client = new HttpClient { BaseAddress = new Uri("http://localhost:8080") - }; builder.Services.AddScoped(sp => new HttpUsersService(client)); diff --git a/Services/HttpUsersService.cs b/Services/HttpUsersService.cs index cf3bccf..f7621f7 100644 --- a/Services/HttpUsersService.cs +++ b/Services/HttpUsersService.cs @@ -14,9 +14,9 @@ public class HttpUsersService : IUsersService private record ListUsersResponse(List Users, uint TotalCount); - public async Task<(uint, List)> ListUsers(uint from, uint len) + public async Task<(uint, List)> ListUsers(uint from, uint len, string? searchString = null) { - var httpResponse = await Client.GetAsync($"/api/admin/list-users?start={from}&n={len}"); + var httpResponse = await Client.GetAsync($"/api/admin/list-users?start={from}&n={len}&search={searchString ?? ""}"); httpResponse.EnsureSuccessStatusCode(); var response = await httpResponse.Content.ReadFromJsonAsync() diff --git a/Services/IUsersService.cs b/Services/IUsersService.cs index 8611121..c523caf 100644 --- a/Services/IUsersService.cs +++ b/Services/IUsersService.cs @@ -4,7 +4,7 @@ namespace AdminPanel.Services { public interface IUsersService { - public Task<(uint, List)> ListUsers(uint from, uint len); + public Task<(uint, List)> ListUsers(uint from, uint len, string? searchString = null); public Task AddUser(string username, string email, string password, bool isAdmin); diff --git a/Services/UsersServiceStub.cs b/Services/UsersServiceStub.cs index 59958e8..d30845e 100644 --- a/Services/UsersServiceStub.cs +++ b/Services/UsersServiceStub.cs @@ -1,5 +1,4 @@ using AdminPanel.Models; -using System.Runtime.CompilerServices; namespace AdminPanel.Services { @@ -36,13 +35,14 @@ namespace AdminPanel.Services return Task.FromResult(user); } - public async Task<(uint, List)> ListUsers(uint from, uint len) + public async Task<(uint, List)> ListUsers(uint from, uint len, string? searchString = null) { //simulate a 1sec long request await Task.Delay(1000); var slice = Users.Values .ToList() - .GetRange((int)from, (int)((from + len > Users.Count) ? Users.Count - from : len)); + .FindAll(a => searchString == null || a.Name.Contains(searchString) || a.Email.Contains(searchString)) + .GetRange((int)from, (int)(from + len > Users.Count ? Users.Count - from : len)); return ((uint)Users.Count, slice); }