public JsonResult Delete([FromBody] ExampleSection model) { try { _sectionService.Remove(model.Id); return(Json(new { state = true })); } catch (Exception exception) { ExampleContext.Log.Error("SectionController.Delete", exception); return(Json(new { state = false, message = "Error while deleting section." })); } }
public void Save(ExampleSection model) { var section = _sectionRepository.FindById(model.Id); if (section == null) { throw new NullReferenceException(); } section.Title = model.Title; section.Description = model.Description; _sectionRepository.Update(section); _provider.SaveChanges(); }
public ActionResult Create([FromBody] ExampleSection model) { try { var id = _sectionService.Add(model); if (id == null) { return(View(model)); } return(RedirectToAction("Index", "Forum", new { id = id })); } catch (Exception) { return(View(model)); } }
public ActionResult Edit([FromBody] ExampleSection model) { if (ModelState.IsValid) { try { _sectionService.Save(model); return(RedirectToAction("Index", "Forum", new { id = model.Id })); } catch (Exception exception) { ExampleContext.Log.Error(exception); ModelState.AddModelError(string.Empty, "Unexpected error. Try again."); } } return(View(model)); }
public ExampleSection GetRootSections() { ExampleSection forum = new ExampleSection(); var collection = _sectionRepository.AsQueryable().Where(s => s.ParentId == null).ToList(); forum.ChildSections = collection.Select(s => new ExampleSection { Id = s.Id, Title = s.Title, Description = s.Description, CreatedDate = s.CreatedDate, ModifiedDate = s.ModifiedDate }) .ToList(); return(forum); }
public int?Add(ExampleSection model) { try { var entity = new Section { Title = model.Title, Description = model.Description, ParentId = model.ParentSectionId }; var newEntity = _sectionRepository.Insert(entity); _provider.SaveChanges(); return(newEntity.Id); } catch (Exception) { return(null); } }
public ExampleSection Find(int id) { var entity = _sectionRepository.FindById(id); if (entity == null) { return(null); } var section = new ExampleSection(entity, true); if (entity.Topics != null && entity.Topics.Any()) { section.Topics = entity.Topics.Select(t => new ExampleTopic(t, section)).ToList(); } else { section.Topics = new List <ExampleTopic>(); } return(section); }
public async Task <ActionResult> Create(int?id) { if (id == null || id == default(int)) { return(View(new ExampleSection())); } var section = await _sectionService.FindByIdAsync(id.Value); if (section == null) { return(HttpNotFound()); } var newSection = new ExampleSection { ParentSectionId = id, ParentSection = new ExampleSection(section, false) }; return(View(newSection)); }