/// <summary> /// Returns a <see cref="Fan.Blogs.Post"/> given a <see cref="PostEntity"/>, maps to the post's specific type. /// </summary> /// <remarks> /// /// </remarks> /// <exception cref="BlogException">If the PostType is not defined</exception> internal static Post MapPost(PostEntity postEnt, Nav nav, User user) { // take tags out from PostEntity List<Tag> tagList = new List<Tag>(); foreach (var tagEnt in postEnt.TagEntities) { tagList.Add(new Tag(nav) { TagId = tagEnt.TagId, TagName = WebHelper.UrlDecode(tagEnt.Name), PostCount = tagEnt.PostEntities.Count, Description = tagEnt.Description, }); } // fill in PostType specific post switch ((PostType)postEnt.PostType) { case PostType.Article: return _MapArticle(postEnt, nav, tagList, user); default: throw new SiteException("Not supported post type."); } }
/// <summary> /// /// </summary> /// <param name="postEnt"></param> /// <param name="nav"></param> /// <param name="tagList"></param> /// <param name="user"></param> /// <returns></returns> private static Article _MapArticle(PostEntity postEnt, Nav nav, List<Tag> tagList, User user) { var md = new MarkdownDeep.Markdown(); return new Article(nav) { PostId = postEnt.PostId, PostGuidId = postEnt.PostGuidId, PostStatus = postEnt.PostStatus, UserId = postEnt.UserId, Author = user.DisplayName, Email = user.Email, UserName = user.UserName, PostSlug = postEnt.Slug, Subject = WebHelper.HtmlDecode(postEnt.Subject), // TODO: should I decode here? Body = postEnt.Body, DateCreated = postEnt.DateCreated, DateUpdated = postEnt.DateUpdated, ViewCount = postEnt.ViewCount, IP = postEnt.IP, Tags = tagList, }; }
/// <summary> /// Adds a post /// </summary> /// <param name="post"></param> /// <returns></returns> public int AddPost(Post post) { using (var db = new BlogDbContext()) { var postEnt = new PostEntity(); #region common //postEnt.PostId = post.PostId; postEnt.NavId = post.Nav.NavId; postEnt.PostGuidId = post.PostGuidId; postEnt.PostType = post.PostType; postEnt.PostStatus = post.PostStatus; postEnt.UserId = post.UserId; postEnt.Subject = post.Subject; postEnt.Body = post.Body; postEnt.IP = post.IP; postEnt.DateCreated = post.DateCreated; postEnt.DateUpdated = post.DateUpdated; postEnt.ViewCount = post.ViewCount; #endregion #region tags if (post.Tags != null) { // Add new labels and association of label/post foreach (var tag in post.Tags) { // find it var tagEnt = db.TagEntities.FirstOrDefault(t => t.Name.ToLower() == tag.TagName.ToLower() && t.NavId == post.Nav.NavId); // if tag is new, add the tag; if publish is true, set PostCount to 1 else to 0; if (tagEnt == null) { tagEnt = new TagEntity(); tagEnt.Name = tag.TagName; tagEnt.Description = tag.Description; tagEnt.PostCount = post.PostStatus == PostStatus.Published ? 1 : 0; tagEnt.NavId = post.Nav.NavId; db.TagEntities.Add(tagEnt); } // if tag exists and publish is true, update PostCount to inc 1 else do nothing. else if (post.PostStatus == PostStatus.Published) { tagEnt.PostCount++; } // add to tag/post association postEnt.TagEntities.Add(tagEnt); } } #endregion #region type specific if (post.PostType == PostType.Article) { Article article = (Article)post; postEnt.Slug = article.RequireSlugValidationAtDataSource ? _ValidateSlug(db, post.Nav, article.PostSlug, post.DateCreated) : article.PostSlug; } #endregion #region archive if (post.PostStatus == PostStatus.Published) // touch archive only if the post is published { // try find ent var archiveEnt = db.ArchiveEntities.FirstOrDefault(a => a.NavId == post.Nav.NavId && a.Year == post.DateCreated.Year); if (archiveEnt == null) // row not exist, add it { archiveEnt = new ArchiveEntity(); archiveEnt.NavId = post.Nav.NavId; archiveEnt.Year = post.DateCreated.Year; switch (post.DateCreated.Month) { case 1: archiveEnt.M01 = 1; break; case 2: archiveEnt.M02 = 1; break; case 3: archiveEnt.M03 = 1; break; case 4: archiveEnt.M04 = 1; break; case 5: archiveEnt.M05 = 1; break; case 6: archiveEnt.M06 = 1; break; case 7: archiveEnt.M07 = 1; break; case 8: archiveEnt.M08 = 1; break; case 9: archiveEnt.M09 = 1; break; case 10: archiveEnt.M10 = 1; break; case 11: archiveEnt.M11 = 1; break; default: archiveEnt.M12 = 1; break; } db.ArchiveEntities.Add(archiveEnt); } else // otherwise update here { switch (post.DateCreated.Month) { case 1: archiveEnt.M01++; break; case 2: archiveEnt.M02++; break; case 3: archiveEnt.M03++; break; case 4: archiveEnt.M04++; break; case 5: archiveEnt.M05++; break; case 6: archiveEnt.M06++; break; case 7: archiveEnt.M07++; break; case 8: archiveEnt.M08++; break; case 9: archiveEnt.M09++; break; case 10: archiveEnt.M10++; break; case 11: archiveEnt.M11++; break; default: archiveEnt.M12++; break; } } } #endregion db.PostEntities.Add(postEnt); db.SaveChanges(); return postEnt.PostId; } }