From 0bb13e535d6f286974f9c07df9504aac90533f5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Sat, 23 Jan 2021 21:05:45 +0300 Subject: [PATCH] Don't allow set endtime before starttime. --- .../EventHubErrorCodes.cs | 1 + .../Localization/EventHub/en.json | 1 + eventhub/src/EventHub.Domain/Events/Event.cs | 7 ++++- .../EventHub.Domain/Events/EventManager.cs | 18 ++++++------ .../Events/EventTests.cs | 28 +++++++++++++++++++ 5 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 eventhub/test/EventHub.Domain.Tests/Events/EventTests.cs 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); + } + } +}