public async Task <IActionResult> Edit(int id, [Bind("Id,Category,Title,Description,Tags")] PolicyViewModel policyVM) { if (id != policyVM?.Id) { return(NotFound()); } if (ModelState.IsValid) { var entity = await _context.Policies .Include(f => f.Category) .SingleOrDefaultAsync(c => c.Id == id); if (entity.IsNull()) { return(NotFound()); } if (!entity.Category.Title.Equals(policyVM.Category.Title, StringComparison.OrdinalIgnoreCase)) { var category = await _context.Categories.SingleOrDefaultAsync(c => c.Title.Equals(policyVM.Category.Title, StringComparison.OrdinalIgnoreCase)); if (category.IsNotNull()) { entity.Category = category; } else { entity.Category = new Category { Title = policyVM.Category.Title, Url = CustomUrlHelper.URLFriendly(policyVM.Category.Title), }; } } entity.Title = policyVM.Title; entity.Description = policyVM.Description; var file = Request.Form.Files.SingleOrDefault(); if (file.IsNotNull()) { var filename = await _fileStorageService.SetBlobAsync(file); entity.FileUrl = filename; } var tags = TagHelpers.GetTagsFromString(policyVM.Tags); var allTagEntities = GetAllTagEntitiesInternal(policyVM, tags); TagHelpers.SetTags <Policy, PolicyTag>(tags, entity, allTagEntities); await _context.SaveChangesAsync(); return(RedirectToAction("Details", new { Id = entity.Id })); } return(View(policyVM)); }
public void Dont_Throw_On_Null() { // Assign // Act TagHelpers.SetTags <News, NewsTag>(null, null, null); TagHelpers.GetTagsFromString(null); // Assert }
public async Task <IActionResult> Create([Bind("Id,Category,Title,Description,Tags")] PolicyViewModel policyVM) { if (ModelState.IsValid) { var category = await _context .Categories .SingleOrDefaultAsync(c => c.Title.Equals(policyVM.Category.Title, StringComparison.OrdinalIgnoreCase)); if (category.IsNotNull()) { policyVM.Category = category; } else { policyVM.Category = new Category { Title = policyVM.Category.Title, Url = CustomUrlHelper.URLFriendly(policyVM.Category.Title), }; } var policy = new Policy { Category = policyVM.Category, Description = policyVM.Description, FileUrl = policyVM.FileUrl, Title = policyVM.Title, Url = CustomUrlHelper.URLFriendly(policyVM.Title), }; var file = Request.Form.Files.SingleOrDefault(); if (file.IsNotNull()) { var filename = await _fileStorageService.SetBlobAsync(file); policy.FileUrl = filename; } var tags = TagHelpers.GetTagsFromString(policyVM.Tags); var allTagEntities = GetAllTagEntitiesInternal(policyVM, tags); TagHelpers.SetTags <Policy, PolicyTag>(tags, policy, allTagEntities); await _context.AddAsync(policy); await _context.SaveChangesAsync(); return(RedirectToAction("Details", new { Id = policy.Id })); } ViewData["Categories"] = new SelectList(_context.Categories, nameof(Category.Id), nameof(Category.Title)); return(View(policyVM)); }
public void Return_Array() { // Assign var tagsInput = "Test, test2; test-again"; // Act var tags = TagHelpers.GetTagsFromString(tagsInput); // Assert Assert.Contains("Test", tags); Assert.Contains("test2", tags); Assert.Contains("test-again", tags); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Category,Question,Answer,Url")] FaqViewModel faqVM) { if (id != faqVM?.Id) { return(NotFound()); } if (ModelState.IsValid) { var entity = await _context.Faqs .Include(f => f.Category) .SingleOrDefaultAsync(c => c.Id == id); if (entity.IsNull()) { return(NotFound()); } if (!entity.Category.Title.Equals(faqVM.Category.Title, StringComparison.OrdinalIgnoreCase)) { var category = await _context.Categories.SingleOrDefaultAsync(c => c.Title.Equals(faqVM.Category.Title, StringComparison.OrdinalIgnoreCase)); if (category.IsNotNull()) { entity.Category = category; } else { entity.Category = new Category { Title = faqVM.Category.Title, Url = CustomUrlHelper.URLFriendly(faqVM.Category.Title), }; } } entity.Answer = faqVM.Answer; entity.Question = faqVM.Question; var tags = TagHelpers.GetTagsFromString(faqVM.Tags); var allTagEntities = GetAllTagEntitiesInternal(faqVM, tags); TagHelpers.SetTags <Faq, FaqTag>(tags, entity, allTagEntities); await _context.SaveChangesAsync(); return(RedirectToAction("Details", new { Id = entity.Id })); } return(View(faqVM)); }
public async Task <IActionResult> Create([Bind("Category,Question,Answer,Tags")] FaqViewModel faqVM) { if (ModelState.IsValid) { var category = await _context .Categories .SingleOrDefaultAsync(c => c.Title.Equals(faqVM.Category.Title, StringComparison.OrdinalIgnoreCase)); if (category.IsNotNull()) { faqVM.Category = category; } else { faqVM.Category = new Category { Title = faqVM.Category.Title, Url = CustomUrlHelper.URLFriendly(faqVM.Category.Title), }; } var faq = new Faq { Answer = faqVM.Answer, Category = faqVM.Category, Question = faqVM.Question, Url = CustomUrlHelper.URLFriendly(faqVM.Question), }; var tags = TagHelpers.GetTagsFromString(faqVM.Tags); var allTagEntities = GetAllTagEntitiesInternal(faqVM, tags); TagHelpers.SetTags <Faq, FaqTag>(tags, faq, allTagEntities); await _context.AddAsync(faq); await _context.SaveChangesAsync(); return(RedirectToAction("Details", new { Id = faq.Id })); } ViewData["Categories"] = new SelectList(_context.Categories, nameof(Category.Id), nameof(Category.Title)); return(View(faqVM)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Title,Text,Published,HeaderImage,Tags")] NewsViewModel news) { try { if (!ModelState.IsValid) { return(View(news)); } var username = HttpContext.User.GetUsername(); var isAdmin = HttpContext.User.IsAdmin(); var entity = _context.News .Include(n => n.User) .SingleOrDefault(n => n.Id == id); if (entity == null) { return(NotFound()); } if (entity.UserId?.Equals(username) != true && !isAdmin) { ModelState.AddModelError("Error", "As a non admin you can only update your own news."); HttpContext.Response.StatusCode = StatusCodes.Status403Forbidden; return(View(news)); } // If the news changes to Published for the first time, set creation date if (!entity.HasEverBeenPublished && news.Published) { entity.Created = _dateTimeFactory.DateTimeOffsetUtc; } else if (news.Published) { entity.Updated = _dateTimeFactory.DateTimeOffsetUtc; } entity.Title = news.Title; entity.Text = news.Text; entity.Published = news.Published; var image = Request.Form.Files.SingleOrDefault(f => f.ContentType.Contains("image")); if (image.IsNotNull()) { var filename = await _fileStorageService.SetImageAsync(image); news.HeaderImage = new Image { FileName = filename }; } if (entity.HeaderImage.IsNotNull() && !String.IsNullOrWhiteSpace(news.HeaderImage?.FileName)) { entity.HeaderImage.FileName = news.HeaderImage?.FileName; } else if (!String.IsNullOrWhiteSpace(news.HeaderImage?.FileName)) { entity.HeaderImage = new Image { FileName = news.HeaderImage.FileName }; } var tags = TagHelpers.GetTagsFromString(news.Tags); var allTagEntities = GetAllTagEntitiesInternal(news, tags); TagHelpers.SetTags <News, NewsTag>(tags, entity, allTagEntities); _context.SaveChanges(); return(RedirectToAction("Details", new { Id = entity.Id })); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError)); } }
public async Task <IActionResult> Create([Bind("Title,Text,Published,HeaderImage,Tags")] NewsViewModel news) { try { if (!ModelState.IsValid) { return(View(news)); } var image = Request.Form.Files.SingleOrDefault(f => f.ContentType.Contains("image")); if (image.IsNotNull()) { var filename = await _fileStorageService.SetImageAsync(image); news.HeaderImage = new Image { FileName = filename }; } var username = HttpContext.User.GetUsername(); var displayName = HttpContext.User.GetDisplayName(); if (_context.Users.Find(username) == null) { var user = new User { Username = username, DisplayName = displayName, }; _context.Users.Add(user); } var newNews = new News { Title = news.Title, Text = news.Text, UserId = username, Published = news.Published, Url = CustomUrlHelper.URLFriendly(news.Title), }; if (!String.IsNullOrWhiteSpace(news.HeaderImage?.FileName)) { newNews.HeaderImage = new Image { FileName = news.HeaderImage.FileName }; } newNews.Created = _dateTimeFactory.DateTimeOffsetUtc; var isTitleAndDateUniqe = !_context.News.Any(n => n.Created.Date == newNews.Created.Date && n.Url == newNews.Url); if (!isTitleAndDateUniqe) { ModelState.AddModelError("error", "There has already been created a news with that title today!"); HttpContext.Response.StatusCode = StatusCodes.Status422UnprocessableEntity; return(View(news)); } var tags = TagHelpers.GetTagsFromString(news.Tags); var allTagEntities = GetAllTagEntitiesInternal(news, tags); TagHelpers.SetTags <News, NewsTag>(tags, newNews, allTagEntities); _context.News.Add(newNews); await _context.SaveChangesAsync(); var newsViewModel = new NewsViewModel(newNews); return(RedirectToAction("Details", new { id = newNews.Id })); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError)); } }