public ActionResult Create([FromBody] ExampleTopic model) { if (ModelState.IsValid) { try { model.Status = TopicStatus.Draft; model.Author = ExampleContext.Current.User; var topicId = _topicService.Add(model); if (topicId != null) { return(RedirectToAction("Edit", "Topic", new { id = topicId.Value })); } ModelState.AddModelError("SectionId", "SectionId is not valid"); } catch (Exception exception) { ExampleContext.Log.Error("TopicController.Create", exception); ModelState.AddModelError(string.Empty, "Unexpected error. Try again."); } } return(View(model)); }
public void Save(ExampleTopic model) { var topic = _topicRepository.FindById(model.Id); if (topic == null) { throw new NullReferenceException(); } topic.Title = model.Title; topic.Text = model.Text; topic.Status = model.Status; if (model.Picture != null) { using (MemoryStream ms = new MemoryStream()) { model.Picture.InputStream.CopyTo(ms); if (topic.PictureId == null) { topic.Picture = new BinaryData(); } topic.Picture.Data = ms.GetBuffer(); topic.Picture.MimeType = model.Picture.ContentType; } } _topicRepository.Update(topic); _provider.SaveChanges(); }
public JsonResult Delete([FromBody] ExampleTopic model) { try { _topicService.Remove(model.Id); return(Json(new { state = true })); } catch (Exception exception) { ExampleContext.Log.Error("TopicController.Delete", exception); return(Json(new { state = false, message = "Error while deleting topic." })); } }
public ActionResult Create(int id) { var section = _sectionService.Find(id); if (section == null) { return(HttpNotFound()); } var topic = new ExampleTopic { Status = TopicStatus.Draft, SectionId = section.Id, Section = section }; return(View(topic)); }
public ActionResult Edit([FromBody] ExampleTopic model) { if (ModelState.IsValid) { try { model.Status = User.IsInRole(UserRoles.Administrator) || User.IsInRole(UserRoles.Moderator) ? TopicStatus.Approved : TopicStatus.NotApproved; _topicService.Save(model); return(RedirectToAction("Detail", new { id = model.Id })); } catch (Exception exception) { ExampleContext.Log.Error(exception); ModelState.AddModelError(string.Empty, "Unexpected error. Try again."); } } return(View(model)); }
public int?Add(ExampleTopic model) { try { var entity = new Topic { Title = model.Title, Status = model.Status, SectionId = model.SectionId, AuthorId = model.Author.Id }; var newEntity = _topicRepository.Insert(entity); _provider.SaveChanges(); return(newEntity.Id); } catch (Exception exception) { ExampleContext.Log.Error("TopicService.Add", exception); return(null); } }