Skip to content

Commit

Permalink
Add user overview in the admin module
Browse files Browse the repository at this point in the history
  • Loading branch information
oveldman committed Jan 31, 2024
1 parent 2476df1 commit 290ad79
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ namespace MadWorld.Backend.Identity.Contracts.UserManagers;

public sealed class GetUsersResponse
{
public int TotalCount { get; set; }
public IReadOnlyList<UserContract> Users { get; set; } = Array.Empty<UserContract>();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MadWorld.Backend.Identity.Application.Mappers;
using MadWorld.Backend.Identity.Contracts.UserManagers;
using MadWorld.Backend.Identity.Domain.Users;
using MadWorld.Backend.Identity.Infrastructure;

namespace MadWorld.Backend.Identity.Application;
Expand All @@ -17,9 +18,12 @@ public GetUsersResponse GetUsers(int page)
{
ArgumentOutOfRangeException.ThrowIfNegative(page);

var totalCount = _repository.CountUsers();
var users = _repository.GetUsers(page);

return new GetUsersResponse()
{
TotalCount = totalCount,
Users = users.ToDto()
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace MadWorld.Backend.Identity.Infrastructure;

public interface IUserRepository
{
int CountUsers();
List<IdentityUserExtended> GetUsers(int page);
Task AddRefreshToken(RefreshToken token);
Task<int> DeleteExpiredRefreshTokens();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace MadWorld.Backend.Identity.Infrastructure;

public class UserRepository : IUserRepository
{
private int TakeAmount = 10;
private const int TakeAmount = 10;

private readonly UserDbContext _context;
private readonly IDateTimeProvider _dateTimeProvider;

Expand All @@ -17,6 +17,13 @@ public UserRepository(UserDbContext context, IDateTimeProvider dateTimeProvider)
_dateTimeProvider = dateTimeProvider;
}

public int CountUsers()
{
return _context
.Users
.Count();
}

public List<IdentityUserExtended> GetUsers(int page)
{
return _context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public async Task Execute_WhenRequestIsValid_ShouldReturnValidResponse()
response.EnsureSuccessStatusCode();
var result = await response.Content
.ReadFromJsonAsync<GetUsersResponse>();
result!.TotalCount.ShouldBe(13);
result!.Users.Count.ShouldBe(10);
result!.Users[1].Email.ShouldBe($"[email protected]");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using MadWorld.Backend.Identity.Contracts.UserManagers;

namespace MadWorld.Frontend.Admin.Application.Users;

public interface IUserService
{
Task<GetUsersResponse> GetUsers(int page);
}
28 changes: 28 additions & 0 deletions MadWorld/MadWorld.Frontend.Admin/Application/Users/UserService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Net.Http.Json;
using MadWorld.Backend.Identity.Contracts.UserManagers;
using MadWorld.Shared.Blazor.Common;

namespace MadWorld.Frontend.Admin.Application.Users;

public class UserService : IUserService
{
private const string Endpoint = "UserManager";

private readonly HttpClient _client;
public UserService(IHttpClientFactory clientFactory)
{
_client = clientFactory.CreateClient(ApiTypes.Identity);
}

public async Task<GetUsersResponse> GetUsers(int page)
{
var response = await _client.GetAsync($"{Endpoint}/Users?page={page}");

if (!response.IsSuccessStatusCode)
{
return new GetUsersResponse();
}

return await response.Content.ReadFromJsonAsync<GetUsersResponse>() ?? new GetUsersResponse();
}
}
19 changes: 19 additions & 0 deletions MadWorld/MadWorld.Frontend.Admin/Application/Users/Users.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@using MadWorld.Backend.Identity.Contracts.UserManagers;

@page "/Users"
<div class="row">
<h3>All Users</h3>
</div>
<div class="row">
<RadzenDataGrid AllowFiltering="false" AllowSorting="false" AllowPaging="true"
AllowVirtualization="true" Style="height:600px"
PageSize="10" Count="@_response.TotalCount" Page="OnPage"
LoadData="LoadData"
PagerPosition="PagerPosition.Bottom"
PagerHorizontalAlign="HorizontalAlign.Center"
ShowPagingSummary="true" Data="@_response.Users" TItem="UserContract">
<Columns>
<RadzenDataGridColumn TItem="UserContract" Property="Email" Title="Email" Width="150px" />
</Columns>
</RadzenDataGrid>
</div>
38 changes: 38 additions & 0 deletions MadWorld/MadWorld.Frontend.Admin/Application/Users/Users.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using MadWorld.Backend.Identity.Contracts.UserManagers;
using Microsoft.AspNetCore.Components;
using Radzen;

namespace MadWorld.Frontend.Admin.Application.Users;

public partial class Users
{
[Inject]
public IUserService UserService { get; set; } = null!;

private GetUsersResponse _response = new();

private int _currentPage = 0;

protected override async Task OnInitializedAsync()
{
await LoadUsers();

await base.OnInitializedAsync();
}

private Task LoadData(LoadDataArgs _) => Task.CompletedTask;

private async Task OnPage(PagerEventArgs args)
{
if (_currentPage != args.PageIndex)
{
_currentPage = args.PageIndex;
await LoadUsers();
}
}

private async Task LoadUsers()
{
_response = await UserService.GetUsers(_currentPage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using MadWorld.Frontend.Admin.Application.Users;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

namespace MadWorld.Frontend.Admin.Extensions;

public static class WebAssemblyHostBuilderExtensions
{
public static void AddApplication(this WebAssemblyHostBuilder builder)
{
builder.Services.AddScoped<IUserService, UserService>();
}
}
6 changes: 5 additions & 1 deletion MadWorld/MadWorld.Frontend.Admin/Layout/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
<span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Home
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="/Users">
<span class="bi bi-people-fill-nav-menu" aria-hidden="true"></span> Users
</NavLink>
</div>
</nav>
</div>

Expand All @@ -26,5 +31,4 @@
{
collapseNavMenu = !collapseNavMenu;
}

}
4 changes: 4 additions & 0 deletions MadWorld/MadWorld.Frontend.Admin/Layout/NavMenu.razor.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='white' class='bi bi-house-door-fill' viewBox='0 0 16 16'%3E%3Cpath d='M6.5 14.5v-3.505c0-.245.25-.495.5-.495h2c.25 0 .5.25.5.5v3.5a.5.5 0 0 0 .5.5h4a.5.5 0 0 0 .5-.5v-7a.5.5 0 0 0-.146-.354L13 5.793V2.5a.5.5 0 0 0-.5-.5h-1a.5.5 0 0 0-.5.5v1.293L8.354 1.146a.5.5 0 0 0-.708 0l-6 6A.5.5 0 0 0 1.5 7.5v7a.5.5 0 0 0 .5.5h4a.5.5 0 0 0 .5-.5Z'/%3E%3C/svg%3E");
}

.bi-people-fill-nav-menu {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='white' class='bi bi-people-fill' viewBox='0 0 16 16'%3E%3Cpath d='M7 14s-1 0-1-1 1-4 5-4 5 3 5 4-1 1-1 1zm4-6a3 3 0 1 0 0-6 3 3 0 0 0 0 6m-5.784 6A2.24 2.24 0 0 1 5 13c0-1.355.68-2.75 1.936-3.72A6.3 6.3 0 0 0 5 9c-4 0-5 3-5 4s1 1 1 1zM4.5 8a2.5 2.5 0 1 0 0-5 2.5 2.5 0 0 0 0 5'/%3E%3C/svg%3E");
}

.bi-plus-square-fill-nav-menu {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='white' class='bi bi-plus-square-fill' viewBox='0 0 16 16'%3E%3Cpath d='M2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2H2zm6.5 4.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3a.5.5 0 0 1 1 0z'/%3E%3C/svg%3E");
}
Expand Down
2 changes: 2 additions & 0 deletions MadWorld/MadWorld.Frontend.Admin/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using MadWorld.Frontend.Admin;
using MadWorld.Frontend.Admin.Extensions;
using MadWorld.Shared.Blazor.Authentications;
using MadWorld.Shared.Blazor.Common;

Expand All @@ -10,6 +11,7 @@

builder.AddCommon();
builder.AddAuthentication();
builder.AddApplication();

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

Expand Down
3 changes: 3 additions & 0 deletions MadWorld/MadWorld.Frontend.Admin/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.AspNetCore.Components.WebAssembly.Http
@using Microsoft.JSInterop
@using Radzen;
@using Radzen.Blazor;

@using MadWorld.Frontend.Admin
@using MadWorld.Frontend.Admin.Layout

0 comments on commit 290ad79

Please sign in to comment.