Exemplo n.º 1
0
        private static SitePage GetSitePage(DbSitePage p)
        {
            var sitePage = new SitePage()
            {
                Url             = p.Url,
                Created         = p.Created,
                LastModified    = p.LastModified,
                Layout          = p.Layout,
                MetaDescription = p.MetaDescription,
                MetaTitle       = p.MetaTitle,
                ShowInMenu      = p.ShowInMenu,
                SortOrder       = p.SortOrder,
                Template        = p.Template,
                Title           = p.Title,
                Visible         = p.Visible
            };

            sitePage.Sections = p.Items?.GroupBy(i => i.SectionKey).Select(g => new PageSection()
            {
                Key   = g.Key,
                Items = g.Select(i => new ContentItem()
                {
                    Template = i.Template,
                    Page     = sitePage,
                    Values   = JsonConvert.DeserializeObject <Dictionary <string, string> >(i.Values)
                }).ToList() ?? new List <ContentItem>()
            }).ToList() ?? new List <PageSection>();
            return(sitePage);
        }
Exemplo n.º 2
0
        public void StoreSitePage(SitePage sitePage)
        {
            var oldPage = Context.Pages.FirstOrDefault(p => p.Url == sitePage.Url);

            if (oldPage != null)
            {
                oldPage.Template        = sitePage.Template;
                oldPage.Layout          = sitePage.Layout;
                oldPage.Title           = sitePage.Title;
                oldPage.Created         = sitePage.Created;
                oldPage.LastModified    = DateTime.Now;
                oldPage.MetaTitle       = sitePage.MetaTitle;
                oldPage.MetaDescription = sitePage.MetaDescription;
                oldPage.ShowInMenu      = sitePage.ShowInMenu;
                oldPage.SortOrder       = sitePage.SortOrder;
                oldPage.Visible         = sitePage.Visible;
                Context.Pages.Update(oldPage);

                //todo(rc): can this be done without?
                Context.RemoveRange(Context.ContentItems.Where(c => c.PageUrl == sitePage.Url));
                Context.SaveChanges();

                //need to clear because items are removed but still in page.items collection
                oldPage.Items.Clear();

                foreach (var section in sitePage.Sections.Where(s => s.Items.Any()))
                {
                    Context.AddRange(section.Items.Select((item, index) => new DbContentItem()
                    {
                        Template   = item.Template,
                        PageUrl    = sitePage.Url,
                        SectionKey = section.Key,
                        Sortorder  = index + 1,
                        Values     = JsonConvert.SerializeObject(item.Values)
                    }));
                }
            }
            else
            {
                DbSitePage dbPage = new DbSitePage()
                {
                    Url             = sitePage.Url,
                    Template        = sitePage.Template,
                    Layout          = sitePage.Layout,
                    Title           = sitePage.Title,
                    Created         = sitePage.Created,
                    LastModified    = DateTime.Now,
                    MetaTitle       = sitePage.MetaTitle,
                    MetaDescription = sitePage.MetaDescription,
                    ShowInMenu      = sitePage.ShowInMenu,
                    SortOrder       = sitePage.SortOrder,
                    Visible         = sitePage.Visible
                };
                Context.Pages.Add(dbPage);
                foreach (var section in sitePage.Sections.Where(s => s.Items.Any()))
                {
                    Context.AddRange(section.Items.Select((item, index) => new DbContentItem()
                    {
                        Template   = item.Template,
                        PageUrl    = sitePage.Url,
                        SectionKey = section.Key,
                        Sortorder  = index + 1,
                        Values     = JsonConvert.SerializeObject(item.Values)
                    }));
                }
            }
            Context.SaveChanges();
        }