/// <summary> /// Build a BlogEntry object from the database /// </summary> /// <param name="i"> /// the LINQ data object to build from /// </param> /// <returns> /// returns a BlogEntry object /// </returns> private static BlogEntry FillRecord(blogentry i) { BlogEntry result = null; if (i != null) { result = new BlogEntry(); result.id = i.id; if (i.author != null) { result.author = AuthorDB.GetItem(i.author.id); } else { result.author = null; } result.title = i.title; result.description = i.description; result.type = (i.type.Equals(Types.article.ToString()) ? Types.article : Types.blogentry); result.allowcomments = i.allowcomments; result.markprivate = i.markprivate; result.body = i.body; result.datecreated = i.datecreated; result.datepublished = i.datepublished; result.datemodified = i.datemodified; result.comments = CommentDB.GetList(i.id); result.tags = TagDB.GetList(i.id); } return(result); }
/// <summary> /// Deletes a specific BlogEntry from the database /// </summary> /// <param name="id"> /// the unique identifier of the blog entry to delete /// </param> /// <returns> /// returns true when deleted, false if not /// </returns> public static bool Delete(long id) { blogentry t = (from blogentry in db.blogentries where blogentry.id == id select blogentry).FirstOrDefault(); if (t != null) { try { // delete related comments of blog entry var comments = (from comment in db.comments where comment.blog_id == id select comment); db.comments.DeleteAllOnSubmit(comments); // delete tag mappings of blog entry var tagmaps = (from tagmap in db.blog_tags where tagmap.blog_id == id select tagmap); db.blog_tags.DeleteAllOnSubmit(tagmaps); // delete blog entry db.blogentries.DeleteOnSubmit(t); db.SubmitChanges(); } catch (ChangeConflictException) { db.ChangeConflicts.ResolveAll(RefreshMode.OverwriteCurrentValues); db.SubmitChanges(); } } return(t != null); }
/// <summary> /// Saves a BlogEntry object to the database /// </summary> /// <param name="myBlogEntry"> /// The BlogEntry object to save to the database /// </param> /// <returns> /// returns the id of the saved BlogEntry object in the database, or -1 if failed /// </returns> public static long Save(BlogEntry myBlogEntry) { blogentry t; bool found = false; if (myBlogEntry.id == -1) { // new record t = new blogentry(); t.datecreated = System.DateTime.Now; t.datepublished = myBlogEntry.datepublished; db.blogentries.InsertOnSubmit(t); found = true; } else { // existing record t = (from blogentry in db.blogentries where blogentry.id == myBlogEntry.id select blogentry).FirstOrDefault(); if (t != null) { found = true; t.id = myBlogEntry.id; } } if (found) { t.author = db.authors.Single(c => c.id == myBlogEntry.author.id); t.title = myBlogEntry.title; t.description = myBlogEntry.description; t.type = (myBlogEntry.type.Equals(Types.article) ? Types.article.ToString() : Types.blogentry.ToString()); t.allowcomments = myBlogEntry.allowcomments; t.markprivate = myBlogEntry.markprivate; t.body = myBlogEntry.body; t.datemodified = System.DateTime.Now; try { db.SubmitChanges(); // delete and recreate blog/tag mappings var tagmaps = (from tagmap in db.blog_tags where tagmap.blog_id == t.id select tagmap); db.blog_tags.DeleteAllOnSubmit(tagmaps); foreach (Tag tag in myBlogEntry.tags) { blog_tag bTag = new blog_tag(); bTag.blog_id = t.id; bTag.tag_id = tag.id; db.blog_tags.InsertOnSubmit(bTag); } db.SubmitChanges(); } catch (ChangeConflictException) { db.ChangeConflicts.ResolveAll(RefreshMode.OverwriteCurrentValues); db.SubmitChanges(); } return(t.id); } else { return(-1); } }
/// <summary> /// Get a specific BlogEntry from the database /// </summary> /// <param name="id"> /// the unique identifier of the blog entry to retrieve /// </param> /// <returns> /// returns a BlogEntry object if found, otherwise NULL /// </returns> public static BlogEntry GetItem(long id) { blogentry t = (from blogentry in db.blogentries where blogentry.id == id select blogentry).FirstOrDefault(); return(FillRecord(t)); }