From f528d746750429814e8a8b9c7186277110a3fbda Mon Sep 17 00:00:00 2001 From: Engincan VESKE Date: Wed, 18 Aug 2021 14:14:08 +0300 Subject: [PATCH] Admin: Add or Remove attends according to UserPick component --- .../Events/EventListFilterDto.cs | 3 +- .../IEventRegistrationAppService.cs | 2 +- .../Events/EventAppService.cs | 9 +++-- .../EventRegistrationAppService.cs | 34 +++++++++++++------ .../EventRegistrationController.cs | 4 +-- .../Pages/AttendeeDetail.razor.cs | 9 +++-- .../Pages/EventManagement.razor | 10 ++++-- .../Pages/EventManagement.razor.cs | 10 ++++-- .../Registrations/EventRegistrationManager.cs | 5 +++ 9 files changed, 62 insertions(+), 24 deletions(-) diff --git a/src/EventHub.Admin.Application.Contracts/Events/EventListFilterDto.cs b/src/EventHub.Admin.Application.Contracts/Events/EventListFilterDto.cs index 69758a5..08fed91 100644 --- a/src/EventHub.Admin.Application.Contracts/Events/EventListFilterDto.cs +++ b/src/EventHub.Admin.Application.Contracts/Events/EventListFilterDto.cs @@ -5,7 +5,8 @@ namespace EventHub.Admin.Events { public class EventListFilterDto : PagedAndSortedResultRequestDto { - public DateTime? StartTime { get; set; } + public DateTime? MinStartTime { get; set; } + public DateTime? MaxStartTime { get; set; } public string Title { 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 c15b793..eb491bb 100644 --- a/src/EventHub.Admin.Application.Contracts/Events/Registrations/IEventRegistrationAppService.cs +++ b/src/EventHub.Admin.Application.Contracts/Events/Registrations/IEventRegistrationAppService.cs @@ -10,7 +10,7 @@ namespace EventHub.Admin.Events.Registrations { Task> GetAttendeesAsync(GetEventRegistrationListInput input); - Task RemoveAttendeeAsync(Guid eventId, Guid attendeeId); + Task UnRegisterAttendeeAsync(Guid eventId, Guid attendeeId); Task RegisterUsersAsync(Guid eventId, List userIds); diff --git a/src/EventHub.Admin.Application/Events/EventAppService.cs b/src/EventHub.Admin.Application/Events/EventAppService.cs index 066cf4b..10eccd8 100644 --- a/src/EventHub.Admin.Application/Events/EventAppService.cs +++ b/src/EventHub.Admin.Application/Events/EventAppService.cs @@ -3,17 +3,19 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Dynamic.Core; using System.Threading.Tasks; +using EventHub.Admin.Permissions; using EventHub.Countries; using EventHub.Events; using EventHub.Events.Registrations; using EventHub.Organizations; +using Microsoft.AspNetCore.Authorization; using Volo.Abp.Application.Dtos; using Volo.Abp.BlobStoring; using Volo.Abp.Domain.Repositories; namespace EventHub.Admin.Events { - //[Authorize(EventHubPermissions.Events.Default)] + [Authorize(EventHubPermissions.Events.Default)] public class EventAppService : EventHubAdminAppService, IEventAppService { private readonly IRepository _eventRepository; @@ -70,7 +72,8 @@ namespace EventHub.Admin.Events }) .WhereIf(!string.IsNullOrWhiteSpace(input.Title), x => x.Title.ToLower().Contains(input.Title.ToLower())) .WhereIf(!string.IsNullOrWhiteSpace(input.OrganizationDisplayName), x => x.OrganizationDisplayName.ToLower().Contains(input.OrganizationDisplayName.ToLower())) - .WhereIf(input.StartTime.HasValue, x => x.StartTime > input.StartTime) + .WhereIf(input.MinStartTime.HasValue, x => x.StartTime >= input.MinStartTime) + .WhereIf(input.MaxStartTime.HasValue, x => x.StartTime <= input.MaxStartTime) .WhereIf(input.MinAttendeeCount.HasValue, x => x.AttendeeCount >= input.MinAttendeeCount) .WhereIf(input.MaxAttendeeCount.HasValue, x => x.AttendeeCount <= input.MaxAttendeeCount); @@ -83,7 +86,7 @@ namespace EventHub.Admin.Events return new PagedResultDto(totalCount, events); } - //[Authorize(EventHubPermissions.Events.Update)] + [Authorize(EventHubPermissions.Events.Update)] public async Task UpdateAsync(Guid id, UpdateEventDto input) { var @event = await _eventRepository.GetAsync(id); diff --git a/src/EventHub.Admin.Application/Events/Registrations/EventRegistrationAppService.cs b/src/EventHub.Admin.Application/Events/Registrations/EventRegistrationAppService.cs index ca2acff..bdcee4f 100644 --- a/src/EventHub.Admin.Application/Events/Registrations/EventRegistrationAppService.cs +++ b/src/EventHub.Admin.Application/Events/Registrations/EventRegistrationAppService.cs @@ -10,10 +10,11 @@ using Volo.Abp.Identity; using EventHub.Events.Registrations; using Microsoft.AspNetCore.Authorization; using EventHub.Events; +using Volo.Abp; namespace EventHub.Admin.Events.Registrations { - //[Authorize(EventHubPermissions.Events.Registrations.Default)] + [Authorize(EventHubPermissions.Events.Registrations.Default)] public class EventRegistrationAppService : EventHubAdminAppService, IEventRegistrationAppService { private readonly IRepository _userRepository; @@ -21,7 +22,6 @@ namespace EventHub.Admin.Events.Registrations private readonly IRepository _eventRepository; private readonly EventRegistrationManager _eventRegistrationManager; - public EventRegistrationAppService( IRepository userRepository, IRepository eventRegistrationRepository, @@ -61,16 +61,13 @@ namespace EventHub.Admin.Events.Registrations ); } - //[Authorize(EventHubPermissions.Events.Registrations.RemoveAttendee)] - public async Task RemoveAttendeeAsync(Guid eventId, Guid attendeeId) + [Authorize(EventHubPermissions.Events.Registrations.RemoveAttendee)] + public async Task UnRegisterAttendeeAsync(Guid eventId, Guid attendeeId) { - await _eventRegistrationManager.UnregisterAsync( - await _eventRepository.GetAsync(eventId), - await _userRepository.GetAsync(attendeeId) - ); + await _eventRegistrationRepository.DeleteAsync(x => x.EventId == eventId && x.UserId == attendeeId); } - //[Authorize(EventHubPermissions.Events.Registrations.AddAttendee)] + [Authorize(EventHubPermissions.Events.Registrations.AddAttendee)] public async Task RegisterUsersAsync(Guid eventId, List userIds) { if (userIds == null || !userIds.Any()) @@ -83,11 +80,15 @@ namespace EventHub.Admin.Events.Registrations var userQueryable = await _userRepository.GetQueryableAsync(); var query = userQueryable.Where(user => userIds.Contains(user.Id)); + await CheckEventCapacityAsync(@event); + var users = await AsyncExecuter.ToListAsync(query); - foreach (var user in users) { - await _eventRegistrationManager.RegisterAsync(@event, user); + if (!await _eventRegistrationManager.IsRegisteredAsync(@event, user)) + { + await _eventRegistrationRepository.InsertAsync(await _eventRegistrationManager.CreateAsync(@event.Id, user.Id)); + } } } @@ -104,5 +105,16 @@ namespace EventHub.Admin.Events.Registrations return await AsyncExecuter.ToListAsync(query); } + + private async Task CheckEventCapacityAsync(Event @event) + { + var registrationCount = await _eventRegistrationRepository.CountAsync(x => x.EventId == @event.Id); + + if (@event.Capacity != null && registrationCount >= @event.Capacity) + { + throw new BusinessException(EventHubErrorCodes.CapacityOfEventFull) + .WithData("EventTitle", @event.Title); + } + } } } 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 a4edcca..7abd373 100644 --- a/src/EventHub.Admin.HttpApi.Host/Controllers/Events/Registrations/EventRegistrationController.cs +++ b/src/EventHub.Admin.HttpApi.Host/Controllers/Events/Registrations/EventRegistrationController.cs @@ -43,9 +43,9 @@ namespace EventHub.Admin.Controllers.Events.Registrations } [HttpDelete] - public Task RemoveAttendeeAsync(Guid eventId, Guid attendeeId) + public Task UnRegisterAttendeeAsync(Guid eventId, Guid attendeeId) { - return _eventRegistrationAppService.RemoveAttendeeAsync(eventId, attendeeId); + return _eventRegistrationAppService.UnRegisterAttendeeAsync(eventId, attendeeId); } } } diff --git a/src/EventHub.Admin.Web/Pages/AttendeeDetail.razor.cs b/src/EventHub.Admin.Web/Pages/AttendeeDetail.razor.cs index 86380db..1e6aae1 100644 --- a/src/EventHub.Admin.Web/Pages/AttendeeDetail.razor.cs +++ b/src/EventHub.Admin.Web/Pages/AttendeeDetail.razor.cs @@ -73,7 +73,7 @@ namespace EventHub.Admin.Web.Pages } private async Task RemoveAttendeeAsync(EventAttendeeDto attendee) { - await EventRegistrationAppService.RemoveAttendeeAsync(EventId, attendee.Id); + await EventRegistrationAppService.UnRegisterAttendeeAsync(EventId, attendee.Id); await GetAttendeesAsync(); } @@ -87,8 +87,13 @@ namespace EventHub.Admin.Web.Pages .Select(x => x.Key) .ToList(); - await EventRegistrationAppService.RegisterUsersAsync(EventId, selectedUserIds); + var removedAttendees = SelectedUserIds.Except(selectedUserIds).ToList(); + foreach (var attendeeId in removedAttendees) + { + await EventRegistrationAppService.UnRegisterAttendeeAsync(EventId, attendeeId); + } + await EventRegistrationAppService.RegisterUsersAsync(EventId, selectedUserIds); await UserPickerModalRef.CloseUserPickerModalAsync(); await GetAttendeesAsync(); diff --git a/src/EventHub.Admin.Web/Pages/EventManagement.razor b/src/EventHub.Admin.Web/Pages/EventManagement.razor index aa0ecb1..8d6cee9 100644 --- a/src/EventHub.Admin.Web/Pages/EventManagement.razor +++ b/src/EventHub.Admin.Web/Pages/EventManagement.razor @@ -32,8 +32,14 @@ - @L["StartTime"] - + @L["MinStartTime"] + + + + + + @L["MaxEndTime"] + diff --git a/src/EventHub.Admin.Web/Pages/EventManagement.razor.cs b/src/EventHub.Admin.Web/Pages/EventManagement.razor.cs index c42e142..05d3453 100644 --- a/src/EventHub.Admin.Web/Pages/EventManagement.razor.cs +++ b/src/EventHub.Admin.Web/Pages/EventManagement.razor.cs @@ -125,9 +125,15 @@ namespace EventHub.Admin.Web.Pages } } - private async Task OnSelectedDateChanged(DateTime? changedDate) + private async Task OnSelectedDateChangedForMinStartTime(DateTime? minStartTime) { - Filter.StartTime = changedDate; + Filter.MinStartTime = minStartTime; + await GetEventsAsync(); + } + + private async Task OnSelectedDateChangedForMaxStartTime(DateTime? maxStartTime) + { + Filter.MaxStartTime = maxStartTime; await GetEventsAsync(); } diff --git a/src/EventHub.Domain/Events/Registrations/EventRegistrationManager.cs b/src/EventHub.Domain/Events/Registrations/EventRegistrationManager.cs index 2938903..9579cbe 100644 --- a/src/EventHub.Domain/Events/Registrations/EventRegistrationManager.cs +++ b/src/EventHub.Domain/Events/Registrations/EventRegistrationManager.cs @@ -16,6 +16,11 @@ namespace EventHub.Events.Registrations _eventRegistrationRepository = eventRegistrationRepository; } + public async Task CreateAsync(Guid eventId, Guid userId) + { + return new EventRegistration(GuidGenerator.Create(), eventId, userId); + } + public async Task RegisterAsync( Event @event, IdentityUser user)