public static T Between <T>(this T value, T minimum, T maximum) where T : struct, IComparable <T> { FuzzyRange <T> spec = FuzzyContext.Get <T, FuzzyRange <T> >(value); spec.Minimum = minimum; spec.Maximum = maximum; return(spec); }
public IComparableExtensionsTest() { value = new TestStruct(random.Next()); spec = Substitute.ForPartsOf<FuzzyRange<TestStruct>>(fuzzy, new TestStruct(int.MinValue), new TestStruct(int.MaxValue)); minimum = new TestStruct(int.MinValue + random.Next() % short.MaxValue); maximum = new TestStruct(int.MaxValue - random.Next() % short.MaxValue); newValue = new TestStruct(random.Next()); FuzzyContext.Set(value, spec); ConfiguredCall arrange = fuzzy.Build(spec).Returns(newValue); }
public void Configure(IProfileExpression profile) { profile.CreateMap <Relation, RelationTitleVM>() .MapMember(x => x.Id, x => x.Id) .MapMember(x => x.Type, x => x.Type) .MapMemberDynamic(x => x.Source, x => x.Source) .MapMemberDynamic(x => x.Destination, x => x.Destination) .MapMemberDynamic(x => x.Event, x => x.Event) .MapMember(x => x.Duration, x => FuzzyRange.TryParse(x.Duration)); }
/// <summary> /// Maps the relation to an excerpt. /// </summary> private RelationContext.RelationExcerpt CreateExcerpt(Relation rel) { return(new RelationContext.RelationExcerpt { Id = rel.Id, SourceId = rel.SourceId, DestinationId = rel.DestinationId, Duration = FuzzyRange.TryParse(rel.Duration), EventId = rel.EventId, IsComplementary = rel.IsComplementary, Type = rel.Type }); }
public void Configure(IProfileExpression profile) { profile.CreateMap <Relation, RelationEditorVM>() .MapMember(x => x.Id, x => x.Id) .MapMember(x => x.SourceIds, x => new [] { x.SourceId }) .MapMember(x => x.DestinationId, x => x.DestinationId) .MapMember(x => x.EventId, x => x.EventId) .MapMember(x => x.Type, x => x.Type) .MapMember(x => x.DurationStart, x => FuzzyRange.TrySplit(x.Duration)[0]) .MapMember(x => x.DurationEnd, x => FuzzyRange.TrySplit(x.Duration)[1]) .ReverseMap() .MapMember(x => x.Duration, x => FuzzyRange.TryCombine(x.DurationStart, x.DurationEnd)) .MapMember(x => x.SourceId, x => x.SourceIds != null && x.SourceIds.Length > 0 ? x.SourceIds[0] : Guid.Empty); }
/// <summary> /// Checks if the create/update request contains valid data. /// </summary> private async Task ValidateRequestAsync(RelationEditorVM vm, bool isNew) { var val = new Validator(); vm.SourceIds = vm.SourceIds ?? new Guid[0]; var pageIds = vm.SourceIds .Concat(new [] { vm.DestinationId ?? Guid.Empty, vm.EventId ?? Guid.Empty }) .ToList(); var pages = await _db.Pages .Where(x => pageIds.Contains(x.Id)) .ToDictionaryAsync(x => x.Id, x => x.Type); var sourceTypes = vm.SourceIds.Select(x => pages.TryGetNullableValue(x)).ToList(); var destType = pages.TryGetNullableValue(vm.DestinationId ?? Guid.Empty); var eventType = pages.TryGetNullableValue(vm.EventId ?? Guid.Empty); if (vm.SourceIds == null || vm.SourceIds.Length == 0) { val.Add(nameof(vm.SourceIds), "Выберите страницу"); } else if (isNew == false && vm.SourceIds.Length > 1) { val.Add(nameof(vm.SourceIds), "При редактировании может быть указана только одна страница"); } else if (sourceTypes.Any(x => x == null)) { val.Add(nameof(vm.SourceIds), "Страница не найдена"); } if (vm.DestinationId == null) { val.Add(nameof(vm.DestinationId), "Выберите страницу"); } else if (destType == null) { val.Add(nameof(vm.DestinationId), "Страница не найдена"); } if (destType != null && sourceTypes.Any(x => x != null && !RelationHelper.IsRelationAllowed(x.Value, destType.Value, vm.Type))) { val.Add(nameof(vm.Type), "Тип связи недопустимм для данных страниц"); } if (vm.EventId != null) { if (eventType == null) { val.Add(nameof(vm.EventId), "Страница не найдена"); } else if (eventType != PageType.Event) { val.Add(nameof(vm.EventId), "Требуется страница события"); } else if (!RelationHelper.IsRelationEventReferenceAllowed(vm.Type)) { val.Add(nameof(vm.EventId), "Событие нельзя привязать к данному типу связи"); } } if (!string.IsNullOrEmpty(vm.DurationStart) || !string.IsNullOrEmpty(vm.DurationEnd)) { if (!RelationHelper.IsRelationDurationAllowed(vm.Type)) { val.Add(nameof(vm.DurationStart), "Дату нельзя указать для данного типа связи"); } else { var from = FuzzyDate.TryParse(vm.DurationStart); var to = FuzzyDate.TryParse(vm.DurationEnd); if (from > to) { val.Add(nameof(vm.DurationStart), "Дата начала не может быть больше даты конца"); } else if (FuzzyRange.TryParse(FuzzyRange.TryCombine(vm.DurationStart, vm.DurationEnd)) == null) { val.Add(nameof(vm.DurationStart), "Введите дату в корректном формате"); } } } var existingRelation = await _db.Relations .AnyAsync(x => vm.SourceIds.Contains(x.SourceId) && x.DestinationId == vm.DestinationId && x.Type == vm.Type && x.Id != vm.Id); if (existingRelation) { val.Add(nameof(vm.DestinationId), "Такая связь уже существует!"); } val.ThrowIfInvalid(); }