Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions EventsExpress.Test/MapperTests/EventMapperProfileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ private EventEditViewModel GetEventEditViewModel()
Rating = 8,
},
},
Location = new LocationViewModel
Location = new MapViewModel
{
Type = LocationType.Online,
OnlineMeeting = "http://basin.example.com/#branch",
Expand Down Expand Up @@ -271,7 +271,7 @@ private EventCreateViewModel GetEventCreateViewModel()
Rating = 8,
},
},
Location = new LocationViewModel
Location = new MapViewModel
{
Type = LocationType.Map,
Latitude = 0.8,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -66,15 +66,15 @@ 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);
}

[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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] } };
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] } };
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] };
}
}
}
Expand Down
27 changes: 5 additions & 22 deletions EventsExpress/ClientApp/src/components/event/event-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
<option value={item.value} key={item.value}> {item.label} </option>
Expand Down Expand Up @@ -169,29 +154,27 @@ class EventForm extends Component {
placeholder='#hashtags'
/>
</div>
<Field name="location.type" component={radioButton} parse={Number} onChange={() => this.checkLocation(this.props.form_values.location)}>
<Field name="locationType" component={radioButton} parse={Number} >
<FormControlLabel value={0} control={<Radio />} label="Map" />
<FormControlLabel value={1} control={<Radio />} label="Online" />
</Field>
{this.props.form_values
&& this.props.form_values.location
&& this.props.form_values.location.type == enumLocationType.map &&
&& this.props.form_values.locationType === enumLocationType.map &&

<div className="mt-2">
<Field
name='location'
name='map'
component={LocationMapWithMarker}
/>
</div>
}
{this.props.form_values
&& this.props.form_values.location
&& this.props.form_values.location.type == enumLocationType.online &&
&& this.props.form_values.locationType === enumLocationType.online &&

<div className="mt-2">
<label htmlFor="url">Enter an https:// URL:</label>
<Field
name='location.onlineMeeting'
name='onlineMeeting'
component={renderTextField}
type="url"
label="Url"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function LocationMapWithMarker(props) {
props.input.value.latitude = initialPos.lat;
props.input.value.longitude = initialPos.lng;
}
const [location, setLocation] = React.useState(initialPos);
const [map, setLocation] = React.useState(initialPos);

function handleChange(latlng) {
setLocation(latlng);
Expand All @@ -25,17 +25,17 @@ export default function LocationMapWithMarker(props) {

return (
<Field
name='location'
location = {location}
name='map'
location = {map}
onUpdate = {handleChange}
component={LocationMap}
>
<Marker position={location}
<Marker position={map}
draggable={true}
onDragend={updateMarker}>
<Popup position={location}>
<Popup position={map}>
<pre>
{JSON.stringify(location, null, 2)}
{JSON.stringify(map, null, 2)}
</pre>
</Popup>
</Marker>
Expand Down
47 changes: 20 additions & 27 deletions EventsExpress/Mapping/EventMapperProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public EventMapperProfile()

CreateMap<EventDto, EventPreviewViewModel>()
.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))
Expand All @@ -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<EventDtoToVisitorsResolver>())
.ForMember(dest => dest.Owners, opts => opts.MapFrom<EventDtoToOwnersResolver>())
.ForMember(dest => dest.Frequency, opts => opts.MapFrom(src => src.Frequency))
Expand All @@ -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());

Expand All @@ -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 =>
Expand All @@ -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)
Expand Down
23 changes: 22 additions & 1 deletion EventsExpress/Validation/Base/BaseEventViewModelValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using EventsExpress.Core.IServices;
using EventsExpress.Db.Enums;
using EventsExpress.ViewModels.Base;
using FluentValidation;

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