public PostTag Save(Post post, PostTag tag) { oxite_Tag foundTag = context.oxite_Tags.Where(t => string.Compare(t.TagName, tag.Name, true) == 0).FirstOrDefault(); Guid tagID; if (foundTag != null) { tagID = foundTag.TagID; } else { tagID = tag.ID == Guid.Empty ? Guid.NewGuid() : tag.ID; context.oxite_Tags.InsertOnSubmit( new oxite_Tag { ParentTagID = tagID, TagID = tagID, TagName = tag.Name, CreatedDate = tag.Created == default(DateTime) ? DateTime.UtcNow : tag.Created } ); context.SubmitChanges(); } return(( from ptr in context.oxite_Blogs_PostTagRelationships join t in context.oxite_Tags on ptr.TagID equals t.TagID where ptr.PostID == post.ID && ptr.TagID == tagID select new PostTag(t.TagID, t.TagName, ptr.TagDisplayName, t.CreatedDate) ).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)); }
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; } }