public CreateResourceWindow(IEnumerable <ResourceDictionary> merged, IResourceProvider provider, IEnumerable <object> targets, IPropertyInfo property) { InitializeComponent(); Resources.MergedDictionaries.AddItems(merged); DataContext = new CreateResourceViewModel(provider, targets, property); SetupResourceKey(); }
public void IsLoading() { var tcs = new TaskCompletionSource <IReadOnlyList <ResourceSource> > (); var target = new object(); var property = new Mock <IPropertyInfo>(); var provider = new Mock <IResourceProvider>(); provider.Setup(p => p.GetResourceSourcesAsync(target, property.Object)).Returns(tcs.Task); var vm = new CreateResourceViewModel(provider.Object, new[] { target }, property.Object); Assert.That(vm.IsLoading, Is.True); bool isLoadingChanged = false; vm.PropertyChanged += (o, e) => { if (e.PropertyName == nameof(CreateResourceViewModel.IsLoading)) { isLoadingChanged = true; } }; tcs.SetResult(new[] { new ResourceSource("app", ResourceSourceType.Application) }); Assert.That(isLoadingChanged, Is.True); Assert.That(vm.IsLoading, Is.False); }
public void SuggestedName() { var target = new object(); var property = new Mock <IPropertyInfo>(); property.SetupGet(pi => pi.Type).Returns(typeof(string)); var provider = new Mock <IResourceProvider>(); provider.Setup(p => p.GetResourceSourcesAsync(target, property.Object)).ReturnsAsync(Sources); var tcs = new TaskCompletionSource <string> (); provider.Setup(p => p.SuggestResourceNameAsync(It.IsAny <IReadOnlyList <object> > (), property.Object)) .Returns(tcs.Task); var vm = new CreateResourceViewModel(provider.Object, new[] { target }, property.Object); Assume.That(vm.ResourceKey, Is.Null); bool changed = false; vm.PropertyChanged += (o, e) => { if (e.PropertyName == nameof(CreateResourceViewModel.ResourceKey)) { changed = true; } }; const string suggested = "suggested"; tcs.SetResult(suggested); Assert.That(changed, Is.True, "ResourceKey did not change"); Assert.That(vm.ResourceKey, Is.EqualTo(suggested)); }
public async Task <IActionResult> CreateResource([FromBody] CreateResourceViewModel viewModel) { var model = mapper.Map <RkResource>(viewModel); await resourceService.CreateResource(model); return(new OkResult()); }
public void DefineInApplicationSource() { var target = new object(); var property = new Mock <IPropertyInfo>(); var provider = new Mock <IResourceProvider>(); provider.Setup(p => p.GetResourceSourcesAsync(target, property.Object)).ReturnsAsync(Sources); var vm = new CreateResourceViewModel(provider.Object, new[] { target }, property.Object); Assume.That(vm.ApplicationSources, Is.Not.Empty); Assume.That(vm.SelectedResourceSource, Is.EqualTo(Sources[0])); Assume.That(vm.DefineInApplication, Is.True); Assume.That(vm.DefineInDocument, Is.False); Assume.That(vm.DefineInApplicationSource, Is.False); bool inAppChanged = false, inDictChanged = false, inDocumentChanged = false, selectedSourceChanged = false; vm.PropertyChanged += (o, e) => { if (e.PropertyName == nameof(CreateResourceViewModel.DefineInApplicationSource)) { inDictChanged = true; } else if (e.PropertyName == nameof(CreateResourceViewModel.DefineInApplication)) { inAppChanged = true; } else if (e.PropertyName == nameof(CreateResourceViewModel.DefineInDocument)) { inDocumentChanged = true; } else if (e.PropertyName == nameof(CreateResourceViewModel.SelectedResourceSource)) { selectedSourceChanged = true; } }; vm.DefineInApplicationSource = true; Assert.That(vm.DefineInApplication, Is.False); Assert.That(vm.DefineInDocument, Is.False); Assert.That(vm.DefineInApplicationSource, Is.True); Assert.That(vm.SelectedResourceSource, Is.EqualTo(Sources[1])); Assert.That(selectedSourceChanged, Is.True); Assert.That(inAppChanged, Is.True); Assert.That(inDictChanged, Is.True); inAppChanged = inDictChanged = inDocumentChanged = selectedSourceChanged = false; vm.DefineInApplicationSource = false; Assert.That(vm.DefineInApplication, Is.True); Assert.That(vm.DefineInDocument, Is.False); Assert.That(vm.DefineInApplicationSource, Is.False); Assert.That(vm.SelectedResourceSource, Is.EqualTo(Sources[0])); Assert.That(selectedSourceChanged, Is.True); Assert.That(inAppChanged, Is.True); Assert.That(inDictChanged, Is.True); }
public void DontDefineInResourceDictionaryFallback(bool appSource, bool documentSource) { var target = new object(); var property = new Mock <IPropertyInfo>(); var provider = new Mock <IResourceProvider>(); provider.Setup(p => p.GetResourceSourcesAsync(target, property.Object)).ReturnsAsync(Sources.Where(s => { if (s.Type == ResourceSourceType.Application) { return(appSource); } if (s.Type == ResourceSourceType.Document) { return(documentSource); } if (s.Type == ResourceSourceType.ResourceDictionary) { return(true); } return(false); }).ToArray()); var vm = new CreateResourceViewModel(provider.Object, new[] { target }, property.Object); vm.DefineInApplicationSource = true; var values = new[] { appSource, documentSource, !appSource && !documentSource }; var defaultOrder = new Func <bool>[] { () => vm.DefineInApplication, () => vm.DefineInDocument, () => vm.DefineInApplicationSource }; vm.DefineInApplicationSource = false; bool hasDefault = false; for (int i = 0; i < values.Length; i++) { if (!hasDefault && values[i]) { hasDefault = true; Assert.That(defaultOrder[i] (), Is.True); } else { Assert.That(defaultOrder[i] (), Is.False); } } }
public void ErrorCheckOnSourceChange() { const string error = "failed"; const string invalidName = "test"; var target = new object(); var property = new Mock <IPropertyInfo>(); property.SetupGet(pi => pi.Type).Returns(typeof(string)); var provider = new Mock <IResourceProvider>(); provider.Setup(p => p.GetResourceSourcesAsync(target, property.Object)).ReturnsAsync(Sources); provider.Setup(p => p.CheckNameErrorsAsync(target, It.Is <ResourceSource> (r => r == Sources[0]), invalidName)) .ReturnsAsync(new ResourceCreateError(error, false)); provider.Setup(p => p.CheckNameErrorsAsync(target, It.Is <ResourceSource> (r => r == Sources[1] || r == Sources[2]), invalidName)) .ReturnsAsync((ResourceCreateError)null); var vm = new CreateResourceViewModel(provider.Object, new[] { target }, property.Object); Assume.That(vm.SelectedResourceSource, Is.EqualTo(Sources[0])); Assume.That(vm.HasErrors, Is.False); bool errorsChanged = false; vm.ErrorsChanged += (sender, args) => { if (args.PropertyName == nameof(CreateResourceViewModel.ResourceKey)) { errorsChanged = true; } }; bool hasErrorsChanged = false; vm.PropertyChanged += (sender, args) => { if (args.PropertyName == nameof(CreateResourceViewModel.HasErrors)) { hasErrorsChanged = true; } }; vm.ResourceKey = invalidName; Assume.That(vm.HasErrors, Is.True); Assume.That(hasErrorsChanged, Is.True, "HasErrors did not change"); Assume.That(errorsChanged, Is.True, "ErrorsChanged did not fire"); hasErrorsChanged = errorsChanged = false; vm.DefineInApplicationSource = true; provider.Verify(r => r.CheckNameErrorsAsync(target, It.IsAny <ResourceSource> (), invalidName), Times.AtLeast(2)); Assert.That(vm.HasErrors, Is.False); Assert.That(hasErrorsChanged, Is.True, "HasErrors did not change"); Assert.That(errorsChanged, Is.True, "ErrorsChanged did not fire"); }
public void NameErrorCleared() { const string error = "failed"; const string invalidName = "test"; var target = new object(); var property = new Mock <IPropertyInfo>(); property.SetupGet(pi => pi.Type).Returns(typeof(string)); var provider = new Mock <IResourceProvider>(); provider.Setup(p => p.GetResourceSourcesAsync(target, property.Object)).ReturnsAsync(Sources); provider.Setup(p => p.CheckNameErrorsAsync(target, It.IsAny <ResourceSource> (), invalidName)) .ReturnsAsync(new ResourceCreateError(error, false)); provider.Setup(p => p.CheckNameErrorsAsync(target, It.IsAny <ResourceSource> (), It.IsNotIn(invalidName))) .ReturnsAsync((ResourceCreateError)null); var vm = new CreateResourceViewModel(provider.Object, new[] { target }, property.Object); bool errorsChanged = false; vm.ErrorsChanged += (sender, args) => { if (args.PropertyName == nameof(CreateResourceViewModel.ResourceKey)) { errorsChanged = true; } }; bool hasErrorsChanged = false; vm.PropertyChanged += (sender, args) => { if (args.PropertyName == nameof(CreateResourceViewModel.HasErrors)) { hasErrorsChanged = true; } }; vm.ResourceKey = invalidName; Assume.That(vm.HasErrors, Is.True); Assume.That(hasErrorsChanged, Is.True, "HasErrors did not change"); Assume.That(errorsChanged, Is.True, "ErrorsChanged did not fire"); Assume.That(vm.GetErrors(nameof(CreateResourceViewModel.ResourceKey)), Contains.Item(error)); errorsChanged = false; hasErrorsChanged = false; vm.ResourceKey = "validName"; Assert.That(vm.HasErrors, Is.False); Assert.That(hasErrorsChanged, Is.True, "HasErrors did not change"); Assert.That(errorsChanged, Is.True, "ErrorsChanged did not fire"); Assert.That(vm.GetErrors(nameof(CreateResourceViewModel.ResourceKey)), Does.Not.Contain(error)); }
public void DocumentSources() { var target = new object(); var property = new Mock <IPropertyInfo>(); var provider = new Mock <IResourceProvider>(); provider.Setup(p => p.GetResourceSourcesAsync(target, property.Object)).ReturnsAsync(Sources); var vm = new CreateResourceViewModel(provider.Object, new[] { target }, property.Object); Assert.That(vm.HasDocumentSources, Is.True, "HasDocumentSources is false"); Assert.That(vm.ApplicationSources.Count, Is.EqualTo(2), "Incorrect number of items"); Assert.That(vm.DocumentSources, Contains.Item(Sources[3])); Assert.That(vm.DocumentSources, Contains.Item(Sources[4])); }
public void DoesntErrorCheckBeforeSourceSet() { var target = new object(); var property = new Mock <IPropertyInfo>(); property.SetupGet(pi => pi.Type).Returns(typeof(string)); var provider = new Mock <IResourceProvider>(); provider.Setup(p => p.GetResourceSourcesAsync(target, property.Object)).ReturnsAsync(Sources); provider.Setup(p => p.SuggestResourceNameAsync(It.IsAny <IReadOnlyCollection <object> > (), It.IsAny <IPropertyInfo> ())) .ReturnsAsync("suggested"); var vm = new CreateResourceViewModel(provider.Object, new[] { target }, property.Object); provider.Verify(r => r.CheckNameErrorsAsync(target, null, It.IsAny <string>()), Times.Never); }
public void IsAppliedToAll() { var target = new object(); var property = new Mock <IPropertyInfo>(); var provider = new Mock <IResourceProvider>(); provider.Setup(p => p.GetResourceSourcesAsync(target, property.Object)).ReturnsAsync(new[] { new ResourceSource("app", ResourceSourceType.Application) }); var vm = new CreateResourceViewModel(provider.Object, new[] { target }, property.Object); Assert.That(vm.IsKeyed, Is.True); Assert.That(vm.IsAppliedToAll, Is.False); bool namedChanged = false, allChanged = false; vm.PropertyChanged += (o, e) => { if (e.PropertyName == nameof(CreateResourceViewModel.IsAppliedToAll)) { allChanged = true; } else if (e.PropertyName == nameof(CreateResourceViewModel.IsKeyed)) { namedChanged = true; } }; vm.IsAppliedToAll = true; Assert.That(vm.IsKeyed, Is.False); Assert.That(vm.IsAppliedToAll, Is.True); Assert.That(namedChanged, Is.True); Assert.That(allChanged, Is.True); namedChanged = allChanged = false; vm.IsAppliedToAll = false; Assert.That(vm.IsKeyed, Is.True); Assert.That(vm.IsAppliedToAll, Is.False); Assert.That(namedChanged, Is.True); Assert.That(allChanged, Is.True); }
public void NameError() { var target = new object(); var property = new Mock <IPropertyInfo>(); property.SetupGet(pi => pi.Type).Returns(typeof(string)); var provider = new Mock <IResourceProvider>(); provider.Setup(p => p.GetResourceSourcesAsync(target, property.Object)).ReturnsAsync(Sources); provider.Setup(p => p.CheckNameErrorsAsync(target, It.IsAny <ResourceSource> (), It.IsAny <string> ())) .ReturnsAsync(new ResourceCreateError("failed", false)); var vm = new CreateResourceViewModel(provider.Object, new[] { target }, property.Object); bool changed = false; vm.ErrorsChanged += (sender, args) => { if (args.PropertyName == nameof(CreateResourceViewModel.ResourceKey)) { changed = true; } }; bool hasErrorsChanged = false; vm.PropertyChanged += (sender, args) => { if (args.PropertyName == nameof(CreateResourceViewModel.HasErrors)) { hasErrorsChanged = true; } }; vm.ResourceKey = "Test"; Assert.That(changed, Is.True, "ErrorsChanged did not fire"); Assert.That(vm.HasErrors, Is.True); Assert.That(hasErrorsChanged, Is.True, "HasErrors did not change"); Assert.That(vm.GetErrors(nameof(CreateResourceViewModel.ResourceKey)), Contains.Item("failed")); }
public async Task <RequestResult <ResourceViewModel> > AddResource(int typeId, CreateResourceViewModel resourceViewModel) { var resType = await _resourceTypeRepository.FindResourceType(typeId, true); if (resType == null) { return(RequestResult <ResourceViewModel> .Failed("Тип ресурса не найден")); } var existingResource = await _resourceRepository.FindResourceByName(resourceViewModel.Name); if (existingResource != null) { return(RequestResult <ResourceViewModel> .Failed("Ресурс с таким именем уже существует")); } var resParameterValues = resType.ResourceParameters.Select(resParam => new ResourceParameterValue { ParameterValue = "", ResourceParameter = resParam }).ToList(); await _resourceParameterValueRepository.AddResourceParameterValues(resParameterValues); var resource = new Resource { ResourceName = resourceViewModel.Name, Price = resourceViewModel.Price, ResourceType = resType, ResourceParameterValues = resParameterValues }; await _resourceRepository.AddResource(resource); return(RequestResult <ResourceViewModel> .Success(new ResourceViewModel(resource))); }
public void FatalError() { var tcs = new TaskCompletionSource <IReadOnlyList <ResourceSource> > (); var target = new object(); var property = new Mock <IPropertyInfo>(); var provider = new Mock <IResourceProvider>(); provider.Setup(p => p.GetResourceSourcesAsync(target, property.Object)).Returns(tcs.Task); var vm = new CreateResourceViewModel(provider.Object, new[] { target }, property.Object); Assume.That(vm.HasFatalError, Is.False); Assume.That(vm.FatalError, Is.Null); bool hasErrorChanged = false, errorChanged = false; vm.PropertyChanged += (o, e) => { if (e.PropertyName == nameof(CreateResourceViewModel.FatalError)) { errorChanged = true; } if (e.PropertyName == nameof(CreateResourceViewModel.HasFatalError)) { hasErrorChanged = true; } }; const string errorMessage = "test"; tcs.SetException(new Exception(errorMessage)); Assert.That(hasErrorChanged, Is.True); Assert.That(errorChanged, Is.True); Assert.That(vm.HasFatalError, Is.True); Assert.That(vm.FatalError, Is.EqualTo(errorMessage)); }
public void CreateResourceCommand() { var target = new object(); var property = new Mock <IPropertyInfo>(); var provider = new Mock <IResourceProvider>(); provider.Setup(p => p.GetResourceSourcesAsync(target, property.Object)).ReturnsAsync(Sources); var vm = new CreateResourceViewModel(provider.Object, new[] { target }, property.Object); Assume.That(vm.IsKeyed, Is.True); Assume.That(vm.ResourceKey, Is.Null); Assume.That(vm.DefineInApplication, Is.True); Assert.That(vm.CreateResourceCommand.CanExecute(null), Is.False); bool changed = false; vm.CreateResourceCommand.CanExecuteChanged += (o, e) => { changed = true; }; vm.ResourceKey = "name"; Assert.That(changed, Is.True); Assert.That(vm.CreateResourceCommand.CanExecute(null), Is.True); bool create = false; vm.CreateResource += (o, e) => { create = true; }; vm.CreateResourceCommand.Execute(null); Assert.That(create, Is.True); }
public int InsertUpdateTextResource(CreateResourceViewModel model) { using (var sql = GetSqlConnection()) { // Insert - Update TextResource var idParent = sql.Query<int>("XInsertUpdateTextResource", new { id = model.Id, whitelabelId = model.WhiteLabelId, section = model.Section, key = model.Key }, commandType: CommandType.StoredProcedure).First(); // Insert - Update TextResourceText foreach (var item in model.Items) { sql.Query<int>("XInsertUpdateTextResourceText", new { id = item.Id, textId = idParent, langId = item.LangId, languageCode = item.LanguageCode, text = item.Text, isHtml = item.IsHtml }, commandType: CommandType.StoredProcedure); } sql.Close(); return 1; } }
public async Task<IActionResult> CreateResource(int typeId, [FromBody] CreateResourceViewModel model) { var result = await _resourceService.AddResource(typeId, model); return result.ToJsonResult(); }
public ActionResult Index(CreateResourceViewModel model) { _service.InsertUpdateTextResource(model); return !ModelState.IsValid ? Json(-100) : Json(1); }