private static Post projectPost(oxite_Blogs_Post p, oxite_User u, oxite_Blogs_Blog b, IQueryable <PostComment> c, IQueryable <oxite_Blogs_PostTagRelationship> t, IQueryable <Trackback> tb)
        {
            Blog blog = new Blog(b.SiteID, b.CommentingDisabled, b.CreatedDate, b.Description, b.DisplayName, b.BlogID, b.ModifiedDate, b.BlogName);

            User creator =
                u != null
                ? new User(u.UserID, u.Username, u.DisplayName, u.Email, u.HashedEmail, new Language(u.oxite_Language.LanguageID)
            {
                Name = u.oxite_Language.LanguageName, DisplayName = u.oxite_Language.LanguageDisplayName
            }, (EntityState)u.Status)
                : null;

            return(new Post(blog, p.Body, p.BodyShort, p.CommentingDisabled, p.CreatedDate, creator, p.PostID, p.ModifiedDate, p.PublishedDate, p.Slug, (EntityState)p.State, t.Select(tag => new PostTag(tag.TagID, tag.TagDisplayName)).ToList(), p.Title, c.ToList(), tb.ToList()));
        }
        public bool RemovePost(Post post)
        {
            oxite_Blogs_Post foundPost   = context.oxite_Blogs_Posts.FirstOrDefault(p => p.PostID == post.ID);
            bool             removedPost = false;

            if (foundPost != null)
            {
                foundPost.State = (byte)EntityState.Removed;

                context.SubmitChanges();

                removedPost = true;
            }

            return(removedPost);
        }
        public PostComment Save(PostComment comment, Guid siteID, string blogName, string postSlug)
        {
            oxite_Comment commentToSave = null;

            if (comment.ID != Guid.Empty)
            {
                commentToSave = context.oxite_Comments.FirstOrDefault(c => c.CommentID == comment.ID);
            }

            if (commentToSave == null)
            {
                commentToSave = new oxite_Comment();

                commentToSave.CommentID   = comment.ID != Guid.Empty ? comment.ID : Guid.NewGuid();
                commentToSave.CreatedDate = commentToSave.ModifiedDate = DateTime.UtcNow;

                context.oxite_Comments.InsertOnSubmit(commentToSave);
            }
            else
            {
                commentToSave.ModifiedDate = DateTime.UtcNow;
            }

            oxite_Blogs_Post post = (from p in context.oxite_Blogs_Posts join b in context.oxite_Blogs_Blogs on p.BlogID equals b.BlogID where b.SiteID == siteID && string.Compare(b.BlogName, blogName, true) == 0 && string.Compare(p.Slug, postSlug, true) == 0 select p).FirstOrDefault();

            if (post == null)
            {
                throw new InvalidOperationException(string.Format("Post in blog {0} at slug {1} could not be found to add the comment to", blogName, postSlug));
            }
            context.oxite_Blogs_PostCommentRelationships.InsertOnSubmit(
                new oxite_Blogs_PostCommentRelationship
            {
                CommentID = commentToSave.CommentID,
                PostID    = post.PostID,
                Slug      = comment.Slug
            }
                );

            commentToSave.ParentCommentID = comment.Parent != null && comment.Parent.ID != Guid.Empty ? comment.Parent.ID : commentToSave.CommentID;
            commentToSave.Body            = comment.Body;
            commentToSave.CreatorIP       = comment.CreatorIP;
            commentToSave.State           = (byte)comment.State;
            commentToSave.UserAgent       = comment.CreatorUserAgent;
            commentToSave.oxite_Language  = context.oxite_Languages.Where(l => l.LanguageName == comment.Language.Name).FirstOrDefault();

            if (comment.CreatorUserID != Guid.Empty)
            {
                commentToSave.CreatorUserID = comment.CreatorUserID;
            }
            else
            {
                oxite_User anonymousUser = context.oxite_Users.FirstOrDefault(u => u.Username == "Anonymous");
                if (anonymousUser == null)
                {
                    throw new InvalidOperationException("Could not find anonymous user");
                }
                commentToSave.CreatorUserID = anonymousUser.UserID;

                commentToSave.CreatorName        = comment.CreatorName;
                commentToSave.CreatorEmail       = comment.CreatorEmail;
                commentToSave.CreatorHashedEmail = comment.CreatorEmailHash;
                commentToSave.CreatorUrl         = comment.CreatorUrl;
            }

            context.SubmitChanges();

            return((
                       from c in context.oxite_Comments
                       join pcr in context.oxite_Blogs_PostCommentRelationships on c.CommentID equals pcr.CommentID
                       join p in context.oxite_Blogs_Posts on pcr.PostID equals p.PostID
                       join b in context.oxite_Blogs_Blogs on p.BlogID equals b.BlogID
                       join u in context.oxite_Users on c.CreatorUserID equals u.UserID
                       where b.SiteID == siteID && string.Compare(b.BlogName, blogName, true) == 0 && string.Compare(p.Slug, postSlug, true) == 0 && string.Compare(pcr.Slug, comment.Slug, true) == 0
                       select projectComment(c, pcr, p, b, u)
                       ).FirstOrDefault());
        }
        public Post Save(Post post)
        {
            oxite_Blogs_Post postToSave = null;

            if (post.ID != Guid.Empty)
            {
                postToSave = context.oxite_Blogs_Posts.FirstOrDefault(p => p.PostID == post.ID);
            }

            if (postToSave == null)
            {
                postToSave = new oxite_Blogs_Post();

                postToSave.PostID      = post.ID != Guid.Empty ? post.ID : Guid.NewGuid();
                postToSave.CreatedDate = postToSave.ModifiedDate = DateTime.UtcNow;

                context.oxite_Blogs_Posts.InsertOnSubmit(postToSave);
            }
            else
            {
                postToSave.ModifiedDate = DateTime.UtcNow;
            }

            postToSave.Body               = post.Body;
            postToSave.BodyShort          = post.BodyShort;
            postToSave.PublishedDate      = post.Published;
            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)
            {
                oxite_Tag persistenceTag = context.oxite_Tags.Where(t => string.Compare(t.TagName, tag.Name, true) == 0).FirstOrDefault();

                if (persistenceTag == null)
                {
                    Guid newTagID = Guid.NewGuid();
                    persistenceTag = new oxite_Tag {
                        TagName = tag.Name, CreatedDate = DateTime.UtcNow, TagID = newTagID, ParentTagID = newTagID
                    };
                    context.oxite_Tags.InsertOnSubmit(persistenceTag);
                }

                if (!context.oxite_Blogs_PostTagRelationships.Any(pt => pt.PostID == postToSave.PostID && pt.TagID == persistenceTag.TagID))
                {
                    context.oxite_Blogs_PostTagRelationships.InsertOnSubmit(new oxite_Blogs_PostTagRelationship {
                        PostID = postToSave.PostID, TagID = persistenceTag.TagID, TagDisplayName = tag.DisplayName ?? tag.Name
                    });
                }
            }

            var tagsRemoved = from t in context.oxite_Tags
                              join pt in context.oxite_Blogs_PostTagRelationships on t.TagID equals pt.TagID
                              where pt.PostID == postToSave.PostID && !post.Tags.Select(tag => tag.Name.ToLower()).Contains(t.TagName.ToLower())
                              select pt;

            context.oxite_Blogs_PostTagRelationships.DeleteAllOnSubmit(tagsRemoved);

            if (post.Blog == null || string.IsNullOrEmpty(post.Blog.Name))
            {
                throw new InvalidOperationException("No blog was specified");
            }
            oxite_Blogs_Blog blog = context.oxite_Blogs_Blogs.FirstOrDefault(b => string.Compare(b.BlogName, post.Blog.Name, true) == 0);

            if (blog == null)
            {
                throw new InvalidOperationException(string.Format("Blog {0} could not be found", post.Blog.Name));
            }
            postToSave.BlogID = blog.BlogID;

            oxite_User user = context.oxite_Users.FirstOrDefault(u => u.Username == post.Creator.Name);

            if (user == null)
            {
                throw new InvalidOperationException(string.Format("User {0} could not be found", post.Creator.Name));
            }
            postToSave.CreatorUserID = user.UserID;

            //TODO: (erikpo) Add an item to the search index
            //postToSave.SearchBody = postToSave.Title + string.Join("", post.Tags.Select(t => t.Name + t.DisplayName).ToArray()) + postToSave.Body + user.DisplayName + user.Username;

            context.SubmitChanges();

            return(GetPost(postToSave.PostID));
        }
        private PostComment projectComment(oxite_Comment comment, oxite_Blogs_PostCommentRelationship pcr, oxite_Blogs_Post p, oxite_Blogs_Blog b, oxite_User user)
        {
            PostCommentSmall parent   = comment.ParentCommentID != comment.CommentID ? getParentComment(comment.ParentCommentID) : null;
            Language         language = new Language(comment.oxite_Language.LanguageID)
            {
                DisplayName = comment.oxite_Language.LanguageDisplayName,
                Name        = comment.oxite_Language.LanguageName
            };

            if (user.Username != "Anonymous")
            {
                return(new PostComment(comment.Body, comment.CreatedDate, getUserAuthenticated(comment, user), comment.CreatorIP, comment.UserAgent, comment.CommentID, language, comment.ModifiedDate, parent, new PostSmall(p.PostID, b.BlogName, p.Slug, p.Title), pcr.Slug, (EntityState)comment.State));
            }
            else
            {
                return(new PostComment(comment.Body, comment.CreatedDate, getUserAnonymous(comment, user), comment.CreatorIP, comment.UserAgent, comment.CommentID, language, comment.ModifiedDate, parent, new PostSmall(p.PostID, b.BlogName, p.Slug, p.Title), pcr.Slug, (EntityState)comment.State));
            }
        }