private UserAuthenticated projectUser(Guid siteID, oxite_User user) { var siteRoles = from sru in context.oxite_SiteRoleUserRelationships join r in context.oxite_Roles on sru.RoleID equals r.RoleID where sru.SiteID == siteID && sru.UserID == user.UserID select projectRoleRecursive(r, RoleType.Site); //var blogRoles = // from aru in context.oxite_BlogRoleUserRelationships // join r in context.oxite_Roles on aru.RoleID equals r.RoleID // where aru.UserID == user.UserID // select new KeyValuePair<Guid, Role>(aru.BlogID, projectRoleRecursive(r, RoleType.Blog)); //var postRoles = // from pru in context.oxite_PostRoleUserRelationships // join r in context.oxite_Roles on pru.RoleID equals r.RoleID // where pru.UserID == user.UserID // select new KeyValuePair<Guid, Role>(pru.PostID, projectRoleRecursive(r, RoleType.Post)); //var pageRoles = // from pru in context.oxite_PageRoleUserRelationships // join r in context.oxite_Roles on pru.RoleID equals r.RoleID // where pru.UserID == user.UserID // select new KeyValuePair<Guid, Role>(pru.PageID, projectRoleRecursive(r, RoleType.Page)); return(new UserAuthenticated(user.UserID, user.Username, user.DisplayName, user.Email, user.HashedEmail, (EntityState)user.Status, siteRoles.ToList(), Enumerable.Empty <KeyValuePair <Guid, Role> >(), Enumerable.Empty <KeyValuePair <Guid, Role> >(), Enumerable.Empty <KeyValuePair <Guid, Role> >())); }
private static UserBase getUser(oxite_User user) { return(new User { DisplayName = user.DisplayName, Email = user.Email, HashedEmail = user.HashedEmail, ID = user.UserID, Name = user.Username, Password = user.Password, PasswordSalt = user.PasswordSalt, Status = user.Status, }); }
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())); }
private static UserBase getUser(oxite_CommentAnonymous commentAnonymous, oxite_User user) { if (commentAnonymous == null) { return(getUser(user)); } return(new UserBase { Name = commentAnonymous.Name, Email = commentAnonymous.Email, HashedEmail = commentAnonymous.HashedEmail, Url = commentAnonymous.Url }); }
public bool Remove(Guid userID) { oxite_User foundUser = context.oxite_Users.FirstOrDefault(u => u.UserID == userID && u.Status != (byte)EntityState.Removed); bool removedUser = false; if (foundUser != null) { foundUser.Status = (byte)EntityState.Removed; context.SubmitChanges(); removedUser = true; } return(removedUser); }
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 UserAuthenticated Save(UserAuthenticated user, Guid siteID) { oxite_User userToSave = null; if (user.ID != Guid.Empty) { userToSave = context.oxite_Users.FirstOrDefault(u => u.UserID == user.ID); } if (userToSave == null) { userToSave = new oxite_User(); userToSave.UserID = user.ID != Guid.Empty ? user.ID : Guid.NewGuid(); context.oxite_Users.InsertOnSubmit(userToSave); } //TODO: (erikpo) Move these fields into a profile table (except maybe username?) if (user.LanguageDefault != null && user.LanguageDefault.ID != Guid.Empty) { userToSave.DefaultLanguageID = user.LanguageDefault.ID; } else { userToSave.DefaultLanguageID = context.oxite_Languages.Where(l => l.LanguageName == "en").First().LanguageID; } userToSave.Username = user.Name; userToSave.DisplayName = user.DisplayName; userToSave.Email = user.Email; userToSave.HashedEmail = user.EmailHash; userToSave.Status = (byte)user.Status; //TODO: (erikpo) Add url fields context.SubmitChanges(); return(GetUser(siteID, userToSave.UserID)); }
public void Save(User user) { oxite_User dbUser = null; Guid userID = user.ID; if (userID != Guid.Empty) { dbUser = (from u in context.oxite_Users where u.UserID == userID select u).FirstOrDefault(); } if (dbUser == null) { if (userID == Guid.Empty) { userID = Guid.NewGuid(); } dbUser = new oxite_User(); dbUser.UserID = userID; context.oxite_Users.InsertOnSubmit(dbUser); } dbUser.DefaultLanguageID = (from l in context.oxite_Languages where l.LanguageName == (site.LanguageDefault ?? "en") select l).First().LanguageID; dbUser.Username = user.Name; dbUser.DisplayName = user.DisplayName; dbUser.Email = user.Email; dbUser.HashedEmail = user.HashedEmail; dbUser.Password = user.Password; dbUser.PasswordSalt = user.PasswordSalt; dbUser.Status = user.Status; context.SubmitChanges(); user.ID = userID; }
private static PostCommentSmall projectPostCommentSmall(oxite_Comment comment, oxite_Blogs_PostCommentRelationship pcr, oxite_User user) { if (user.Username != "Anonymous") { return(new PostCommentSmall(new CommentSmall(comment.CommentID, comment.CreatedDate, getUserAuthenticated(comment, user)), pcr.Slug)); } else { return(new PostCommentSmall(new CommentSmall(comment.CommentID, comment.CreatedDate, getUserAnonymous(comment, user)), pcr.Slug)); } }
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(); }
public ScheduleItemComment Save(ScheduleItemComment comment) { 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_Conferences_ScheduleItem scheduleItem = (from si in context.oxite_Conferences_ScheduleItems join e in context.oxite_Conferences_Events on si.EventID equals e.EventID where /*e.SiteID == siteID && */ string.Compare(e.EventName, comment.ScheduleItem.EventName, true) == 0 && string.Compare(si.Slug, comment.ScheduleItem.Slug, true) == 0 select si).FirstOrDefault(); if (scheduleItem == null) { throw new InvalidOperationException(string.Format("ScheduleItem in Event {0} at slug {1} could not be found to add the comment to", comment.ScheduleItem.EventName, comment.ScheduleItem.Slug)); } context.oxite_Conferences_ScheduleItemCommentRelationships.InsertOnSubmit( new oxite_Conferences_ScheduleItemCommentRelationship { CommentID = commentToSave.CommentID, ScheduleItemID = scheduleItem.ScheduleItemID, 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 sicr in context.oxite_Conferences_ScheduleItemCommentRelationships on c.CommentID equals sicr.CommentID join si in context.oxite_Conferences_ScheduleItems on sicr.ScheduleItemID equals si.ScheduleItemID join e in context.oxite_Conferences_Events on si.EventID equals e.EventID join u in context.oxite_Users on c.CreatorUserID equals u.UserID where /*e.SiteID == siteID && */ string.Compare(e.EventName, comment.ScheduleItem.EventName, true) == 0 && string.Compare(si.Slug, scheduleItem.Slug, true) == 0 && string.Compare(sicr.Slug, comment.Slug, true) == 0 select projectComment(c, sicr, si, e, 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 static UserAnonymous getUserAnonymous(oxite_Comment comment, oxite_User user) { return(new UserAnonymous(comment.CreatorName, comment.CreatorEmail, comment.CreatorHashedEmail, comment.CreatorUrl)); }
private static ScheduleItemCommentSmall projectScheduleItemCommentSmall(oxite_Comment comment, oxite_Conferences_ScheduleItemCommentRelationship sicr, oxite_User user) { if (user.Username != "Anonymous") { return(new ScheduleItemCommentSmall(new CommentSmall(comment.CommentID, comment.CreatedDate, getUserAuthenticated(comment, user)), sicr.Slug)); } else { return(new ScheduleItemCommentSmall(new CommentSmall(comment.CommentID, comment.CreatedDate, getUserAnonymous(comment, user)), sicr.Slug)); } }
private ScheduleItemComment projectComment(oxite_Comment comment, oxite_Conferences_ScheduleItemCommentRelationship sicr, oxite_Conferences_ScheduleItem si, oxite_Conferences_Event e, oxite_User user) { ScheduleItemCommentSmall 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 ScheduleItemComment(comment.Body, comment.CreatedDate, getUserAuthenticated(comment, user), comment.CreatorIP, comment.UserAgent, comment.CommentID, language, comment.ModifiedDate, parent, new ScheduleItemSmall(si.ScheduleItemID, e.EventName, si.Slug, si.Title), sicr.Slug, (EntityState)comment.State)); } else { return(new ScheduleItemComment(comment.Body, comment.CreatedDate, getUserAnonymous(comment, user), comment.CreatorIP, comment.UserAgent, comment.CommentID, language, comment.ModifiedDate, parent, new ScheduleItemSmall(si.ScheduleItemID, e.EventName, si.Slug, si.Title), sicr.Slug, (EntityState)comment.State)); } }
private static User getUserAuthenticated(oxite_Comment comment, oxite_User user) { return(new User(user.UserID, user.Username, user.DisplayName, user.Email, user.HashedEmail, (EntityState)user.Status)); }
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()); }
private static UserBase getUser(oxite_SubscriptionAnonymous subscriptionAnonymous, oxite_User user) { if (subscriptionAnonymous == null) { return(getUser(user)); } return(new UserBase { Name = subscriptionAnonymous.Name, Email = subscriptionAnonymous.Email }); }
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)); } }
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; } }
public void SaveComment(Post post, Comment comment) { oxite_Comment persistenceComment = null; Guid commentID = comment.ID; if (comment.ID != Guid.Empty) { persistenceComment = context.oxite_Comments.Where(c => c.CommentID == comment.ID).FirstOrDefault(); } if (persistenceComment == null) { if (commentID == Guid.Empty) { commentID = Guid.NewGuid(); } persistenceComment = new oxite_Comment { CommentID = commentID }; context.oxite_Comments.InsertOnSubmit(persistenceComment); } persistenceComment.PostID = post.ID; persistenceComment.Body = comment.Body; persistenceComment.CreatedDate = comment.Created ?? DateTime.UtcNow; persistenceComment.CreatorIP = comment.CreatorIP; persistenceComment.ModifiedDate = DateTime.UtcNow; persistenceComment.State = (byte)comment.State; persistenceComment.UserAgent = comment.CreatorUserAgent; persistenceComment.oxite_Language = context.oxite_Languages.Where(l => l.LanguageName == comment.Language.Name).FirstOrDefault(); if (comment.Creator is User) { oxite_User persistenceUser = context.oxite_Users.Where(u => u.Username.ToLower() == comment.Creator.Name.ToLower()).FirstOrDefault(); if (persistenceUser == null) { throw new InvalidOperationException(string.Format("User {0} could not be found", comment.Creator.Name)); } persistenceComment.oxite_User = persistenceUser; } else { oxite_User anonymousUser = context.oxite_Users.Where(u => u.Username == "Anonymous").FirstOrDefault(); if (anonymousUser == null) { throw new InvalidOperationException("Could not find anonymous user"); } if (persistenceComment.oxite_CommentAnonymous == null) { persistenceComment.oxite_CommentAnonymous = new oxite_CommentAnonymous(); } persistenceComment.oxite_CommentAnonymous.Email = comment.Creator.Email; persistenceComment.oxite_CommentAnonymous.HashedEmail = comment.Creator.HashedEmail; persistenceComment.oxite_CommentAnonymous.Name = comment.Creator.Name; persistenceComment.oxite_CommentAnonymous.Url = comment.Creator.Url; persistenceComment.oxite_User = anonymousUser; } context.SubmitChanges(); comment.ID = commentID; comment.Created = persistenceComment.CreatedDate; }