diff --git a/EventsExpress.Test/MapperTests/EventMapperProfileTests.cs b/EventsExpress.Test/MapperTests/EventMapperProfileTests.cs index 5a7a44df3..5bbb365ff 100644 --- a/EventsExpress.Test/MapperTests/EventMapperProfileTests.cs +++ b/EventsExpress.Test/MapperTests/EventMapperProfileTests.cs @@ -232,7 +232,7 @@ private EventEditViewModel GetEventEditViewModel() Rating = 8, }, }, - Location = new LocationViewModel + Location = new MapViewModel { Type = LocationType.Online, OnlineMeeting = "http://basin.example.com/#branch", @@ -271,7 +271,7 @@ private EventCreateViewModel GetEventCreateViewModel() Rating = 8, }, }, - Location = new LocationViewModel + Location = new MapViewModel { Type = LocationType.Map, Latitude = 0.8, diff --git a/EventsExpress.Test/ValidationTests/LocationViewModelValidationTests.cs b/EventsExpress.Test/ValidationTests/LocationViewModelValidationTests.cs index 1fede545a..28fd2d5cd 100644 --- a/EventsExpress.Test/ValidationTests/LocationViewModelValidationTests.cs +++ b/EventsExpress.Test/ValidationTests/LocationViewModelValidationTests.cs @@ -22,7 +22,7 @@ public void Setup() [Category("Latitude is null")] public void Should_have_error_when_Latitude_is_null() { - var model = new LocationViewModel { Type = LocationType.Map, Latitude = null, Longitude = 7.7 }; + var model = new MapViewModel { Type = LocationType.Map, Latitude = null, Longitude = 7.7 }; var result = validator.TestValidate(model); result.ShouldHaveValidationErrorFor(x => x.Latitude).WithErrorMessage("Field is required!"); } @@ -31,14 +31,14 @@ public void Should_have_error_when_Latitude_is_null() [Category("Longitude is null")] public void Should_have_error_when_Longitude_is_null() { - var model = new LocationViewModel { Type = LocationType.Map, Latitude = 8.8, Longitude = null }; + var model = new MapViewModel { Type = LocationType.Map, Latitude = 8.8, Longitude = null }; var result = validator.TestValidate(model); result.ShouldHaveValidationErrorFor(x => x.Longitude).WithErrorMessage("Field is required!"); } [TestCaseSource(typeof(CorrectMap))] [Category("Correct Longitude and Latitude")] - public void Should_not_have_error_when_Correct_Map(LocationViewModel model) + public void Should_not_have_error_when_Correct_Map(MapViewModel model) { var result = validator.TestValidate(model); result.ShouldNotHaveValidationErrorFor(x => x.Latitude); @@ -47,7 +47,7 @@ public void Should_not_have_error_when_Correct_Map(LocationViewModel model) [TestCaseSource(typeof(CorrectUrl))] [Category("Correct Url")] - public void Should_not_have_error_when_Correct__URL(LocationViewModel model) + public void Should_not_have_error_when_Correct__URL(MapViewModel model) { var result = validator.TestValidate(model); result.ShouldNotHaveValidationErrorFor(x => x.OnlineMeeting); @@ -56,7 +56,7 @@ public void Should_not_have_error_when_Correct__URL(LocationViewModel model) [TestCaseSource(typeof(InCorrectUrl))] [Category("InCorrect Url")] [Test] - public void Should_have_error_when_InCorrect__URL(LocationViewModel model) + public void Should_have_error_when_InCorrect__URL(MapViewModel model) { string modelRes = $"Link '{model.OnlineMeeting}' must be a valid URI. eg: http://www.SomeWebSite.com.au"; var result = validator.TestValidate(model); @@ -66,7 +66,7 @@ public void Should_have_error_when_InCorrect__URL(LocationViewModel model) [TestCaseSource(typeof(CorrectEnumViewModel))] [Category("Correct Enum")] - public void SetLocationTypeForEvent_ValidType_ValidationErrorIsNotReturn(LocationViewModel model) + public void SetLocationTypeForEvent_ValidType_ValidationErrorIsNotReturn(MapViewModel model) { var result = validator.TestValidate(model); result.ShouldNotHaveValidationErrorFor(e => e.Type); @@ -74,7 +74,7 @@ public void SetLocationTypeForEvent_ValidType_ValidationErrorIsNotReturn(Locatio [TestCaseSource(typeof(InCorrectEnumViewModel))] [Category("InCorrect Enum")] - public void SetLocationTypeForEvent_InvalidLocation_ReturnError(LocationViewModel model) + public void SetLocationTypeForEvent_InvalidLocation_ReturnError(MapViewModel model) { var result = validator.TestValidate(model); result.ShouldHaveValidationErrorFor(e => e.Type); diff --git a/EventsExpress.Test/ValidationTests/TestClasses/Location/CorrectEnumViewModel.cs b/EventsExpress.Test/ValidationTests/TestClasses/Location/CorrectEnumViewModel.cs index 50d0647c5..085c22b2a 100644 --- a/EventsExpress.Test/ValidationTests/TestClasses/Location/CorrectEnumViewModel.cs +++ b/EventsExpress.Test/ValidationTests/TestClasses/Location/CorrectEnumViewModel.cs @@ -7,8 +7,8 @@ public class CorrectEnumViewModel : IEnumerable { - private readonly LocationViewModel modelMap = new LocationViewModel { Latitude = 7.7, Longitude = 8.8, OnlineMeeting = "https://example.com/", Type = LocationType.Map }; - private readonly LocationViewModel modelOnline = new LocationViewModel { Latitude = 7.7, Longitude = 8.8, OnlineMeeting = "https://example.com/", Type = LocationType.Online }; + private readonly MapViewModel modelMap = new MapViewModel { Latitude = 7.7, Longitude = 8.8, OnlineMeeting = "https://example.com/", Type = LocationType.Map }; + private readonly MapViewModel modelOnline = new MapViewModel { Latitude = 7.7, Longitude = 8.8, OnlineMeeting = "https://example.com/", Type = LocationType.Online }; public IEnumerator GetEnumerator() { diff --git a/EventsExpress.Test/ValidationTests/TestClasses/Location/CorrectMap.cs b/EventsExpress.Test/ValidationTests/TestClasses/Location/CorrectMap.cs index 3f9d0828f..30eb54501 100644 --- a/EventsExpress.Test/ValidationTests/TestClasses/Location/CorrectMap.cs +++ b/EventsExpress.Test/ValidationTests/TestClasses/Location/CorrectMap.cs @@ -21,7 +21,7 @@ public IEnumerator GetEnumerator() { for (int i = 0; i < pointArr.GetLength(0); i++) { - yield return new object[] { new LocationViewModel { Type = type, Latitude = pointArr[i, 0], Longitude = pointArr[i, 1] } }; + yield return new object[] { new MapViewModel { Type = type, Latitude = pointArr[i, 0], Longitude = pointArr[i, 1] } }; } } } diff --git a/EventsExpress.Test/ValidationTests/TestClasses/Location/CorrectUrl.cs b/EventsExpress.Test/ValidationTests/TestClasses/Location/CorrectUrl.cs index a418790ac..d6ecc9c94 100644 --- a/EventsExpress.Test/ValidationTests/TestClasses/Location/CorrectUrl.cs +++ b/EventsExpress.Test/ValidationTests/TestClasses/Location/CorrectUrl.cs @@ -20,7 +20,7 @@ public IEnumerator GetEnumerator() { for (int i = 0; i < urlArr.Length; i++) { - yield return new object[] { new LocationViewModel { Type = type, OnlineMeeting = urlArr[i] } }; + yield return new object[] { new MapViewModel { Type = type, OnlineMeeting = urlArr[i] } }; } } } diff --git a/EventsExpress.Test/ValidationTests/TestClasses/Location/InCorrectEnumViewModel.cs b/EventsExpress.Test/ValidationTests/TestClasses/Location/InCorrectEnumViewModel.cs index 5351c6a7a..b895e9f42 100644 --- a/EventsExpress.Test/ValidationTests/TestClasses/Location/InCorrectEnumViewModel.cs +++ b/EventsExpress.Test/ValidationTests/TestClasses/Location/InCorrectEnumViewModel.cs @@ -6,8 +6,8 @@ public class InCorrectEnumViewModel : IEnumerable { - private readonly LocationViewModel firstIncorrectView = new LocationViewModel { Latitude = 7.7, Longitude = 8.8, OnlineMeeting = "https://example.com/", Type = (LocationType)8 }; - private readonly LocationViewModel secondIncorrectView = new LocationViewModel { Latitude = 7.7, Longitude = 8.8, OnlineMeeting = "https://example.com/", Type = (LocationType)9 }; + private readonly MapViewModel firstIncorrectView = new MapViewModel { Latitude = 7.7, Longitude = 8.8, OnlineMeeting = "https://example.com/", Type = (LocationType)8 }; + private readonly MapViewModel secondIncorrectView = new MapViewModel { Latitude = 7.7, Longitude = 8.8, OnlineMeeting = "https://example.com/", Type = (LocationType)9 }; public IEnumerator GetEnumerator() { diff --git a/EventsExpress.Test/ValidationTests/TestClasses/Location/InCorrectUrl.cs b/EventsExpress.Test/ValidationTests/TestClasses/Location/InCorrectUrl.cs index f28dc1717..a3eefcb81 100644 --- a/EventsExpress.Test/ValidationTests/TestClasses/Location/InCorrectUrl.cs +++ b/EventsExpress.Test/ValidationTests/TestClasses/Location/InCorrectUrl.cs @@ -22,7 +22,7 @@ public IEnumerator GetEnumerator() { for (int i = 0; i < urlArr.Length; i++) { - yield return new object[] { new LocationViewModel { Type = type, OnlineMeeting = urlArr[i] } }; + yield return new string[] { urlArr[i] }; } } } diff --git a/EventsExpress/ClientApp/src/components/event/event-form.js b/EventsExpress/ClientApp/src/components/event/event-form.js index b17b43194..fe217fbc7 100644 --- a/EventsExpress/ClientApp/src/components/event/event-form.js +++ b/EventsExpress/ClientApp/src/components/event/event-form.js @@ -31,21 +31,6 @@ class EventForm extends Component { } - checkLocation = (location) => { - - if (location !== null) { - if (location.type == enumLocationType.map) { - location.latitude = null; - location.longitude = null; - change(`event-form`, `location`, location); - } - - if (location.type == enumLocationType.online) { - location.onlineMeeting = null; - change(`event-form`, `location.onlineMeeting`, location); - } - } - } periodicityListOptions = (periodicity.map((item) => {item.label} @@ -169,29 +154,27 @@ class EventForm extends Component { placeholder='#hashtags' /> - this.checkLocation(this.props.form_values.location)}> + } label="Map" /> } label="Online" /> {this.props.form_values - && this.props.form_values.location - && this.props.form_values.location.type == enumLocationType.map && + && this.props.form_values.locationType === enumLocationType.map && } {this.props.form_values - && this.props.form_values.location - && this.props.form_values.location.type == enumLocationType.online && + && this.props.form_values.locationType === enumLocationType.online && Enter an https:// URL: - - + - {JSON.stringify(location, null, 2)} + {JSON.stringify(map, null, 2)} diff --git a/EventsExpress/Mapping/EventMapperProfile.cs b/EventsExpress/Mapping/EventMapperProfile.cs index 0bda061f6..0ebf73aff 100644 --- a/EventsExpress/Mapping/EventMapperProfile.cs +++ b/EventsExpress/Mapping/EventMapperProfile.cs @@ -57,7 +57,9 @@ public EventMapperProfile() CreateMap() .ForMember(dest => dest.Categories, opts => opts.MapFrom(src => src.Categories.Select(x => MapCategoryViewModelFromCategoryDto(x)))) - .ForMember(dest => dest.Location, opts => opts.MapFrom(src => MapLocation(src))) + .ForMember(dest => dest.Map, opts => opts.MapFrom(src => MapLocation(src))) + .ForMember(dest => dest.LocationType, opts => opts.MapFrom(src => src.Type)) + .ForMember(dest => dest.OnlineMeeting, opts => opts.MapFrom(src => src.OnlineMeeting)) .ForMember(dest => dest.CountVisitor, opts => opts.MapFrom(src => src.Visitors.Count(x => x.UserStatusEvent == 0))) .ForMember(dest => dest.MaxParticipants, opts => opts.MapFrom(src => src.MaxParticipants)) .ForMember(dest => dest.EventStatus, opts => opts.MapFrom(src => src.EventStatus)) @@ -68,7 +70,9 @@ public EventMapperProfile() .ForMember(dest => dest.Categories, opts => opts.MapFrom(src => src.Categories.Select(x => MapCategoryViewModelFromCategoryDto(x)))) .ForMember(dest => dest.Inventories, opts => opts.MapFrom(src => src.Inventories.Select(x => MapInventoryViewModelFromInventoryDto(x)))) - .ForMember(dest => dest.Location, opts => opts.MapFrom(src => MapLocation(src))) + .ForMember(dest => dest.Map, opts => opts.MapFrom(src => MapLocation(src))) + .ForMember(dest => dest.LocationType, opts => opts.MapFrom(src => src.Type)) + .ForMember(dest => dest.OnlineMeeting, opts => opts.MapFrom(src => src.OnlineMeeting)) .ForMember(dest => dest.Visitors, opts => opts.MapFrom()) .ForMember(dest => dest.Owners, opts => opts.MapFrom()) .ForMember(dest => dest.Frequency, opts => opts.MapFrom(src => src.Frequency)) @@ -85,7 +89,7 @@ public EventMapperProfile() .ForMember(dest => dest.OwnerIds, opts => opts.MapFrom(src => src.Owners.Select(x => x.Id))) .ForMember(dest => dest.Point, opts => opts.MapFrom(src => PointOrNullEdit(src))) .ForMember(dest => dest.OnlineMeeting, opts => opts.MapFrom(src => OnlineMeetingOrNullEdit(src))) - .ForMember(dest => dest.Type, opts => opts.MapFrom(src => src.Location.Type)) + .ForMember(dest => dest.Type, opts => opts.MapFrom(src => src.LocationType)) .ForMember(dest => dest.Photo, opts => opts.Ignore()) .ForMember(dest => dest.Visitors, opts => opts.Ignore()); @@ -95,7 +99,7 @@ public EventMapperProfile() .ForMember(dest => dest.OwnerIds, opts => opts.MapFrom(src => src.Owners.Select(x => x.Id))) .ForMember(dest => dest.Point, opts => opts.MapFrom(src => PointOrNullCreate(src))) .ForMember(dest => dest.OnlineMeeting, opts => opts.MapFrom(src => OnlineMeetingOrNullCreate(src))) - .ForMember(dest => dest.Type, opts => opts.MapFrom(src => src.Location.Type)) + .ForMember(dest => dest.Type, opts => opts.MapFrom(src => src.LocationType)) .ForMember(dest => dest.Periodicity, opts => opts.MapFrom(src => src.Periodicity)) .ForMember(dest => dest.IsReccurent, opts => opts.MapFrom(src => src.IsReccurent)) .ForMember(dest => dest.Inventories, opts => opts.MapFrom(src => @@ -104,53 +108,42 @@ public EventMapperProfile() .ForMember(dest => dest.Visitors, opts => opts.Ignore()); } - private static LocationViewModel MapLocation(EventDto eventDto) + private static MapViewModel MapLocation(EventDto eventDto) { - if (eventDto.OnlineMeeting == null && eventDto.Point == null) + if (eventDto.Point == null) { return null; } - return eventDto.Type == LocationType.Map ? - new LocationViewModel + return new MapViewModel { Latitude = eventDto.Point.X, Longitude = eventDto.Point.Y, - OnlineMeeting = null, - Type = eventDto.Type, - } - : - new LocationViewModel - { - Latitude = null, - Longitude = null, - OnlineMeeting = eventDto.OnlineMeeting.ToString(), - Type = eventDto.Type, - }; + }; } private static Uri OnlineMeetingOrNullEdit(EventEditViewModel eventEditViewModel) { - return eventEditViewModel.Location?.Type == LocationType.Online ? - new Uri(eventEditViewModel.Location.OnlineMeeting) : null; + return eventEditViewModel.LocationType == LocationType.Online ? + new Uri(eventEditViewModel.OnlineMeeting) : null; } private static Point PointOrNullEdit(EventEditViewModel editViewModel) { - return editViewModel.Location?.Type == LocationType.Map ? - new Point(editViewModel.Location.Latitude.Value, editViewModel.Location.Longitude.Value) { SRID = 4326 } : null; + return editViewModel.LocationType == LocationType.Map ? + new Point(editViewModel.Map.Latitude.Value, editViewModel.Map.Longitude.Value) { SRID = 4326 } : null; } private static Uri OnlineMeetingOrNullCreate(EventCreateViewModel eventCreateViewModel) { - return eventCreateViewModel.Location.Type == LocationType.Online ? - new Uri(eventCreateViewModel.Location.OnlineMeeting) : null; + return eventCreateViewModel.LocationType == LocationType.Online ? + new Uri(eventCreateViewModel.OnlineMeeting) : null; } private static Point PointOrNullCreate(EventCreateViewModel createViewModel) { - return createViewModel.Location.Type == LocationType.Map ? - new Point(createViewModel.Location.Latitude.Value, createViewModel.Location.Longitude.Value) { SRID = 4326 } : null; + return createViewModel.LocationType == LocationType.Map ? + new Point(createViewModel.Map.Latitude.Value, createViewModel.Map.Longitude.Value) { SRID = 4326 } : null; } private static string UserName(User user) diff --git a/EventsExpress/Validation/Base/BaseEventViewModelValidator.cs b/EventsExpress/Validation/Base/BaseEventViewModelValidator.cs index 1bfde29a4..e88b4aba7 100644 --- a/EventsExpress/Validation/Base/BaseEventViewModelValidator.cs +++ b/EventsExpress/Validation/Base/BaseEventViewModelValidator.cs @@ -2,6 +2,7 @@ { using System; using EventsExpress.Core.IServices; + using EventsExpress.Db.Enums; using EventsExpress.ViewModels.Base; using FluentValidation; @@ -27,7 +28,27 @@ public BaseEventViewModelValidator(ICategoryService categoryService) RuleFor(x => x.Frequency).GreaterThan(0).WithMessage("Frequency must be greater then 0!"); RuleFor(x => x.Periodicity).IsInEnum().WithMessage("Field is required!"); }); - RuleFor(x => x.Location).SetValidator(new LocationViewModelValidator()); + RuleFor(x => x.LocationType).IsInEnum().WithMessage("Field Location Type is required!"); + When(x => x.LocationType == LocationType.Map, () => + { + RuleFor(x => x.Map.Latitude).NotEmpty().WithMessage("Field is required!"); + RuleFor(x => x.Map.Longitude).NotEmpty().WithMessage("Field is required!"); + }).Otherwise(() => + { + RuleFor(x => x.OnlineMeeting).Must(LinkMustBeAUri) + .WithMessage("Link '{PropertyValue}' must be a valid URI. eg: http://www.SomeWebSite.com.au"); + }); + } + + private bool LinkMustBeAUri(string link) + { + if (string.IsNullOrWhiteSpace(link)) + { + return false; + } + + return Uri.TryCreate(link, UriKind.Absolute, out Uri outUri) + && (outUri.Scheme == Uri.UriSchemeHttp || outUri.Scheme == Uri.UriSchemeHttps); } } } diff --git a/EventsExpress/Validation/LocationViewModelValidator.cs b/EventsExpress/Validation/LocationViewModelValidator.cs index c0f947433..239daad79 100644 --- a/EventsExpress/Validation/LocationViewModelValidator.cs +++ b/EventsExpress/Validation/LocationViewModelValidator.cs @@ -5,31 +5,10 @@ using EventsExpress.ViewModels.Base; using FluentValidation; - public class LocationViewModelValidator : AbstractValidator + public class LocationViewModelValidator : AbstractValidator { public LocationViewModelValidator() { - RuleFor(x => x.Type).IsInEnum().WithMessage("Field Location Type is required!"); - When(location => location.Type == LocationType.Map, () => - { - RuleFor(x => x.Latitude).NotEmpty().WithMessage("Field is required!"); - RuleFor(x => x.Longitude).NotEmpty().WithMessage("Field is required!"); - }).Otherwise(() => - { - RuleFor(x => x.OnlineMeeting).Must(LinkMustBeAUri) - .WithMessage("Link '{PropertyValue}' must be a valid URI. eg: http://www.SomeWebSite.com.au"); - }); - } - - private bool LinkMustBeAUri(string link) - { - if (string.IsNullOrWhiteSpace(link)) - { - return false; - } - - return Uri.TryCreate(link, UriKind.Absolute, out Uri outUri) - && (outUri.Scheme == Uri.UriSchemeHttp || outUri.Scheme == Uri.UriSchemeHttps); } } } diff --git a/EventsExpress/ViewModels/Base/EventViewModelBase.cs b/EventsExpress/ViewModels/Base/EventViewModelBase.cs index 9bc4e014d..3ff862ffc 100644 --- a/EventsExpress/ViewModels/Base/EventViewModelBase.cs +++ b/EventsExpress/ViewModels/Base/EventViewModelBase.cs @@ -22,7 +22,11 @@ public class EventViewModelBase public Periodicity Periodicity { get; set; } - public LocationViewModel Location { get; set; } + public MapViewModel Map { get; set; } + + public string OnlineMeeting { get; set; } + + public LocationType LocationType { get; set; } public bool? IsPublic { get; set; } diff --git a/EventsExpress/ViewModels/LocationViewModel.cs b/EventsExpress/ViewModels/MapViewModel.cs similarity index 58% rename from EventsExpress/ViewModels/LocationViewModel.cs rename to EventsExpress/ViewModels/MapViewModel.cs index f158580de..cb27d1a41 100644 --- a/EventsExpress/ViewModels/LocationViewModel.cs +++ b/EventsExpress/ViewModels/MapViewModel.cs @@ -2,14 +2,10 @@ { using EventsExpress.Db.Enums; - public class LocationViewModel + public class MapViewModel { public double? Latitude { get; set; } public double? Longitude { get; set; } - - public string OnlineMeeting { get; set; } - - public LocationType Type { get; set; } } }
- {JSON.stringify(location, null, 2)} + {JSON.stringify(map, null, 2)}