public File Save(Guid postID, File file) { oxite_File fileToSave = null; oxite_Blogs_PostFileRelationship postFile; if (file.ID != Guid.Empty) { fileToSave = context.oxite_Files.FirstOrDefault(f => f.FileID == file.ID); } if (fileToSave == null) { context.oxite_Files.InsertOnSubmit( fileToSave = new oxite_File { FileID = file.ID != Guid.Empty ? file.ID : Guid.NewGuid() }); postFile = new oxite_Blogs_PostFileRelationship() { PostID = postID, FileID = fileToSave.FileID }; } else { postFile = context.oxite_Blogs_PostFileRelationships.FirstOrDefault(pfr => pfr.FileID == fileToSave.FileID); if (postFile != null && postFile.PostID != postID) { context.oxite_Blogs_PostFileRelationships.DeleteOnSubmit(postFile); postFile = new oxite_Blogs_PostFileRelationship() { PostID = postID, FileID = fileToSave.FileID }; } else { postFile = null; } } if (postFile != null) { context.oxite_Blogs_PostFileRelationships.InsertOnSubmit(postFile); } fileToSave.Length = file.SizeInBytes; fileToSave.MimeType = file.MimeType; fileToSave.TypeName = file.TypeName; fileToSave.Url = file.Url.ToString(); context.SubmitChanges(); return(projectFile(fileToSave)); }
public File Save(Guid scheduleItemID, File file) { oxite_File fileToSave = null; oxite_Conferences_ScheduleItemFileRelationship scheduleItemFile; if (file.ID != Guid.Empty) { fileToSave = context.oxite_Files.FirstOrDefault(f => f.FileID == file.ID); } if (fileToSave == null) { context.oxite_Files.InsertOnSubmit(fileToSave = new oxite_File { FileID = file.ID != Guid.Empty ? file.ID : Guid.NewGuid() }); scheduleItemFile = new oxite_Conferences_ScheduleItemFileRelationship() { ScheduleItemID = scheduleItemID, FileID = fileToSave.FileID }; } else { scheduleItemFile = context.oxite_Conferences_ScheduleItemFileRelationships.FirstOrDefault(pfr => pfr.FileID == fileToSave.FileID); if (scheduleItemFile != null && scheduleItemFile.ScheduleItemID != scheduleItemID) { context.oxite_Conferences_ScheduleItemFileRelationships.DeleteOnSubmit(scheduleItemFile); scheduleItemFile = new oxite_Conferences_ScheduleItemFileRelationship() { ScheduleItemID = scheduleItemID, FileID = fileToSave.FileID }; } else { scheduleItemFile = null; } } if (scheduleItemFile != null) { context.oxite_Conferences_ScheduleItemFileRelationships.InsertOnSubmit(scheduleItemFile); } fileToSave.Length = file.SizeInBytes; fileToSave.MimeType = file.MimeType; fileToSave.TypeName = file.TypeName; fileToSave.Url = file.Url.ToString(); context.SubmitChanges(); return(projectFile(fileToSave)); }
public bool Remove(Guid postID, Guid fileID) { bool removedFile = false; oxite_File foundFile = context.oxite_Files.FirstOrDefault(f => f.FileID == fileID); if (foundFile != null) { oxite_Blogs_PostFileRelationship foundPostFile = context.oxite_Blogs_PostFileRelationships.FirstOrDefault(pfr => pfr.PostID == postID && pfr.FileID == fileID); if (foundPostFile != null) { context.oxite_Files.DeleteOnSubmit(foundFile); context.SubmitChanges(); removedFile = true; } } return(removedFile); }
public bool Remove(Guid scheduleItemID, Guid fileID) { bool removedFile = false; oxite_File foundFile = context.oxite_Files.FirstOrDefault(f => f.FileID == fileID); if (foundFile != null) { oxite_Conferences_ScheduleItemFileRelationship foundScheduleItemFile = context.oxite_Conferences_ScheduleItemFileRelationships.FirstOrDefault(sifr => sifr.ScheduleItemID == scheduleItemID && sifr.FileID == fileID); if (foundScheduleItemFile != null) { context.oxite_Files.DeleteOnSubmit(foundFile); context.SubmitChanges(); removedFile = true; } } return(removedFile); }
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; } }
private static File projectFile(oxite_File f) { return(new File(f.FileID, f.TypeName, f.MimeType, new Uri(f.Url), f.Length)); }