public void Remove(Post post) { oxite_Post persistencePost = context.oxite_Posts.Where(p => p.PostID == post.ID).FirstOrDefault(); if (persistencePost != null) { persistencePost.State = (byte)EntityState.Removed; context.SubmitChanges(); } }
private Page getParentPage(oxite_Post post) { return(( from pr in context.oxite_PostRelationships join p in context.oxite_Posts on pr.ParentPostID equals p.PostID where pr.PostID == post.PostID && pr.ParentPostID != post.PostID select new Page { ID = p.PostID, Slug = p.Slug, Parent = getParentPage(p) } ).FirstOrDefault()); }
public void Remove(Page page) { if (page.ID == Guid.Empty) { throw new ArgumentException("ID is required", "page"); } oxite_Post persistencePost = context.oxite_Posts.Where(p => p.PostID == page.ID).FirstOrDefault(); if (persistencePost != null) { persistencePost.State = (byte)EntityState.Removed; context.SubmitChanges(); } }
private static Post projectPost(oxite_Post p, oxite_User u, oxite_Area a, IQueryable <Comment> c, IQueryable <File> f, IQueryable <Tag> t, IQueryable <Trackback> tb) { return(new Post { ID = p.PostID, Creator = u != null ? new User { DisplayName = u.DisplayName, Email = u.Email, HashedEmail = u.HashedEmail, ID = u.UserID, LanguageDefault = new Language { ID = u.oxite_Language.LanguageID, DisplayName = u.oxite_Language.LanguageDisplayName, Name = u.oxite_Language.LanguageName }, Name = u.Username, Status = u.Status, } : null, Area = a != null ? new Area { ID = a.AreaID, Name = a.AreaName, DisplayName = a.DisplayName, Description = a.Description, CommentingDisabled = a.CommentingDisabled, Created = a.CreatedDate, Modified = a.ModifiedDate } : null, Body = p.Body, BodyShort = p.BodyShort, Comments = new LazyList <Comment>(c), Created = p.CreatedDate, Files = f.ToList(), Modified = p.ModifiedDate, Published = p.PublishedDate != SqlDateTime.MaxValue.Value ? p.PublishedDate : (DateTime?)null, Slug = p.Slug, State = (EntityState)p.State, Tags = t.ToList(), Title = p.Title, Trackbacks = tb.ToList(), CommentingDisabled = p.CommentingDisabled }); }
public void Save(Page page) { oxite_Post postToSave = null; oxite_PostRelationship parentPostRelationship = null; if (page.ID != Guid.Empty) { postToSave = context.oxite_Posts.Where(p => p.PostID == page.ID).FirstOrDefault(); parentPostRelationship = context.oxite_PostRelationships.Where(pr => pr.SiteID == siteID && pr.PostID == page.ID).FirstOrDefault(); } if (postToSave == null) { postToSave = new oxite_Post { PostID = Guid.NewGuid() }; context.oxite_Posts.InsertOnSubmit(postToSave); } postToSave.Body = page.Body; postToSave.BodyShort = page.BodyShort ?? ""; postToSave.CreatedDate = page.Created ?? DateTime.UtcNow; postToSave.ModifiedDate = DateTime.UtcNow; postToSave.PublishedDate = page.Published ?? SqlDateTime.MaxValue.Value; postToSave.Slug = page.Slug; postToSave.State = (byte)page.State; postToSave.Title = page.Title; postToSave.SearchBody = page.Body; // set the parent page if (page.Parent == null || page.Parent.ID == Guid.Empty) { if (parentPostRelationship != null) { if (parentPostRelationship.ParentPostID != page.ID) { context.oxite_PostRelationships.DeleteOnSubmit(parentPostRelationship); parentPostRelationship = new oxite_PostRelationship { SiteID = siteID, PostID = postToSave.PostID, ParentPostID = page.ID }; context.oxite_PostRelationships.InsertOnSubmit(parentPostRelationship); } } else { parentPostRelationship = new oxite_PostRelationship { SiteID = siteID, PostID = postToSave.PostID, ParentPostID = postToSave.PostID }; context.oxite_PostRelationships.InsertOnSubmit(parentPostRelationship); } } else { if (parentPostRelationship != null) { if (parentPostRelationship.ParentPostID != page.Parent.ID) { context.oxite_PostRelationships.DeleteOnSubmit(parentPostRelationship); parentPostRelationship = new oxite_PostRelationship { SiteID = siteID, PostID = postToSave.PostID, ParentPostID = page.Parent.ID }; context.oxite_PostRelationships.InsertOnSubmit(parentPostRelationship); } } else { parentPostRelationship = new oxite_PostRelationship { SiteID = siteID, PostID = postToSave.PostID, ParentPostID = page.Parent.ID }; context.oxite_PostRelationships.InsertOnSubmit(parentPostRelationship); } } // The associated user but not changes to the user itself oxite_User user = context.oxite_Users.Where(u => u.Username.ToLower() == page.Creator.Name.ToLower()).FirstOrDefault(); if (user == null) { throw new InvalidOperationException(string.Format("User {0} could not be found", page.Creator.Name)); } if (postToSave.CreatorUserID != user.UserID) { postToSave.oxite_User = user; } context.SubmitChanges(); }
private bool getHasChildren(oxite_Post post) { return((from pr in context.oxite_PostRelationships where pr.ParentPostID == post.PostID select pr).Any()); }
public void Save(Post post) { using (TransactionScope scope = new TransactionScope()) { oxite_Post postToSave = null; bool postIsNew = false; Guid postID = post.ID; if (post.ID != Guid.Empty) { postToSave = context.oxite_Posts.Where(p => p.PostID == post.ID).FirstOrDefault(); } if (postToSave == null) { if (post.ID == Guid.Empty) { postID = Guid.NewGuid(); } postToSave = new oxite_Post { PostID = postID }; context.oxite_Posts.InsertOnSubmit(postToSave); postIsNew = true; } postToSave.Body = post.Body; postToSave.BodyShort = post.BodyShort; postToSave.CreatedDate = post.Created ?? DateTime.UtcNow; postToSave.ModifiedDate = DateTime.UtcNow; postToSave.PublishedDate = post.Published ?? SqlDateTime.MaxValue.Value; postToSave.Slug = post.Slug; postToSave.State = (byte)post.State; postToSave.Title = post.Title; postToSave.CommentingDisabled = post.CommentingDisabled; // Tags: Use existing, create new ones if needed. Don't edit old tags foreach (Tag tag in post.Tags) { string normalizedName = normalizeTagName(tag.Name); oxite_Tag persistenceTag = context.oxite_Tags.Where(t => t.TagName.ToLower() == normalizedName.ToLower()).FirstOrDefault(); if (persistenceTag == null) { Guid newTagID = Guid.NewGuid(); persistenceTag = new oxite_Tag { TagName = normalizedName, CreatedDate = tag.Created.HasValue ? tag.Created.Value : DateTime.UtcNow, TagID = newTagID, ParentTagID = newTagID }; context.oxite_Tags.InsertOnSubmit(persistenceTag); } if (!context.oxite_PostTagRelationships.Where(pt => pt.PostID == postToSave.PostID && pt.TagID == persistenceTag.TagID).Any()) { context.oxite_PostTagRelationships.InsertOnSubmit(new oxite_PostTagRelationship { PostID = postToSave.PostID, TagID = persistenceTag.TagID, TagDisplayName = tag.DisplayName ?? tag.Name }); } } var updatedTagNames = post.Tags.Select(t => normalizeTagName(t.Name).ToLower()); var tagsRemoved = from t in context.oxite_Tags join pt in context.oxite_PostTagRelationships on t.TagID equals pt.TagID where pt.PostID == postToSave.PostID && !updatedTagNames.Contains(t.TagName.ToLower()) select pt; context.oxite_PostTagRelationships.DeleteAllOnSubmit(tagsRemoved); // Files: Use existing, create new ones if needed. Edit display name and mimetype of old files if (post.Files != null) { foreach (File file in post.Files) { oxite_File persistenceFile = context.oxite_Files.Where(f => f.PostID == postToSave.PostID && string.Compare(f.Url, file.Url.ToString(), true) == 0).FirstOrDefault(); if (persistenceFile == null) { persistenceFile = new oxite_File() { PostID = postToSave.PostID, Url = file.Url.ToString(), ID = Guid.NewGuid() }; context.oxite_Files.InsertOnSubmit(persistenceFile); } persistenceFile.DisplayName = file.DisplayName; persistenceFile.Length = file.SizeInBytes; persistenceFile.MimeType = file.MimeType; } var updatedFileUrls = post.Files.Select(f => f.Url.ToString()); var filesRemoved = from f in context.oxite_Files where f.PostID == post.ID && !updatedFileUrls.Contains(f.Url) select f; context.oxite_Files.DeleteAllOnSubmit(filesRemoved); } // The area associated with the post but not changes to the area itself oxite_Area area = post.Area.ID == Guid.Empty ? context.oxite_Areas.Where(a => a.AreaName.ToLower() == post.Area.Name.ToLower()).FirstOrDefault() : context.oxite_Areas.Where(a => a.AreaID == post.Area.ID).FirstOrDefault(); if (area == null) { throw new InvalidOperationException(string.Format("Area {0} could not be found.", post.Area.Name ?? post.Area.ID.ToString())); } if (postIsNew && (from p in context.oxite_Posts join ap in context.oxite_PostAreaRelationships on p.PostID equals ap.PostID where ap.AreaID == area.AreaID && p.Slug == postToSave.Slug select p).Any()) { throw new InvalidOperationException(string.Format("There is already a post with slug {0} in area {1}.", post.Slug, area.AreaName)); } if (postToSave.oxite_PostAreaRelationships.Count == 0) { context.oxite_PostAreaRelationships.InsertOnSubmit(new oxite_PostAreaRelationship { AreaID = area.AreaID, PostID = postToSave.PostID }); } else { oxite_PostAreaRelationship areaMapping = context.oxite_PostAreaRelationships.Where(pa => pa.PostID == postToSave.PostID).FirstOrDefault(); areaMapping.AreaID = area.AreaID; } // The associated user but not changes to the user itself oxite_User user = context.oxite_Users.Where(u => u.Username.ToLower() == post.Creator.Name.ToLower()).FirstOrDefault(); if (user == null) { throw new InvalidOperationException(string.Format("User {0} could not be found", post.Creator.Name)); } if (postToSave.CreatorUserID != user.UserID) { postToSave.oxite_User = user; } postToSave.SearchBody = postToSave.Title + postToSave.Body + postToSave.oxite_User.Username + postToSave.oxite_User.DisplayName + string.Join("", post.Tags.Select(t => t.Name + t.DisplayName).ToArray()); context.SubmitChanges(); scope.Complete(); post.ID = postID; } }