diff --git a/eventhub/src/EventHub.Application.Contracts/Events/Registrations/IEventRegistrationAppService.cs b/eventhub/src/EventHub.Application.Contracts/Events/Registrations/IEventRegistrationAppService.cs new file mode 100644 index 0000000..0aa6920 --- /dev/null +++ b/eventhub/src/EventHub.Application.Contracts/Events/Registrations/IEventRegistrationAppService.cs @@ -0,0 +1,13 @@ +using System; +using System.Threading.Tasks; +using Volo.Abp.Application.Services; + +namespace EventHub.Events.Registrations +{ + public interface IEventRegistrationAppService : IApplicationService + { + Task RegisterAsync(Guid id); + + Task UnregisterAsync(Guid id); + } +} diff --git a/eventhub/src/EventHub.Application/Events/Registrations/EventRegistrationAppService.cs b/eventhub/src/EventHub.Application/Events/Registrations/EventRegistrationAppService.cs new file mode 100644 index 0000000..f5f81b4 --- /dev/null +++ b/eventhub/src/EventHub.Application/Events/Registrations/EventRegistrationAppService.cs @@ -0,0 +1,44 @@ +using System; +using System.Threading.Tasks; +using EventHub.Users; +using Microsoft.AspNetCore.Authorization; +using Volo.Abp.Domain.Repositories; +using Volo.Abp.Users; + +namespace EventHub.Events.Registrations +{ + public class EventRegistrationAppService : EventHubAppService, IEventRegistrationAppService + { + private readonly EventRegistrationManager _eventRegistrationManager; + private readonly IRepository _userRepository; + private readonly IRepository _eventRepository; + + public EventRegistrationAppService( + EventRegistrationManager eventRegistrationManager, + IRepository userRepository, + IRepository eventRepository) + { + _eventRegistrationManager = eventRegistrationManager; + _userRepository = userRepository; + _eventRepository = eventRepository; + } + + [Authorize] + public async Task RegisterAsync(Guid id) + { + await _eventRegistrationManager.RegisterAsync( + await _eventRepository.GetAsync(id), + await _userRepository.GetAsync(CurrentUser.GetId()) + ); + } + + [Authorize] + public async Task UnregisterAsync(Guid id) + { + await _eventRegistrationManager.UnregisterAsync( + await _eventRepository.GetAsync(id), + await _userRepository.GetAsync(CurrentUser.GetId()) + ); + } + } +} diff --git a/eventhub/src/EventHub.Domain/Properties/AssemblyInfo.cs b/eventhub/src/EventHub.Domain/Properties/AssemblyInfo.cs index 025db80..be9bcfe 100644 --- a/eventhub/src/EventHub.Domain/Properties/AssemblyInfo.cs +++ b/eventhub/src/EventHub.Domain/Properties/AssemblyInfo.cs @@ -1,3 +1,4 @@ using System.Runtime.CompilerServices; +[assembly:InternalsVisibleToAttribute("EventHub.Application.Tests")] [assembly:InternalsVisibleToAttribute("EventHub.Domain.Tests")] [assembly:InternalsVisibleToAttribute("EventHub.TestBase")] diff --git a/eventhub/test/EventHub.Application.Tests/Events/Registrations/EventRegistrationAppServiceTests.cs b/eventhub/test/EventHub.Application.Tests/Events/Registrations/EventRegistrationAppServiceTests.cs new file mode 100644 index 0000000..d445dca --- /dev/null +++ b/eventhub/test/EventHub.Application.Tests/Events/Registrations/EventRegistrationAppServiceTests.cs @@ -0,0 +1,72 @@ +using System; +using System.Threading.Tasks; +using Shouldly; +using Volo.Abp.Domain.Repositories; +using Volo.Abp.Users; +using Xunit; + +namespace EventHub.Events.Registrations +{ + public class EventRegistrationAppServiceTests : EventHubApplicationTestBase + { + private readonly IEventRegistrationAppService _eventRegistrationAppService; + private readonly IRepository _eventRegistrationRepository; + private readonly EventHubTestData _testData; + private readonly ICurrentUser _currentUser; + + public EventRegistrationAppServiceTests() + { + _eventRegistrationAppService = GetRequiredService(); + _eventRegistrationRepository = GetRequiredService>(); + _testData = GetRequiredService(); + _currentUser = GetRequiredService(); + } + + [Fact] + public async Task Should_Register_To_An_Event() + { + await _eventRegistrationAppService.RegisterAsync( + _testData.AbpMicroservicesFutureEventId + ); + + (await GetRegistrationOrNull( + _testData.AbpMicroservicesFutureEventId, + _currentUser.GetId() + )).ShouldNotBeNull(); + } + + [Fact] + public async Task Should_Unregister_From_An_Event() + { + await WithUnitOfWorkAsync(async () => + { + await _eventRegistrationRepository.InsertAsync( + new EventRegistration( + Guid.NewGuid(), + _testData.AbpMicroservicesFutureEventId, + _currentUser.GetId() + ) + ); + }); + + await _eventRegistrationAppService.UnregisterAsync( + _testData.AbpMicroservicesFutureEventId + ); + + (await GetRegistrationOrNull( + _testData.AbpMicroservicesFutureEventId, + _currentUser.GetId()) + ).ShouldBeNull(); + } + + private async Task GetRegistrationOrNull(Guid eventId, Guid userId) + { + return await WithUnitOfWorkAsync(async () => + { + return await _eventRegistrationRepository.FirstOrDefaultAsync( + x => x.EventId == eventId && x.UserId == userId + ); + }); + } + } +}