public async Task <IActionResult> Create(string sitemapId, string sourceType) { if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageSitemaps)) { return(Forbid()); } var sitemap = await _sitemapManager.GetSitemapAsync(sitemapId); if (sitemap == null) { return(NotFound()); } var source = _factories.FirstOrDefault(x => x.Name == sourceType)?.Create(); if (source == null) { return(NotFound()); } var model = new CreateSourceViewModel { SitemapId = sitemapId, SitemapSource = source, SitemapSourceId = source.Id, SitemapSourceType = sourceType, Editor = await _displayManager.BuildEditorAsync(source, updater : _updateModelAccessor.ModelUpdater, isNew : true) }; model.Editor.SitemapSource = source; return(View(model)); }
public JsonResult Create([FromBody] CreateSourceViewModel source) { if (!ModelState.IsValid) { Response.StatusCode = (int)HttpStatusCode.BadRequest; var errors = ModelState.Values.Where(x => x.Errors.Count > 0); var errorMessages = new List <string>(); foreach (var error in errors) { errorMessages.Add(error.Errors[0].ErrorMessage); } return(Json(errorMessages.ToArray())); } _unifyDbContext.Sources.Add(new Source() { UnifyUserId = _userManager.GetUserId(User), Name = source.Name, Url = source.Url, Tags = source.Tags }); _unifyDbContext.SaveChanges(); return(Json("Success")); }
public async Task <IActionResult> Create(CreateSourceViewModel model) { if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageSitemaps)) { return(Forbid()); } var sitemap = await _sitemapManager.LoadSitemapAsync(model.SitemapId); if (sitemap == null) { return(NotFound()); } var source = _factories.FirstOrDefault(x => x.Name == model.SitemapSourceType)?.Create(); if (source == null) { return(NotFound()); } dynamic editor = await _displayManager.UpdateEditorAsync(source, updater : _updateModelAccessor.ModelUpdater, isNew : true); editor.SitemapStep = source; if (ModelState.IsValid) { source.Id = model.SitemapSourceId; sitemap.SitemapSources.Add(source); // Clear sitemap cache when new source added. await _sitemapCacheProvider.ClearSitemapCacheAsync(sitemap.Path); await _sitemapManager.SaveSitemapAsync(sitemap.SitemapId, sitemap); _notifier.Success(H["Sitemap source added successfully"]); return(RedirectToAction("Display", "Admin", new { sitemapId = model.SitemapId })); } model.Editor = editor; // If we got this far, something failed, redisplay form return(View(model)); }