mirror of https://github.com/abpframework/eventhub
6 changed files with 211 additions and 4 deletions
@ -1,18 +1,63 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using EventHub.Users; |
|||
using Volo.Abp; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.Domain.Services; |
|||
using Volo.Abp.Timing; |
|||
|
|||
namespace EventHub.Events.Registrations |
|||
{ |
|||
public class EventRegistrationManager : DomainService |
|||
{ |
|||
private readonly IRepository<EventRegistration, Guid> _eventRegistrationRepository; |
|||
private readonly IClock _clock; |
|||
|
|||
public EventRegistrationManager( |
|||
IRepository<EventRegistration, Guid> eventRegistrationRepository |
|||
) |
|||
IRepository<EventRegistration, Guid> eventRegistrationRepository, |
|||
IClock clock) |
|||
{ |
|||
_eventRegistrationRepository = eventRegistrationRepository; |
|||
_clock = clock; |
|||
} |
|||
|
|||
public async Task RegisterAsync( |
|||
Event @event, |
|||
AppUser user) |
|||
{ |
|||
CheckEventEndTime(@event); |
|||
|
|||
if (await _eventRegistrationRepository.AnyAsync(x => x.EventId == @event.Id && x.UserId == user.Id)) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
await _eventRegistrationRepository.InsertAsync( |
|||
new EventRegistration( |
|||
GuidGenerator.Create(), |
|||
@event.Id, |
|||
user.Id |
|||
) |
|||
); |
|||
} |
|||
|
|||
public async Task UnregisterAsync( |
|||
Event @event, |
|||
AppUser user) |
|||
{ |
|||
CheckEventEndTime(@event); |
|||
|
|||
await _eventRegistrationRepository.DeleteAsync( |
|||
x => x.EventId == @event.Id && x.UserId == user.Id |
|||
); |
|||
} |
|||
|
|||
private void CheckEventEndTime(Event @event) |
|||
{ |
|||
if (_clock.Now > @event.EndTime) |
|||
{ |
|||
throw new BusinessException(EventHubErrorCodes.CantRegisterOrUnregisterForAPastEvent); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,116 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Volo.Abp; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Xunit; |
|||
|
|||
namespace EventHub.Events.Registrations |
|||
{ |
|||
public class EventRegistrationManagerTests : EventHubDomainTestBase |
|||
{ |
|||
private readonly EventRegistrationManager _eventRegistrationManager; |
|||
private readonly IRepository<EventRegistration, Guid> _eventRegistrationRepository; |
|||
private readonly EventHubTestData _testData; |
|||
|
|||
public EventRegistrationManagerTests() |
|||
{ |
|||
_eventRegistrationManager = GetRequiredService<EventRegistrationManager>(); |
|||
_eventRegistrationRepository = GetRequiredService<IRepository<EventRegistration, Guid>>(); |
|||
_testData = GetRequiredService<EventHubTestData>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Register_To_An_Event() |
|||
{ |
|||
await WithUnitOfWorkAsync(async () => |
|||
{ |
|||
var user = await GetUserAsync(_testData.UserAdminId); |
|||
var @event = await GetEventAsync(_testData.AbpMicroservicesFutureEventId); |
|||
await _eventRegistrationManager.RegisterAsync(@event, user); |
|||
}); |
|||
|
|||
(await GetRegistrationOrNull(_testData.AbpMicroservicesFutureEventId, _testData.UserAdminId)) |
|||
.ShouldNotBeNull(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Register_To_An_Event_In_The_Past() |
|||
{ |
|||
var exception = await Assert.ThrowsAsync<BusinessException>(async () => |
|||
{ |
|||
await WithUnitOfWorkAsync(async () => |
|||
{ |
|||
var user = await GetUserAsync(_testData.UserAdminId); |
|||
var @event = await GetEventAsync(_testData.AbpBlazorPastEventId); |
|||
await _eventRegistrationManager.RegisterAsync(@event, user); |
|||
}); |
|||
}); |
|||
|
|||
exception.Code.ShouldBe(EventHubErrorCodes.CantRegisterOrUnregisterForAPastEvent); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Register_Multiple_Times() |
|||
{ |
|||
// Register
|
|||
await WithUnitOfWorkAsync(async () => |
|||
{ |
|||
var user = await GetUserAsync(_testData.UserAdminId); |
|||
var @event = await GetEventAsync(_testData.AbpMicroservicesFutureEventId); |
|||
await _eventRegistrationManager.RegisterAsync(@event, user); |
|||
}); |
|||
|
|||
// Register again
|
|||
await WithUnitOfWorkAsync(async () => |
|||
{ |
|||
var user = await GetUserAsync(_testData.UserAdminId); |
|||
var @event = await GetEventAsync(_testData.AbpMicroservicesFutureEventId); |
|||
await _eventRegistrationManager.RegisterAsync(@event, user); |
|||
}); |
|||
|
|||
await WithUnitOfWorkAsync(async () => |
|||
{ |
|||
var registrationCount = await _eventRegistrationRepository.CountAsync( |
|||
x => x.EventId == _testData.AbpMicroservicesFutureEventId && x.UserId == _testData.UserAdminId |
|||
); |
|||
registrationCount.ShouldBe(1); |
|||
}); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Unregister_From_An_Event() |
|||
{ |
|||
await WithUnitOfWorkAsync(async () => |
|||
{ |
|||
await _eventRegistrationRepository.InsertAsync( |
|||
new EventRegistration( |
|||
Guid.NewGuid(), |
|||
_testData.AbpMicroservicesFutureEventId, |
|||
_testData.UserAdminId |
|||
) |
|||
); |
|||
}); |
|||
|
|||
await WithUnitOfWorkAsync(async () => |
|||
{ |
|||
var user = await GetUserAsync(_testData.UserAdminId); |
|||
var @event = await GetEventAsync(_testData.AbpMicroservicesFutureEventId); |
|||
await _eventRegistrationManager.UnregisterAsync(@event, user); |
|||
}); |
|||
|
|||
(await GetRegistrationOrNull(_testData.AbpMicroservicesFutureEventId, _testData.UserAdminId)) |
|||
.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