コード例 #1
0
 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." }));
     }
 }
コード例 #2
0
        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();
        }
コード例 #3
0
 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));
     }
 }
コード例 #4
0
 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));
 }
コード例 #5
0
        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);
        }
コード例 #6
0
 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);
     }
 }
コード例 #7
0
        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);
        }
コード例 #8
0
        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));
        }