From 4dcf9f2a52bd4b1ab6ef661318b195d7f142e1a4 Mon Sep 17 00:00:00 2001 From: Engincan VESKE Date: Wed, 18 Aug 2021 12:10:46 +0300 Subject: [PATCH] Admin: Create UserPicker component --- .../GetEventRegistrationListInput.cs | 10 ++ .../IEventRegistrationAppService.cs | 4 +- .../Users/GetUnregisteredUserEventInput.cs | 15 --- .../Users/GetUserListInput.cs | 9 ++ .../Users/IUserAppService.cs | 2 +- .../EventRegistrationAppService.cs | 26 +++- .../Users/UserAppService.cs | 25 ++-- .../EventRegistrationController.cs | 12 +- .../Controllers/Users/UserController.cs | 4 +- .../Components/UserPicker/UserPicker.razor | 71 ++++++++++- .../Components/UserPicker/UserPicker.razor.cs | 117 ++++++++++++++++++ .../Pages/AttendeeDetail.razor | 92 +------------- .../Pages/AttendeeDetail.razor.cs | 99 +++------------ .../Localization/EventHub/en.json | 4 +- 14 files changed, 279 insertions(+), 211 deletions(-) create mode 100644 src/EventHub.Admin.Application.Contracts/Events/Registrations/GetEventRegistrationListInput.cs delete mode 100644 src/EventHub.Admin.Application.Contracts/Users/GetUnregisteredUserEventInput.cs create mode 100644 src/EventHub.Admin.Application.Contracts/Users/GetUserListInput.cs create mode 100644 src/EventHub.Admin.Web/Components/UserPicker/UserPicker.razor.cs diff --git a/src/EventHub.Admin.Application.Contracts/Events/Registrations/GetEventRegistrationListInput.cs b/src/EventHub.Admin.Application.Contracts/Events/Registrations/GetEventRegistrationListInput.cs new file mode 100644 index 0000000..8b07bf4 --- /dev/null +++ b/src/EventHub.Admin.Application.Contracts/Events/Registrations/GetEventRegistrationListInput.cs @@ -0,0 +1,10 @@ +using System; +using Volo.Abp.Application.Dtos; + +namespace EventHub.Admin.Events.Registrations +{ + public class GetEventRegistrationListInput : PagedAndSortedResultRequestDto + { + public Guid EventId { get; set; } + } +} diff --git a/src/EventHub.Admin.Application.Contracts/Events/Registrations/IEventRegistrationAppService.cs b/src/EventHub.Admin.Application.Contracts/Events/Registrations/IEventRegistrationAppService.cs index 74d21bd..c15b793 100644 --- a/src/EventHub.Admin.Application.Contracts/Events/Registrations/IEventRegistrationAppService.cs +++ b/src/EventHub.Admin.Application.Contracts/Events/Registrations/IEventRegistrationAppService.cs @@ -8,10 +8,12 @@ namespace EventHub.Admin.Events.Registrations { public interface IEventRegistrationAppService : IApplicationService { - Task> GetAttendeesAsync(Guid eventId); + Task> GetAttendeesAsync(GetEventRegistrationListInput input); Task RemoveAttendeeAsync(Guid eventId, Guid attendeeId); Task RegisterUsersAsync(Guid eventId, List userIds); + + Task> GetAllAttendeeIdsAsync(Guid eventId); } } diff --git a/src/EventHub.Admin.Application.Contracts/Users/GetUnregisteredUserEventInput.cs b/src/EventHub.Admin.Application.Contracts/Users/GetUnregisteredUserEventInput.cs deleted file mode 100644 index 64086a9..0000000 --- a/src/EventHub.Admin.Application.Contracts/Users/GetUnregisteredUserEventInput.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using Volo.Abp.Application.Dtos; - -namespace EventHub.Admin.Users -{ - public class GetUnregisteredUserEventInput : PagedResultRequestDto - { - public Guid EventId { get; set; } - public string Username { get; set; } - public string Name { get; set; } - public string Surname { get; set; } - public string Email { get; set; } - public string Sorting { get; set; } - } -} diff --git a/src/EventHub.Admin.Application.Contracts/Users/GetUserListInput.cs b/src/EventHub.Admin.Application.Contracts/Users/GetUserListInput.cs new file mode 100644 index 0000000..9027b52 --- /dev/null +++ b/src/EventHub.Admin.Application.Contracts/Users/GetUserListInput.cs @@ -0,0 +1,9 @@ +using Volo.Abp.Application.Dtos; + +namespace EventHub.Admin.Users +{ + public class GetUserListInput : PagedAndSortedResultRequestDto + { + public string Username { get; set; } + } +} diff --git a/src/EventHub.Admin.Application.Contracts/Users/IUserAppService.cs b/src/EventHub.Admin.Application.Contracts/Users/IUserAppService.cs index c7d710e..4920226 100644 --- a/src/EventHub.Admin.Application.Contracts/Users/IUserAppService.cs +++ b/src/EventHub.Admin.Application.Contracts/Users/IUserAppService.cs @@ -7,6 +7,6 @@ namespace EventHub.Admin.Users { public interface IUserAppService : IApplicationService { - Task> GetUnregisteredUsersOfEventAsync(GetUnregisteredUserEventInput input); + Task> GetListAsync(GetUserListInput input); } } diff --git a/src/EventHub.Admin.Application/Events/Registrations/EventRegistrationAppService.cs b/src/EventHub.Admin.Application/Events/Registrations/EventRegistrationAppService.cs index 0e5b78e..ca2acff 100644 --- a/src/EventHub.Admin.Application/Events/Registrations/EventRegistrationAppService.cs +++ b/src/EventHub.Admin.Application/Events/Registrations/EventRegistrationAppService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Dynamic.Core; using System.Threading.Tasks; using EventHub.Admin.Permissions; using Volo.Abp.Application.Dtos; @@ -33,18 +34,25 @@ namespace EventHub.Admin.Events.Registrations _eventRegistrationManager = eventRegistrationManager; } - public async Task> GetAttendeesAsync(Guid eventId) + public async Task> GetAttendeesAsync(GetEventRegistrationListInput input) { var eventRegistrationQueryable = await _eventRegistrationRepository.GetQueryableAsync(); var userQueryable = await _userRepository.GetQueryableAsync(); var query = from eventRegistration in eventRegistrationQueryable join user in userQueryable on eventRegistration.UserId equals user.Id - where eventRegistration.EventId == eventId + where eventRegistration.EventId == input.EventId orderby eventRegistration.CreationTime descending select user; var totalCount = await AsyncExecuter.CountAsync(query); + + if (!string.IsNullOrWhiteSpace(input.Sorting)) + { + query = query.OrderBy(input.Sorting); + } + + query = query.PageBy(input); var users = await AsyncExecuter.ToListAsync(query.Take(10)); return new PagedResultDto( @@ -82,5 +90,19 @@ namespace EventHub.Admin.Events.Registrations await _eventRegistrationManager.RegisterAsync(@event, user); } } + + public async Task> GetAllAttendeeIdsAsync(Guid eventId) + { + var eventRegistrationQueryable = await _eventRegistrationRepository.GetQueryableAsync(); + var userQueryable = await _userRepository.GetQueryableAsync(); + + var query = from eventRegistration in eventRegistrationQueryable + join user in userQueryable on eventRegistration.UserId equals user.Id + where eventRegistration.EventId == eventId + orderby eventRegistration.CreationTime descending + select user.Id; + + return await AsyncExecuter.ToListAsync(query); + } } } diff --git a/src/EventHub.Admin.Application/Users/UserAppService.cs b/src/EventHub.Admin.Application/Users/UserAppService.cs index b05458a..398e51b 100644 --- a/src/EventHub.Admin.Application/Users/UserAppService.cs +++ b/src/EventHub.Admin.Application/Users/UserAppService.cs @@ -1,8 +1,8 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Dynamic.Core; using System.Threading.Tasks; -using EventHub.Events.Registrations; using Volo.Abp.Application.Dtos; using Volo.Abp.Domain.Repositories; using Volo.Abp.Identity; @@ -12,33 +12,26 @@ namespace EventHub.Admin.Users public class UserAppService : EventHubAdminAppService, IUserAppService { private readonly IRepository _identityUserRepository; - private readonly IRepository _eventRegistrationRepository; - public UserAppService(IRepository identityUserRepository, IRepository eventRegistrationRepository) + public UserAppService(IRepository identityUserRepository) { _identityUserRepository = identityUserRepository; - _eventRegistrationRepository = eventRegistrationRepository; } - public async Task> GetUnregisteredUsersOfEventAsync(GetUnregisteredUserEventInput input) + public async Task> GetListAsync(GetUserListInput input) { var identityUserQueryable = await _identityUserRepository.GetQueryableAsync(); - var eventRegistrationQuery = (await _eventRegistrationRepository.GetQueryableAsync()) - .Where(x => x.EventId == input.EventId) - .Select(x => x.UserId); - - var userIds = await AsyncExecuter.ToListAsync(eventRegistrationQuery); - var query = identityUserQueryable - .Where(x => !userIds.Contains(x.Id)) - .WhereIf(!string.IsNullOrWhiteSpace(input.Username), user => user.UserName.ToLower().Contains(input.Username.ToLower())) - .WhereIf(!string.IsNullOrWhiteSpace(input.Name), user => user.Name.ToLower().Contains(input.Name.ToLower())) - .WhereIf(!string.IsNullOrWhiteSpace(input.Surname), user => user.Surname.ToLower().Contains(input.Surname.ToLower())) - .WhereIf(!string.IsNullOrWhiteSpace(input.Email), user => user.Email.ToLower().Contains(input.Email.ToLower())); + .WhereIf(!string.IsNullOrWhiteSpace(input.Username), user => user.UserName.ToLower().Contains(input.Username.ToLower())); var totalCount = await AsyncExecuter.CountAsync(query); + if (!string.IsNullOrWhiteSpace(input.Sorting)) + { + query = query.OrderBy(input.Sorting); + } + query = query.PageBy(input); var users = await AsyncExecuter.ToListAsync(query); diff --git a/src/EventHub.Admin.HttpApi.Host/Controllers/Events/Registrations/EventRegistrationController.cs b/src/EventHub.Admin.HttpApi.Host/Controllers/Events/Registrations/EventRegistrationController.cs index 78439ed..a4edcca 100644 --- a/src/EventHub.Admin.HttpApi.Host/Controllers/Events/Registrations/EventRegistrationController.cs +++ b/src/EventHub.Admin.HttpApi.Host/Controllers/Events/Registrations/EventRegistrationController.cs @@ -25,9 +25,15 @@ namespace EventHub.Admin.Controllers.Events.Registrations } [HttpGet("{eventId}")] - public Task> GetAttendeesAsync(Guid eventId) + public Task> GetAllAttendeeIdsAsync(Guid eventId) { - return _eventRegistrationAppService.GetAttendeesAsync(eventId); + return _eventRegistrationAppService.GetAllAttendeeIdsAsync(eventId); + } + + [HttpGet] + public Task> GetAttendeesAsync(GetEventRegistrationListInput input) + { + return _eventRegistrationAppService.GetAttendeesAsync(input); } [HttpPost] @@ -36,7 +42,7 @@ namespace EventHub.Admin.Controllers.Events.Registrations return _eventRegistrationAppService.RegisterUsersAsync(eventId, userIds); } - [HttpGet] + [HttpDelete] public Task RemoveAttendeeAsync(Guid eventId, Guid attendeeId) { return _eventRegistrationAppService.RemoveAttendeeAsync(eventId, attendeeId); diff --git a/src/EventHub.Admin.HttpApi.Host/Controllers/Users/UserController.cs b/src/EventHub.Admin.HttpApi.Host/Controllers/Users/UserController.cs index 47c0725..75ff77e 100644 --- a/src/EventHub.Admin.HttpApi.Host/Controllers/Users/UserController.cs +++ b/src/EventHub.Admin.HttpApi.Host/Controllers/Users/UserController.cs @@ -22,9 +22,9 @@ namespace EventHub.Admin.Controllers.Users } [HttpGet] - public Task> GetUnregisteredUsersOfEventAsync(GetUnregisteredUserEventInput input) + public Task> GetListAsync(GetUserListInput input) { - return _userAppService.GetUnregisteredUsersOfEventAsync(input); + return _userAppService.GetListAsync(input); } } } diff --git a/src/EventHub.Admin.Web/Components/UserPicker/UserPicker.razor b/src/EventHub.Admin.Web/Components/UserPicker/UserPicker.razor index e2590e7..82d4fc6 100644 --- a/src/EventHub.Admin.Web/Components/UserPicker/UserPicker.razor +++ b/src/EventHub.Admin.Web/Components/UserPicker/UserPicker.razor @@ -1 +1,70 @@ -@inherits EventHubComponentBase \ No newline at end of file +@using EventHub.Admin.Users +@inherits EventHubComponentBase +@inject IUserAppService UserAppService + + + + @L["AddUser"] + + + +
+ + + @L["UserName"] + + + +
+ + + + + + + + + @if (SelectAllUsers.ContainsKey(context.Id)) + { + + } + + + + + + @(context.Username) + + + + + + @(context.Name) + + + + + + @(context.Surname) + + + + + + @(context.Email) + + + + +
+ + + + +
diff --git a/src/EventHub.Admin.Web/Components/UserPicker/UserPicker.razor.cs b/src/EventHub.Admin.Web/Components/UserPicker/UserPicker.razor.cs new file mode 100644 index 0000000..66982f1 --- /dev/null +++ b/src/EventHub.Admin.Web/Components/UserPicker/UserPicker.razor.cs @@ -0,0 +1,117 @@ +using Blazorise; +using EventHub.Admin.Users; +using Microsoft.AspNetCore.Components; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Blazorise.DataGrid; +using Microsoft.AspNetCore.Components.Web; +using Volo.Abp.Application.Dtos; + +namespace EventHub.Admin.Web.Components.UserPicker +{ + public partial class UserPicker + { + [Parameter] + public Modal UserPickerModal { get; set; } + + [Parameter] + public EventCallback SaveFormAsync { get; set; } + + [Parameter] + public List SelectedUserIds { get; set; } = new List(); + + private GetUserListInput Filter { get; set; } + + private IReadOnlyList UserList { get; set; } + private int CurrentPage { get; set; } + private string CurrentSorting { get; set; } + private int TotalCount { get; set; } + private int PageSize { get; } + public Dictionary SelectAllUsers = new(); + + private bool AllUserSelected + { + get => SelectAllUsers.All(x => x.Value); + set + { + foreach (var key in SelectAllUsers.Keys) + { + SelectAllUsers[key] = value; + } + } + } + + public UserPicker() + { + Filter = new GetUserListInput(); + PageSize = LimitedResultRequestDto.DefaultMaxResultCount; + } + + protected override async Task OnInitializedAsync() + { + await GetUsersAsync(); + } + + protected override async Task OnParametersSetAsync() + { + await GetUsersAsync(); + } + + private async Task GetUsersAsync() + { + Filter.MaxResultCount = PageSize; + Filter.SkipCount = CurrentPage * PageSize; + Filter.Sorting = CurrentSorting; + + var result = await UserAppService.GetListAsync(Filter); + UserList = result.Items; + TotalCount = (int)result.TotalCount; + + FillSelectedUserList(); + } + + private void FillSelectedUserList() + { + SelectAllUsers = new Dictionary(); + + foreach (var user in UserList) + { + SelectAllUsers.Add(user.Id, SelectedUserIds.Contains(user.Id)); + } + } + + private async Task OnKeyPressed(KeyboardEventArgs e) + { + if (e.Code is "Enter" or "NumpadEnter") + { + await GetUsersAsync(); + } + } + + private async Task OnDataGridReadAsync(DataGridReadDataEventArgs e) + { + CurrentSorting = e.Columns + .Where(c => c.Direction != SortDirection.None) + .Select(c => c.Field + (c.Direction == SortDirection.Descending ? " DESC" : "")) + .JoinAsString(","); + CurrentPage = e.Page - 1; + + await GetUsersAsync(); + await InvokeAsync(StateHasChanged); + } + + public Task CloseUserPickerModalAsync() + { + UserPickerModal.Hide(); + + return Task.CompletedTask; + } + + private async Task SaveUserPickerFormAsync() + { + await SaveFormAsync.InvokeAsync(); + } + } +} diff --git a/src/EventHub.Admin.Web/Pages/AttendeeDetail.razor b/src/EventHub.Admin.Web/Pages/AttendeeDetail.razor index d3e5233..c4414b3 100644 --- a/src/EventHub.Admin.Web/Pages/AttendeeDetail.razor +++ b/src/EventHub.Admin.Web/Pages/AttendeeDetail.razor @@ -17,7 +17,7 @@ @if (CanAddAttendee) { } @@ -69,92 +69,6 @@ - - - - @L["AddAttendee"] - - - -
-
- - - @L["Username"] - - - - - - @L["Name"] - - - - - - @L["Surname"] - - - - - - @L["Email"] - - - -
-
- - - - - - - - - @if (SelectAllUsers.ContainsKey(context.Id)) - { - - } - - - - - - @(context.Username) - - - - - - @(context.Name) - - - - - - @(context.Surname) - - - - - - @(context.Email) - - - - -
- - - - -
+ + - diff --git a/src/EventHub.Admin.Web/Pages/AttendeeDetail.razor.cs b/src/EventHub.Admin.Web/Pages/AttendeeDetail.razor.cs index cb8f721..fd348aa 100644 --- a/src/EventHub.Admin.Web/Pages/AttendeeDetail.razor.cs +++ b/src/EventHub.Admin.Web/Pages/AttendeeDetail.razor.cs @@ -8,9 +8,8 @@ using Blazorise; using Volo.Abp.Application.Dtos; using Blazorise.DataGrid; using EventHub.Admin.Permissions; -using EventHub.Admin.Users; +using EventHub.Admin.Web.Components.UserPicker; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Components.Web; namespace EventHub.Admin.Web.Pages { @@ -25,28 +24,15 @@ namespace EventHub.Admin.Web.Pages private int PageSize { get; } private bool CanAddAttendee { get; set; } private Modal AddAttendeeModal { get; set; } - private GetUnregisteredUserEventInput Filter { get; set; } - private IReadOnlyList UserList { get; set; } - private int UserListTotalCount { get; set; } - - private Dictionary SelectAllUsers = new(); - - private bool AllUserSelected - { - get => SelectAllUsers.All(x => x.Value); - set - { - foreach (var key in SelectAllUsers.Keys) - { - SelectAllUsers[key] = value; - } - } - } + private GetEventRegistrationListInput Filter { get; set; } + public UserPicker UserPickerModalRef { get; set; } + private List SelectedUserIds { get; set; } public AttendeeDetail() { PageSize = LimitedResultRequestDto.DefaultMaxResultCount; - Filter = new GetUnregisteredUserEventInput(); + Filter = new GetEventRegistrationListInput(); + SelectedUserIds = new List(); } protected override async Task OnInitializedAsync() @@ -57,10 +43,17 @@ namespace EventHub.Admin.Web.Pages private async Task GetAttendeesAsync() { - var result = await EventRegistrationAppService.GetAttendeesAsync(EventId); + Filter.EventId = EventId; + Filter.MaxResultCount = PageSize; + Filter.SkipCount = CurrentPage * PageSize; + Filter.Sorting = CurrentSorting; + + var result = await EventRegistrationAppService.GetAttendeesAsync(Filter); AttendeeList = result.Items; TotalCount = (int)result.TotalCount; + + SelectedUserIds = await EventRegistrationAppService.GetAllAttendeeIdsAsync(EventId); } private async Task SetPermissionsAsync() @@ -85,67 +78,19 @@ namespace EventHub.Admin.Web.Pages await GetAttendeesAsync(); } - private async Task OpenAddAttendeeModal() - { - AddAttendeeModal.Show(); - await GetUsersAsync(); - } - - private async Task GetUsersAsync() - { - Filter.EventId = EventId; - Filter.MaxResultCount = PageSize; - Filter.SkipCount = CurrentPage * PageSize; - Filter.Sorting = CurrentSorting; - - var result = await UserAppService.GetUnregisteredUsersOfEventAsync(Filter); - UserList = result.Items; - UserListTotalCount = (int)result.TotalCount; - - FillSelectedUserList(); - } - - private void FillSelectedUserList() - { - SelectAllUsers = new Dictionary(); - - foreach (var user in UserList) - { - SelectAllUsers.Add(user.Id, false); - } - } - - private async Task OnKeyPressed(KeyboardEventArgs e) - { - if (e.Code is "Enter" or "NumpadEnter") - { - await GetUsersAsync(); - } - } - - private async Task OnDataGridReadForUsersAsync(DataGridReadDataEventArgs e) - { - CurrentSorting = e.Columns - .Where(c => c.Direction != SortDirection.None) - .Select(c => c.Field + (c.Direction == SortDirection.Descending ? " DESC" : "")) - .JoinAsString(","); - CurrentPage = e.Page - 1; - - await GetUsersAsync(); - await InvokeAsync(StateHasChanged); - } - private async Task AddSelectedUsersToEventAsync() { try { - var selectedUserIds= SelectAllUsers + var selectedUserIds = UserPickerModalRef + .SelectAllUsers .Where(x => x.Value) .Select(x => x.Key) .ToList(); await EventRegistrationAppService.RegisterUsersAsync(EventId, selectedUserIds); - await CloseAddAttendeeModalAsync(); + + AddAttendeeModal.Hide(); await GetAttendeesAsync(); await InvokeAsync(StateHasChanged); @@ -156,15 +101,9 @@ namespace EventHub.Admin.Web.Pages } } - private void ClosingAddAttendeeModal(ModalClosingEventArgs eventArgs) + private void ClosingUserPickerModal(ModalClosingEventArgs eventArgs) { eventArgs.Cancel = eventArgs.CloseReason == CloseReason.FocusLostClosing; } - - private Task CloseAddAttendeeModalAsync() - { - AddAttendeeModal.Hide(); - return Task.CompletedTask; - } } } diff --git a/src/EventHub.Domain.Shared/Localization/EventHub/en.json b/src/EventHub.Domain.Shared/Localization/EventHub/en.json index a816c25..cd1e975 100644 --- a/src/EventHub.Domain.Shared/Localization/EventHub/en.json +++ b/src/EventHub.Domain.Shared/Localization/EventHub/en.json @@ -141,6 +141,8 @@ "AddNewUser": "Add New User", "AttendeeRemoveConfirmationMessage": "Are you sure you want to remove this item?", "RemoveUser": "Remove User", - "AddAttendee": "Add Attendee" + "AddAttendee": "Add Attendee", + "AddUser": "Add User", + } }