Browse Source

Implemented EventRegistrationManager.

pull/15/head
Halil İbrahim Kalkan 6 years ago
parent
commit
a4d6977c38
  1. 1
      eventhub/src/EventHub.Domain.Shared/EventHubErrorCodes.cs
  2. 3
      eventhub/src/EventHub.Domain.Shared/Localization/EventHub/en.json
  3. 2
      eventhub/src/EventHub.Domain/Events/Registrations/EventRegistration.cs
  4. 49
      eventhub/src/EventHub.Domain/Events/Registrations/EventRegistrationManager.cs
  5. 116
      eventhub/test/EventHub.Domain.Tests/Events/Registrations/EventRegistrationManagerTests.cs
  6. 44
      eventhub/test/EventHub.TestBase/EventHubTestBase.cs

1
eventhub/src/EventHub.Domain.Shared/EventHubErrorCodes.cs

@ -5,5 +5,6 @@
public const string OrganizationNameAlreadyExists = "EventHub:OrganizationNameAlreadyExists";
public const string NotAuthorizedToCreateEventInThisOrganization = "EventHub:NotAuthorizedToCreateEventInThisOrganization";
public const string EventEndTimeCantBeEarlierThanStartTime = "EventHub:EventEndTimeCantBeEarlierThanStartTime";
public const string CantRegisterOrUnregisterForAPastEvent = "EventHub:CantRegisterOrUnregisterForAPastEvent";
}
}

3
eventhub/src/EventHub.Domain.Shared/Localization/EventHub/en.json

@ -17,8 +17,9 @@
"DisplayName:IsOnline": "Is Online?",
"DisplayName:Capacity": "Capacity",
"EventHub:OrganizationNameAlreadyExists": "The organization {Name} already exists. Please use another name.",
"EventHub:NotAuthorizedToCreateEventInThisOrganization": "You are not authorized to create events for the organization {OrganizationName}",
"EventHub:NotAuthorizedToCreateEventInThisOrganization": "You are not authorized to create events for the organization {OrganizationName}.",
"EventHub:EventEndTimeCantBeEarlierThanStartTime": "Event end time can not be earlier than the start time.",
"EventHub:CantRegisterOrUnregisterForAPastEvent": "Can not register to or unregister from an event in the past.",
"Events": "Events",
"Members": "Members",
"SeeOrganization": "See Organization",

2
eventhub/src/EventHub.Domain/Events/Registrations/EventRegistration.cs

@ -14,7 +14,7 @@ namespace EventHub.Events.Registrations
}
public EventRegistration(
internal EventRegistration(
Guid id,
Guid eventId,
Guid userId)

49
eventhub/src/EventHub.Domain/Events/Registrations/EventRegistrationManager.cs

@ -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);
}
}
}
}

116
eventhub/test/EventHub.Domain.Tests/Events/Registrations/EventRegistrationManagerTests.cs

@ -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
);
});
}
}
}

44
eventhub/test/EventHub.TestBase/EventHubTestBase.cs

@ -2,9 +2,12 @@
using System.Threading.Tasks;
using EventHub.Events;
using EventHub.Organizations;
using EventHub.Users;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
using Volo.Abp.Modularity;
using Volo.Abp.Uow;
using Volo.Abp.Testing;
@ -92,6 +95,17 @@ namespace EventHub
);
}
protected virtual async Task<Organization> GetOrganizationAsync(string name)
{
var organization = await GetOrganizationOrNullAsync(name);
if (organization == null)
{
throw new EntityNotFoundException(typeof(Event), name);
}
return organization;
}
protected virtual async Task<Event> GetEventOrNullAsync(Guid id)
{
var organizationRepository = GetRequiredService<IRepository<Event, Guid>>();
@ -99,5 +113,35 @@ namespace EventHub
() => organizationRepository.FindAsync(id)
);
}
protected virtual async Task<Event> GetEventAsync(Guid id)
{
var @event = await GetEventOrNullAsync(id);
if (@event == null)
{
throw new EntityNotFoundException(typeof(Event), id);
}
return @event;
}
protected virtual async Task<AppUser> GetUserOrNullAsync(Guid id)
{
var userRepository = GetRequiredService<IRepository<AppUser, Guid>>();
return await WithUnitOfWorkAsync(
() => userRepository.FindAsync(id)
);
}
protected virtual async Task<AppUser> GetUserAsync(Guid id)
{
var user = await GetUserOrNullAsync(id);
if (user == null)
{
throw new EntityNotFoundException(typeof(AppUser), id);
}
return user;
}
}
}

Loading…
Cancel
Save