mirror of https://github.com/abpframework/eventhub
4 changed files with 130 additions and 0 deletions
@ -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); |
|||
} |
|||
} |
|||
@ -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<AppUser, Guid> _userRepository; |
|||
private readonly IRepository<Event, Guid> _eventRepository; |
|||
|
|||
public EventRegistrationAppService( |
|||
EventRegistrationManager eventRegistrationManager, |
|||
IRepository<AppUser, Guid> userRepository, |
|||
IRepository<Event, Guid> 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()) |
|||
); |
|||
} |
|||
} |
|||
} |
|||
@ -1,3 +1,4 @@ |
|||
using System.Runtime.CompilerServices; |
|||
[assembly:InternalsVisibleToAttribute("EventHub.Application.Tests")] |
|||
[assembly:InternalsVisibleToAttribute("EventHub.Domain.Tests")] |
|||
[assembly:InternalsVisibleToAttribute("EventHub.TestBase")] |
|||
|
|||
@ -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<EventRegistration, Guid> _eventRegistrationRepository; |
|||
private readonly EventHubTestData _testData; |
|||
private readonly ICurrentUser _currentUser; |
|||
|
|||
public EventRegistrationAppServiceTests() |
|||
{ |
|||
_eventRegistrationAppService = GetRequiredService<IEventRegistrationAppService>(); |
|||
_eventRegistrationRepository = GetRequiredService<IRepository<EventRegistration, Guid>>(); |
|||
_testData = GetRequiredService<EventHubTestData>(); |
|||
_currentUser = GetRequiredService<ICurrentUser>(); |
|||
} |
|||
|
|||
[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<EventRegistration> GetRegistrationOrNull(Guid eventId, Guid userId) |
|||
{ |
|||
return await WithUnitOfWorkAsync(async () => |
|||
{ |
|||
return await _eventRegistrationRepository.FirstOrDefaultAsync( |
|||
x => x.EventId == eventId && x.UserId == userId |
|||
); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue