Exemple #1
0
        public async Task <IActionResult> Create(CreateFirstLevelSectionViewModel model)
        {
            if (ModelState.IsValid)
            {
                FirstLevelSection section = await applicationContext.FirstLevelSections.FirstOrDefaultAsync(p => p.Title == model.Title);

                if (section == null)
                {
                    section = new FirstLevelSection {
                        Title = model.Title, PublishDate = DateTime.Now, Slug = model.Title.GenerateSlug()
                    };

                    await applicationContext.FirstLevelSections.AddAsync(section);

                    Event evnt = new Event
                    {
                        Date        = DateTime.Now,
                        Description = $"«{User.Identity.Name}» создал раздел с названием «{section.Title}»"
                    };

                    await applicationContext.Events.AddAsync(evnt);

                    await applicationContext.SaveChangesAsync();

                    return(RedirectToAction("Index", "Sections"));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Раздел с таким названием уже существует");
                }
            }

            return(View(model));
        }
        public void TestGetSectionUsingExtensionMethod()
        {
            IConfigurationRoot configuration = new ConfigurationBuilder()
                                               .SetBasePath(Directory.GetCurrentDirectory())
                                               .AddJsonFile("appsettings.json")
                                               .Build();

            FirstLevelSection firstLevelSection = configuration.GetSection <FirstLevelSection>(); //GetSection<T> is a custom extensionmethod

            Assert.AreEqual("Setting1", firstLevelSection.Setting);
        }
        public void TestGetSection()
        {
            IConfigurationRoot configuration = new ConfigurationBuilder()
                                               .SetBasePath(Directory.GetCurrentDirectory())
                                               .AddJsonFile("appsettings.json")
                                               .Build();

            IConfigurationSection configurationSection = configuration.GetSection(nameof(FirstLevelSection));
            FirstLevelSection     firstLevelSection    = configurationSection.Get <FirstLevelSection>();

            Assert.AreEqual("Setting1", firstLevelSection.Setting);
            Assert.AreEqual("Setting2", firstLevelSection.SecondLevelSection.Setting);
        }
Exemple #4
0
        public async Task <IActionResult> Details(string id, string slug)
        {
            FirstLevelSection section = await applicationContext.FirstLevelSections.FirstOrDefaultAsync(p => p.Id == id);

            if (section != null)
            {
                List <SecondLevelSection> subSections = (from item in applicationContext.SecondLevelSections.ToList()
                                                         where item.Id == id
                                                         select item).ToList();

                return(View(new DetailsFirstLevelSectionViewModel {
                    Title = section.Title, EditDate = section.EditDate != null ? section.EditDate : section.PublishDate, SecondLevelSections = subSections
                }));
            }

            return(NotFound());
        }
Exemple #5
0
        public async Task <IActionResult> ViewSection(string fstLevelSection)
        {
            FirstLevelSection firstLevelSection = await applicationContext.FirstLevelSections.FirstOrDefaultAsync(p => p.Slug == fstLevelSection);

            if (firstLevelSection != null)
            {
                List <SecondLevelSection> sections = (from item in applicationContext.SecondLevelSections
                                                      where item.FirstLevelTitleId == firstLevelSection.Id
                                                      select item).ToList();

                return(View("~/Views/Sections/Details.cshtml", new DetailsFirstLevelSectionViewModel
                {
                    Title = firstLevelSection.Title,
                    EditDate = firstLevelSection.EditDate != null ? firstLevelSection.EditDate : firstLevelSection.PublishDate,
                    SecondLevelSections = sections
                }));
            }

            return(NotFound());
        }