public ActionResult Save(TopicFormViewModel viewModel) { if (!ModelState.IsValid) { return(View("TopicForm", viewModel)); } var topic = viewModel.Topic; topic.TrimNameAndDescription(); // check if topic name is unique if (DoesTopicNameExist(topic)) { ModelState.AddModelError("", "Topic already exists."); return(View("TopicForm", viewModel)); } try { SaveTopic(topic); _fileHelper.SaveTopicIcon(viewModel.PostedFile, topic); return(RedirectToAction("Index", new { selectedTopicId = topic.Id })); } catch (DbEntityValidationException ex) { var error = ex.EntityValidationErrors?.FirstOrDefault()?.ValidationErrors?.FirstOrDefault(); ModelState.AddModelError("", error?.ErrorMessage); return(View("TopicForm", viewModel)); } }
public ActionResult Add() { var topic = new Topic(); var viewModel = new TopicFormViewModel { Topic = topic }; return(View("TopicForm", viewModel)); }
public ActionResult Edit(int id) { var topic = _unitOfWork.TopicRepository.SingleOrDefault(t => t.Id == id); if (topic == null) { return(HttpNotFound()); } var viewModel = new TopicFormViewModel { Topic = topic }; return(View("TopicForm", viewModel)); }
public void Save_ExistingTopic_ShouldUpdateTopicInDatabase() { var topic = _context.AddTestTopicToDatabase(); var viewModel = new TopicFormViewModel { Topic = new Topic { Name = "Test Topic", Description = "Test desc", Id = topic.Id } }; _controller.Save(viewModel); var topicFromDb = _contextAfterAction.Topics.Find(topic.Id); Assert.That(topicFromDb.Name, Is.EqualTo("Test Topic")); Assert.That(topicFromDb.Description, Is.EqualTo("Test desc")); }
public void Save_NewTopicWithExistingTopicName_ShouldNotAddToDatabase() { var topic = _context.AddTestTopicToDatabase(); var newTopic = new Topic { Name = topic.Name, Description = topic.Description + "-" }; var viewModel = new TopicFormViewModel { Topic = newTopic }; _controller.Save(viewModel); var topicFromDb = _contextAfterAction.Topics.Where(t => t.Name == topic.Name); Assert.That(topicFromDb.Count(), Is.EqualTo(1)); }
public void Save_NewTopic_ShouldAddToDatabase() { var topic = new Topic { Name = "Test Topic", Description = "Test desc" }; var viewModel = new TopicFormViewModel { Topic = topic }; _controller.Save(viewModel); var topicFromDb = _contextAfterAction.Topics.SingleOrDefault(t => t.Name == topic.Name); Assert.That(topicFromDb.Id, Is.Not.Null); Assert.That(topicFromDb.Name, Is.EqualTo(topic.Name)); Assert.That(topicFromDb.Description, Is.EqualTo(topic.Description)); }
public async Task <IActionResult> Create(TopicFormViewModel model) { if (!ModelState.IsValid) { return(View(model)); } if (!await SubforumExists(model.SubforumId)) { return(BadRequest()); } model.Content = this.html.Sanitize(model.Content); var authorId = this.userManager.GetUserId(User); await this.topics.CreateAsync(model.Name, model.Content, authorId, model.SubforumId); return(RedirectToAction(nameof(All), new { subforumId = model.SubforumId })); }