Beispiel #1
0
        public async Task CreateMenu()
        {
            var site = await GetSite();

            var menu = new Menu()
            {
                Name   = "TopMenu",
                SiteId = site.Id,
                Items  = new List <MenuItem>()
                {
                    new MenuItem()
                    {
                        Link = "/home", Text = "Home"
                    },
                    new MenuItem()
                    {
                        Link = "/about", Text = "About"
                    }
                },
                IsActive = true
            };

            var result = await _menusService.SaveAsync(menu, TEST_USER);

            Assert.NotNull(result);

            Assert.True(result.Succeeded);
            Assert.True(result.NewId > 0);
        }
Beispiel #2
0
        public async Task <ActionResult> Post(Menu menu)
        {
            Result result = null;

            try
            {
                result = await _menuService.SaveAsync(menu, GetCurrentUserName());

                if (!result.Succeeded)
                {
                    ThrowException(result);
                }

                return(Created("", result.NewId));
            }
            catch (Exception ex)
            {
                _logger.LogError($"POST Error: {ex.Message}, Stack: {ex.StackTrace}");
                return(BadRequest(ex.Message));
            }
        }
Beispiel #3
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            var toRun = _configuration.GetValue <bool>("General:SeedData");

            if (!toRun)
            {
                return;
            }

            metaDatas.Add("description", new MetaData()
            {
                Value = "Demo description", Key = "description"
            });
            try
            {
                // create site
                var site = new Site()
                {
                    Name = "Demo", Host = "www.domain.com", Language = Languages.English, IsActive = true
                };
                var siteCreationResult = await _siteService.SaveAsync(site, USER);

                if (!siteCreationResult.Succeeded)
                {
                    throw new Exception("Site was not created");
                }

                // create page componet contact form
                var contactForm = new Content()
                {
                    Name = "ContactForm", SiteId = siteCreationResult.NewId, Type = ContentTypes.PageComponent, IsActive = true
                };
                var contentCreationResult = await _contentService.SaveAsync(contactForm, USER);

                if (!contentCreationResult.Succeeded)
                {
                    throw new Exception("Content was not created");
                }

                contactForm.Id = contentCreationResult.NewId;

                // create master page
                var masterPageCreationResult = await _pageService.SaveAsync(new Page()
                {
                    Name = "Layout", Slug = "layout", SiteId = siteCreationResult.NewId, PageType = PageTypes.MasterPage, IsActive = true, MetaData = new Dictionary <string, MetaData>()
                }, USER);

                if (!masterPageCreationResult.Succeeded)
                {
                    throw new Exception("Master page was not created");
                }

                // create menu
                var menuCreationResult = await _menuService.SaveAsync(new Menu()
                {
                    SiteId = siteCreationResult.NewId, Name = "TopMenu", IsActive = true, Description = "Top menu"
                }, USER);

                if (!menuCreationResult.Succeeded)
                {
                    throw new Exception("Menu was not created");
                }


                // create pages home, about, contactform
                var pages = new List <Page>()
                {
                    new Page()
                    {
                        MasterPageId = masterPageCreationResult.NewId, Name = "Home", Slug = "home", SiteId = siteCreationResult.NewId, Title = "Demo homepage", IsActive = true, PageType = PageTypes.ContentPage,
                        Contents     = new List <Content>()
                        {
                            new Content()
                            {
                                Name = $"content-home", Body = "<p>Weclome to demo home page</p>", Type = ContentTypes.InlineHTML, IsActive = true, SiteId = siteCreationResult.NewId
                            }
                        },
                        MetaData = metaDatas
                    },
                    new Page()
                    {
                        MasterPageId = masterPageCreationResult.NewId, Name = "About", Slug = "about", SiteId = siteCreationResult.NewId, Title = "About", IsActive = true, PageType = PageTypes.ContentPage,
                        Contents     = new List <Content>()
                        {
                            new Content()
                            {
                                Name = $"content-about", Body = "<p>About us</p>", Type = ContentTypes.InlineHTML, IsActive = true, SiteId = siteCreationResult.NewId
                            }
                        },
                        MetaData = metaDatas
                    },
                    new Page()
                    {
                        MasterPageId = masterPageCreationResult.NewId, Name = "Contactus", Slug = "contact", SiteId = siteCreationResult.NewId, Title = "Contact us", IsActive = true, PageType = PageTypes.ContentPage,
                        Contents     = new List <Content>()
                        {
                            contactForm
                        },
                        MetaData = metaDatas
                    }
                };

                byte i = 1;
                foreach (var page in pages)
                {
                    var pageCreationResult = await _pageService.SaveAsync(page, USER);

                    if (!pageCreationResult.Succeeded)
                    {
                        throw new Exception("Page not created");
                    }

                    var menuItem = new MenuItem()
                    {
                        Text     = page.Name,
                        Link     = $"/{site.Language.Code}/{page.Slug}",
                        MenuId   = menuCreationResult.NewId,
                        Alt      = page.Name,
                        IsActive = true,
                        Order    = i
                    };

                    var meniItemCreationResult = await _menuService.SaveItemAsync(menuItem, USER);

                    if (!meniItemCreationResult.Succeeded)
                    {
                        throw new Exception("Menu item was not created");
                    }
                    i++;
                }


                // create menu
            }
            catch (Exception ex)
            {
                _logger.LogError($"Seeding data error: {ex.Message}, stack: {ex.StackTrace}");
            }
        }