public async Task<PaymentsTopicViewModel> GetViewModel(PaymentFormBindingModel bindingModel = null) { /*------------------------------------------------------------------------------------------------------------------------ | Establish variables \-----------------------------------------------------------------------------------------------------------------------*/ var braintreeGateway = _braintreeConfiguration.GetGateway(); var clientToken = braintreeGateway.ClientToken.Generate(); /*------------------------------------------------------------------------------------------------------------------------ | Establish view model \-----------------------------------------------------------------------------------------------------------------------*/ var viewModel = await _topicMappingService.MapAsync<PaymentsTopicViewModel>(CurrentTopic); viewModel.BindingModel = bindingModel; /*------------------------------------------------------------------------------------------------------------------------ | Pass client token to model \-----------------------------------------------------------------------------------------------------------------------*/ if (viewModel != null) { viewModel.ClientToken = clientToken; } /*------------------------------------------------------------------------------------------------------------------------ | Return topic view \-----------------------------------------------------------------------------------------------------------------------*/ return viewModel; }
/*========================================================================================================================== | GET: INDEX (VIEW TOPIC) \-------------------------------------------------------------------------------------------------------------------------*/ /// <summary> /// Provides access to a view associated with the current topic's Content Type, if appropriate, view (as defined by the /// query string or topic's view. /// </summary> /// <returns>A view associated with the requested topic's Content Type and view.</returns> public async virtual Task<ActionResult> IndexAsync(string path) { /*------------------------------------------------------------------------------------------------------------------------ | Establish default view model \-----------------------------------------------------------------------------------------------------------------------*/ var topicViewModel = await _topicMappingService.MapAsync(CurrentTopic).ConfigureAwait(false); /*------------------------------------------------------------------------------------------------------------------------ | Return topic view \-----------------------------------------------------------------------------------------------------------------------*/ return new TopicViewResult(topicViewModel, CurrentTopic.ContentType, CurrentTopic.View); }
public async Task<PaymentsTopicViewModel> GetViewModel(PaymentFormBindingModel bindingModel = null) { /*------------------------------------------------------------------------------------------------------------------------ | Establish variables \-----------------------------------------------------------------------------------------------------------------------*/ var braintreeGateway = _braintreeConfiguration.GetGateway(); var clientToken = braintreeGateway.ClientToken.Generate(); /*------------------------------------------------------------------------------------------------------------------------ | Establish view model \-----------------------------------------------------------------------------------------------------------------------*/ var viewModel = new PaymentsTopicViewModel { ClientToken = clientToken, BindingModel = bindingModel }; viewModel = (PaymentsTopicViewModel)await _topicMappingService.MapAsync(CurrentTopic, viewModel).ConfigureAwait(true); /*------------------------------------------------------------------------------------------------------------------------ | Return topic view \-----------------------------------------------------------------------------------------------------------------------*/ return viewModel; }
/*========================================================================================================================== | HELPER: CREATE INVOICE VIEW MODEL \-------------------------------------------------------------------------------------------------------------------------*/ /// <summary> /// Constructs a new view model containing the /// </summary> private async Task<InvoiceTopicViewModel> GetInvoiceViewModel(int invoiceNumber) { var invoice = _topicRepository.Load($"Administration:Invoices:{invoiceNumber}"); var viewModel = await _topicMappingService.MapAsync<InvoiceTopicViewModel>(invoice); return viewModel; }
/*========================================================================================================================== | HELPER: CREATE VIEW MODEL \-------------------------------------------------------------------------------------------------------------------------*/ /// <summary> /// Constructs a new view model based on the <typeparamref name="T"/> binding model type. /// </summary> private async Task<FormPageTopicViewModel<T>> CreateViewModel<T>(T bindingModel = null) where T: class, ITopicBindingModel, new() => await _topicMappingService.MapAsync( CurrentTopic, new FormPageTopicViewModel<T>(bindingModel) ) as FormPageTopicViewModel<T>;
public async Task Map_Generic_ReturnsNewModel() { var topic = TopicFactory.Create("Test", "Page"); topic.Attributes.SetValue("MetaTitle", "ValueA"); topic.Attributes.SetValue("Title", "Value1"); topic.Attributes.SetValue("IsHidden", "1"); var target = await _mappingService.MapAsync<PageTopicViewModel>(topic).ConfigureAwait(false); Assert.AreEqual<string>("ValueA", target.MetaTitle); Assert.AreEqual<string>("Value1", target.Title); Assert.AreEqual<bool>(true, target.IsHidden); }
/*========================================================================================================================== | METHOD: INVOKE \-------------------------------------------------------------------------------------------------------------------------*/ /// <summary> /// Assembles the view model for the <see cref="ReflexiveViewComponent"/>. /// </summary> public async Task<IViewComponentResult> InvokeAsync( EditingTopicViewModel currentTopic, ReflexiveAttributeDescriptorViewModel attribute, string htmlFieldPrefix ) { /*------------------------------------------------------------------------------------------------------------------------ | Validate parameters \-----------------------------------------------------------------------------------------------------------------------*/ Contract.Requires(currentTopic, nameof(currentTopic)); Contract.Requires(attribute, nameof(attribute)); /*------------------------------------------------------------------------------------------------------------------------ | Set HTML prefix \-----------------------------------------------------------------------------------------------------------------------*/ ViewData.TemplateInfo.HtmlFieldPrefix = htmlFieldPrefix; /*------------------------------------------------------------------------------------------------------------------------ | Establish snapshot of previously saved attribute descriptor \-----------------------------------------------------------------------------------------------------------------------*/ var topic = _topicRepository.Load(currentTopic.UniqueKey); var reflexiveViewModel = (AttributeDescriptorViewModel?)null; if (topic?.ContentType.EndsWith("AttributeDescriptor", StringComparison.OrdinalIgnoreCase)?? false) { reflexiveViewModel = (AttributeDescriptorViewModel?)await _topicMappingService.MapAsync(topic).ConfigureAwait(false); } if (reflexiveViewModel is null) { reflexiveViewModel = new(); } /*------------------------------------------------------------------------------------------------------------------------ | Establish hybrid view model >------------------------------------------------------------------------------------------------------------------------- | The ParentAttributeDescriptor will be of the target type expected for the view component that will be executed. But it | should use the core AttributeDescriptor attributes of the current attribute, so it shows up in the same location with | the same title and description as defined for the ReflexiveAttributeDescriptorViewModel. \-----------------------------------------------------------------------------------------------------------------------*/ reflexiveViewModel = reflexiveViewModel with { Key = attribute.Key, Description = attribute.Description, DisplayGroup = attribute.DisplayGroup, DefaultValue = attribute.DefaultValue, IsRequired = attribute.IsRequired, SortOrder = attribute.SortOrder, Title = attribute.Title }; /*------------------------------------------------------------------------------------------------------------------------ | Establish view model \-----------------------------------------------------------------------------------------------------------------------*/ var viewModel = new AttributeViewModel<AttributeDescriptorViewModel>(currentTopic, reflexiveViewModel); /*------------------------------------------------------------------------------------------------------------------------ | Return view with view model \-----------------------------------------------------------------------------------------------------------------------*/ return View(viewModel); } } // Class
/*========================================================================================================================== | GET VIEW MODEL (ASYNC) \-------------------------------------------------------------------------------------------------------------------------*/ /// <inheritdoc /> public async Task<T?> GetViewModelAsync( Topic? sourceTopic, int tiers = 1, Func<Topic, bool>? validationDelegate = null ) { /*------------------------------------------------------------------------------------------------------------------------ | Validate preconditions \-----------------------------------------------------------------------------------------------------------------------*/ tiers--; if (sourceTopic is null) { return null; } /*------------------------------------------------------------------------------------------------------------------------ | Establish variables \-----------------------------------------------------------------------------------------------------------------------*/ var taskQueue = new List<Task<T?>>(); var children = new List<T>(); var viewModel = (T?)null; /*------------------------------------------------------------------------------------------------------------------------ | Establish default delegate \-----------------------------------------------------------------------------------------------------------------------*/ if (validationDelegate is null) { validationDelegate = (Topic) => true; } /*------------------------------------------------------------------------------------------------------------------------ | Map object \-----------------------------------------------------------------------------------------------------------------------*/ viewModel = await _topicMappingService.MapAsync<T>(sourceTopic, Relationships.None).ConfigureAwait(false); Contract.Assume( viewModel, $"The 'ITopicMappingService' failed to return a {typeof(T)} model for the '{sourceTopic.GetUniqueKey()}' topic." ); /*------------------------------------------------------------------------------------------------------------------------ | Request mapping of children \-----------------------------------------------------------------------------------------------------------------------*/ if (tiers >= 0 && viewModel.Children.Count == 0) { foreach (var topic in sourceTopic.Children.Where(t => t.IsVisible() && validationDelegate(t))) { taskQueue.Add(GetViewModelAsync(topic, tiers, validationDelegate)); } } /*------------------------------------------------------------------------------------------------------------------------ | Process children \-----------------------------------------------------------------------------------------------------------------------*/ while (taskQueue.Count > 0 && viewModel.Children.Count == 0) { var dtoTask = await Task.WhenAny(taskQueue).ConfigureAwait(false); var dto = await dtoTask.ConfigureAwait(false); taskQueue.Remove(dtoTask); if (dto is not null) { children.Add(dto); } } /*------------------------------------------------------------------------------------------------------------------------ | Add children to view model \-----------------------------------------------------------------------------------------------------------------------*/ if (viewModel.Children.Count == 0) { lock (viewModel) { if (viewModel.Children.Count == 0) { children.ForEach(c => viewModel.Children.Add(c)); } } } /*------------------------------------------------------------------------------------------------------------------------ | Return view model \-----------------------------------------------------------------------------------------------------------------------*/ return viewModel; }