diff --git a/eventhub/src/EventHub.Domain.Shared/EventHubErrorCodes.cs b/eventhub/src/EventHub.Domain.Shared/EventHubErrorCodes.cs index 10c63e5..64ae4c3 100644 --- a/eventhub/src/EventHub.Domain.Shared/EventHubErrorCodes.cs +++ b/eventhub/src/EventHub.Domain.Shared/EventHubErrorCodes.cs @@ -4,5 +4,6 @@ { public const string OrganizationNameAlreadyExists = "EventHub:OrganizationNameAlreadyExists"; public const string NotAuthorizedToCreateEventInThisOrganization = "EventHub:NotAuthorizedToCreateEventInThisOrganization"; + public const string EventEndTimeCantBeEarlierThanStartTime = "EventHub:EventEndTimeCantBeEarlierThanStartTime"; } } diff --git a/eventhub/src/EventHub.Domain.Shared/Localization/EventHub/en.json b/eventhub/src/EventHub.Domain.Shared/Localization/EventHub/en.json index e8da647..0a6cac7 100644 --- a/eventhub/src/EventHub.Domain.Shared/Localization/EventHub/en.json +++ b/eventhub/src/EventHub.Domain.Shared/Localization/EventHub/en.json @@ -18,6 +18,7 @@ "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:EventEndTimeCantBeEarlierThanStartTime": "Event end time can not be earlier than the start time.", "Events": "Events", "Members": "Members", "SeeOrganization": "See Organization", diff --git a/eventhub/src/EventHub.Domain/Events/Event.cs b/eventhub/src/EventHub.Domain/Events/Event.cs index a1f4784..88af6bb 100644 --- a/eventhub/src/EventHub.Domain/Events/Event.cs +++ b/eventhub/src/EventHub.Domain/Events/Event.cs @@ -25,7 +25,7 @@ namespace EventHub.Events } - public Event( + internal Event( Guid id, Guid organizationId, string title, @@ -54,6 +54,11 @@ namespace EventHub.Events public Event SetTime(DateTime startTime, DateTime endTime) { + if (startTime > endTime) + { + throw new BusinessException(EventHubErrorCodes.EventEndTimeCantBeEarlierThanStartTime); + } + StartTime = startTime; EndTime = endTime; return this; diff --git a/eventhub/src/EventHub.Domain/Events/EventManager.cs b/eventhub/src/EventHub.Domain/Events/EventManager.cs index deedf66..15b4386 100644 --- a/eventhub/src/EventHub.Domain/Events/EventManager.cs +++ b/eventhub/src/EventHub.Domain/Events/EventManager.cs @@ -7,20 +7,22 @@ namespace EventHub.Events { public class EventManager : DomainService { - public async Task CreateAsync( + public ValueTask CreateAsync( Organization organization, string title, DateTime startTime, DateTime endTime, string description) { - return new Event( - GuidGenerator.Create(), - organization.Id, - title, - startTime, - endTime, - description + return ValueTask.FromResult( + new Event( + GuidGenerator.Create(), + organization.Id, + title, + startTime, + endTime, + description + ) ); } } diff --git a/eventhub/test/EventHub.Domain.Tests/Events/EventTests.cs b/eventhub/test/EventHub.Domain.Tests/Events/EventTests.cs new file mode 100644 index 0000000..0c873d9 --- /dev/null +++ b/eventhub/test/EventHub.Domain.Tests/Events/EventTests.cs @@ -0,0 +1,28 @@ +using System; +using Shouldly; +using Volo.Abp; +using Xunit; + +namespace EventHub.Events +{ + public class EventTests + { + [Fact] + public void Should_Not_Allow_End_Time_To_Be_Earlier_Than_Start_Time() + { + var exception = Assert.Throws(() => + { + new Event( + Guid.NewGuid(), + Guid.NewGuid(), + "Introduction to the ABP Framework", + DateTime.Now, + DateTime.Now.AddDays(-2), + "In this event, we will introduce the ABP Framework and explore the fundamental features." + ); + }); + + exception.Code.ShouldBe(EventHubErrorCodes.EventEndTimeCantBeEarlierThanStartTime); + } + } +}