diff --git a/eventhub/src/EventHub.Application.Contracts/Events/EventDto.cs b/eventhub/src/EventHub.Application.Contracts/Events/EventDto.cs new file mode 100644 index 0000000..c672296 --- /dev/null +++ b/eventhub/src/EventHub.Application.Contracts/Events/EventDto.cs @@ -0,0 +1,22 @@ +using System; +using Volo.Abp.Application.Dtos; + +namespace EventHub.Events +{ + public class EventDto : EntityDto + { + public Guid OrganizationId { get; set; } + + public string Title { get; set; } + + public DateTime StartTime { get; set; } + + public DateTime EndTime { get; set; } + + public string Description { get; set; } + + public bool IsOnline { get; set; } + + public int? Capacity { get; set; } + } +} diff --git a/eventhub/src/EventHub.Application.Contracts/Events/IEventAppService.cs b/eventhub/src/EventHub.Application.Contracts/Events/IEventAppService.cs index e75b435..f2feaf5 100644 --- a/eventhub/src/EventHub.Application.Contracts/Events/IEventAppService.cs +++ b/eventhub/src/EventHub.Application.Contracts/Events/IEventAppService.cs @@ -5,6 +5,6 @@ namespace EventHub.Events { public interface IEventAppService : IApplicationService { - Task CreateAsync(CreateEventDto input); + Task CreateAsync(CreateEventDto input); } } diff --git a/eventhub/src/EventHub.Application/EventHubApplicationAutoMapperProfile.cs b/eventhub/src/EventHub.Application/EventHubApplicationAutoMapperProfile.cs index 7cc6fd3..d829bf2 100644 --- a/eventhub/src/EventHub.Application/EventHubApplicationAutoMapperProfile.cs +++ b/eventhub/src/EventHub.Application/EventHubApplicationAutoMapperProfile.cs @@ -1,4 +1,5 @@ using AutoMapper; +using EventHub.Events; using EventHub.Organizations; namespace EventHub @@ -9,6 +10,8 @@ namespace EventHub { CreateMap(); CreateMap(); + + CreateMap(); } } } diff --git a/eventhub/src/EventHub.Application/Events/EventAppService.cs b/eventhub/src/EventHub.Application/Events/EventAppService.cs index f1d91d1..43ba537 100644 --- a/eventhub/src/EventHub.Application/Events/EventAppService.cs +++ b/eventhub/src/EventHub.Application/Events/EventAppService.cs @@ -25,7 +25,7 @@ namespace EventHub.Events } [Authorize] - public async Task CreateAsync(CreateEventDto input) + public async Task CreateAsync(CreateEventDto input) { var organization = await _organizationRepository.GetAsync(input.OrganizationId); @@ -43,7 +43,12 @@ namespace EventHub.Events input.Description ); + @event.IsOnline = input.IsOnline; + @event.Capacity = input.Capacity; + await _eventRepository.InsertAsync(@event); + + return ObjectMapper.Map(@event); } } } diff --git a/eventhub/test/EventHub.Application.Tests/Events/EventAppServiceTests.cs b/eventhub/test/EventHub.Application.Tests/Events/EventAppServiceTests.cs index 91d8911..c18d990 100644 --- a/eventhub/test/EventHub.Application.Tests/Events/EventAppServiceTests.cs +++ b/eventhub/test/EventHub.Application.Tests/Events/EventAppServiceTests.cs @@ -1,5 +1,7 @@ using System; using System.Threading.Tasks; +using Shouldly; +using Volo.Abp; using Xunit; namespace EventHub.Events @@ -18,7 +20,7 @@ namespace EventHub.Events [Fact] public async Task Should_Create_A_New_Valid_Event() { - await _eventAppService.CreateAsync( + var eventDto = await _eventAppService.CreateAsync( new CreateEventDto { OrganizationId = _testData.OrganizationVolosoftId, @@ -29,6 +31,32 @@ namespace EventHub.Events IsOnline = true } ); + + eventDto.OrganizationId.ShouldBe(_testData.OrganizationVolosoftId); + eventDto.Title.ShouldBe("Introduction to the ABP Framework"); + eventDto.Description.ShouldBe("In this event, we will introduce the ABP Framework and explore the fundamental features."); + eventDto.IsOnline.ShouldBeTrue(); + } + + [Fact] + public async Task Should_Not_Create_Event_For_Not_Authorized_Organization() + { + var exception = await Assert.ThrowsAsync(async () => + { + await _eventAppService.CreateAsync( + new CreateEventDto + { + OrganizationId = _testData.OrganizationDotnetEuropeId, + Title = "Introduction to the ABP Framework", + Description = "In this event, we will introduce the ABP Framework and explore the fundamental features.", + StartTime = DateTime.Now.AddDays(1), + EndTime = DateTime.Now.AddDays(1).AddHours(3), + IsOnline = true + } + ); + }); + + exception.Code.ShouldBe(EventHubErrorCodes.NotAuthorizedToCreateEventInThisOrganization); } } } diff --git a/eventhub/test/EventHub.Domain.Tests/Events/EventManagerTests.cs b/eventhub/test/EventHub.Domain.Tests/Events/EventManagerTests.cs new file mode 100644 index 0000000..1bffcc6 --- /dev/null +++ b/eventhub/test/EventHub.Domain.Tests/Events/EventManagerTests.cs @@ -0,0 +1,45 @@ +using System; +using System.Threading.Tasks; +using EventHub.Organizations; +using Shouldly; +using Volo.Abp.Domain.Repositories; +using Xunit; + +namespace EventHub.Events +{ + public class EventManagerTests : EventHubDomainTestBase + { + private readonly EventHubTestData _testData; + private readonly EventManager _eventManager; + private readonly IRepository _organizationRepository; + + public EventManagerTests() + { + _testData = GetRequiredService(); + _eventManager = GetRequiredService(); + _organizationRepository = GetRequiredService>(); + } + + [Fact] + public async Task Should_Create_A_Valid_Event() + { + await WithUnitOfWorkAsync(async () => + { + var volosoftOrganization = await _organizationRepository.GetAsync( + _testData.OrganizationVolosoftId + ); + + var @event = await _eventManager.CreateAsync( + volosoftOrganization, + "Introduction to the ABP Framework", + DateTime.Now.AddDays(1), + DateTime.Now.AddDays(1).AddHours(3), + "In this event, we will introduce the ABP Framework and explore the fundamental features." + ); + + @event.OrganizationId.ShouldBe(volosoftOrganization.Id); + @event.Title.ShouldBe("Introduction to the ABP Framework"); + }); + } + } +} diff --git a/eventhub/test/EventHub.TestBase/EventHubTestBase.cs b/eventhub/test/EventHub.TestBase/EventHubTestBase.cs index 9d23248..2f6db20 100644 --- a/eventhub/test/EventHub.TestBase/EventHubTestBase.cs +++ b/eventhub/test/EventHub.TestBase/EventHubTestBase.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using EventHub.Events; using EventHub.Organizations; using Microsoft.Extensions.DependencyInjection; using Volo.Abp; @@ -86,10 +87,16 @@ namespace EventHub protected virtual async Task GetOrganizationOrNullAsync(string name) { var organizationRepository = GetRequiredService>(); - return await WithUnitOfWorkAsync(async () => - { - return await organizationRepository.FirstOrDefaultAsync(o => o.Name == name); - } + return await WithUnitOfWorkAsync( + () => organizationRepository.FirstOrDefaultAsync(o => o.Name == name) + ); + } + + protected virtual async Task GetEventOrNullAsync(Guid id) + { + var organizationRepository = GetRequiredService>(); + return await WithUnitOfWorkAsync( + () => organizationRepository.FindAsync(id) ); } } diff --git a/eventhub/test/EventHub.TestBase/EventHubTestDataSeedContributor.cs b/eventhub/test/EventHub.TestBase/EventHubTestDataSeedContributor.cs index 93bb7d8..b7e7ff1 100644 --- a/eventhub/test/EventHub.TestBase/EventHubTestDataSeedContributor.cs +++ b/eventhub/test/EventHub.TestBase/EventHubTestDataSeedContributor.cs @@ -46,7 +46,7 @@ namespace EventHub var dotnetEurope = await _organizationRepository.InsertAsync( await _organizationManager.CreateAsync( - _currentUser.GetId(), + Guid.NewGuid(), //TODO: create a second user and set its id! _eventHubTestData.OrganizationDotnetEuropeName, "Dotnet Europe", "Organizing events on Microsoft's .NET Platform in European Countries."